MondoCore.Rest
1.2.0
dotnet add package MondoCore.Rest --version 1.2.0
NuGet\Install-Package MondoCore.Rest -Version 1.2.0
<PackageReference Include="MondoCore.Rest" Version="1.2.0" />
<PackageVersion Include="MondoCore.Rest" Version="1.2.0" />
<PackageReference Include="MondoCore.Rest" />
paket add MondoCore.Rest --version 1.2.0
#r "nuget: MondoCore.Rest, 1.2.0"
#:package MondoCore.Rest@1.2.0
#addin nuget:?package=MondoCore.Rest&version=1.2.0
#tool nuget:?package=MondoCore.Rest&version=1.2.0
MondoCore.Rest
Class/interface for calling REST apis <br>
Dependency Injection
- In your dependency injection code create an instance of an api.
- Each unique api should be a different instance of RestApi.
- Differentiate each api by using a different class.
- You can pass in an optional IHeaderFactory. This is used to insert headers, e.g. a bearer token, at request time.
<br/>
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// ...
builder.Services.AddHttpClient();
// Register the apis. Automobile and Truck are just used to differentiate apis and can be blank classes
builder.Services.AddRestApi<Automobile>("cars", "https://www.notrealcars.com)
.AddRestApi<Truck>("trucks", "https://www.notrealtrucks.com, myHeaderFactory);
// ...
}
}
public static class Extensions
{
public static IServiceCollection AddRestApi<T>(this IServiceCollection services, string name, string url)
{
// Register the api and url with the HttpClientFactory
services.AddHttpClient(name: name, configureClient: (p, client) =>
{
client.BaseAddress = new Uri(url);
client.Timeout = TimeSpan.FromSeconds(60);
});
// Now inject the RestApi class
services.AddScoped<IRestApi<T>>( p=> new RestApi<T>(p.GetRequiredService<IHttpClientFactory>(), name));
return services;
}
}
<br>
Calling an API with IRestApi
Inject the IRestApi interface into your class
using MondoCore.Rest;
public class CarService
{
private readonly IRestApi<Automoble> _api;
public CarService(IRestApi<Automoble> api)
{
_api = api;
}
public async Task<List<Automobile>> GetGreenCars()
{
return _api.Get<List<Automobile>>("all/green"); // Appends "/all/green" onto the url given at DI time
}
public async Task<Automobile> GetCar(string id)
{
return _api.Get<Automobile>("car/" + id); // Appends "car/1" (for instance) onto the url given at DI time
}
}
<br>
Reference
IRestAPI<T>
Interface used to call REST API. Note that the generic type isn't actually used in the interface itself, it's just used to differentiate apis in dependency injection.
Task SendRequest<TRequest>(HttpMethod method, string url, TRequest? content = default(TRequest?), object? headers = null)
No need to call this directly. See the default interface methods below.
Task<TResponse> SendRequest<TRequest, TResponse>(HttpMethod method, string url, TRequest? content = default(TRequest?), object? headers = null);
No need to call this directly. See the default interface methods below.
Default Methods
Task<T> Get<T>(string url, object? headers = null)
Does a GET request to the api. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary
Task<TResponse> Post<TRequest, TResponse>(string url, TRequest content, object? headers = null)
Does a POST request to the api that returns a response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary
Task Post<TRequest>(string url, TRequest content, object? headers = null)
Does a POST request to the api thats returns no response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary
Task<TResponse> Put<TRequest, TResponse>(string url, TRequest content, object? headers = null)
Does a PUT request to the api that returns a response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary
Task Put<TRequest>(string url, TRequest content, object? headers = null)
Does a PUT request to the api thats returns no response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary
Task<TResponse> Patch<TRequest, TResponse>(string url, TRequest content, object? headers = null)
Does a PATCH request to the api that returns a response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary
Task Patch<TRequest>(string url, TRequest content, object? headers = null)
Does a PATCH request to the api thats returns no response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary
Task Delete<TRequest>(string url, object? headers = null)
Does a DELETE request to the api thats returns no response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary
License
MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.Http (>= 8.0.0)
- MondoCore.Common (>= 1.5.2)
- System.Net.Http.Json (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.