Salix.RestClient
3.3.3
dotnet add package Salix.RestClient --version 3.3.3
NuGet\Install-Package Salix.RestClient -Version 3.3.3
<PackageReference Include="Salix.RestClient" Version="3.3.3" />
paket add Salix.RestClient --version 3.3.3
#r "nuget: Salix.RestClient, 3.3.3"
// Install Salix.RestClient as a Cake Addin #addin nuget:?package=Salix.RestClient&version=3.3.3 // Install Salix.RestClient as a Cake Tool #tool nuget:?package=Salix.RestClient&version=3.3.3
Salix.RestClient
Wrapper around IHttpClientFactory HttpClient in System.Net.Http (Core 3+) to provide easier access to RESTful APIs from backend .Net code.
Package provides abstract base class for any of 3 usage types - with Factory, Named client/factory and Typed client to be used to implement your own REST service HttpClient.
Included HttpClient extensions - shortcuts to do GET, POST, PUT, PATCH and DELETE operations with variety of parameters.
Operations can return raw HttpResponseMessage
or directly deserialized - typed data object. Internally it uses Json.Net (default) which can be switched to use System.Text.Json
deserializer.
As parameters you can supply path variables and/or query parameters in addition to data content. These are wrapped into own managing objects to simplify their usage.
Example:
/api/parents/{id}/children/{childId}?skip=0&take=10
can be executed as this GET extension method call, returning strongly typed DomainObject
:
var result = await _client.GetAsync<DomainObject>("/api/parents/{id}/children/{childId}", new PathParameters("id", 12, "childId", 15), new QueryParameters(new { skip = 0, take = 10 })
Package expects it to have concrete settings object, from witch it gathers BaseAddress
, [optionally] factory name, authentication approach, any default headers ("text/json" is added by default, if not specified implicitly).
Custom RestClientException
is thrown on failures, containing all the information about request failure.
Built in timer will get you execution time you can read after each request for any monitoring needs.
Development time verbose logging will output plenty of details on internal work to logging output (Use Debug() to see it in Visual Studio output window).
Usage
More detailed documentation is in Wiki.
Create your own API client class, deriving from AbstractRestClient
class and use any of four constructors, depending on your approach - constructors with IHttpClientFactory for factory or named approaches or constructors with HttpClient instance for typed clients.
public class MyServiceClient : AbstractRestClient
{
/// <summary>
/// Client to work with MyService API. Here assumed typed client implementation/setup with default Json.Net serizalizer.
/// </summary>
public MyServiceClient(HttpClient httpClient, MyServiceClientSettings parameters, ILogger<MyServiceClient> logger)
: base(httpClient, parameters, logger)
{
}
}
Client needs settings to be defined (derived from RestServiceSettings
) to know where and how to connect to API:
// Yepp. Nothing required for this class if nothing special is needed.
public class MyServiceClientSettings : RestServiceSettings
{
}
For this you need to create this class instance supplying base class property values (from configuration). The only required property to set is BaseAddress
. All other are optional if you want to change authentication mechanism or add some default header key-values for all API calls.
new MyServiceClientSettings {
BaseAddress = "https://api.myservice.com",
Authentication = new RestServiceAuthentication
{
AuthenticationType = ApiAuthenticationType.Basic,
UserName = "apiuser",
Password = "secret"
},
FactoryName = "namedClient",
RequestHeaders = new Dictionary<string, string> { { "version", "2.0" } }
}
Using MS Extensions for Dependency Injection, register them appropriately
services.AddHttpClient<MyServiceClient>(); // This registers IHttpClientFactory with typed client
services.AddSingleton(myServiceClientSettings); // instance of configuration for API client
Then use client injected instance in your logic/controller/handler classes:
public class BusinessLogic
{
private readonly MyServiceClient _api;
// ctor
public BusinessLogic(MyServiceClient apiClient) => _api = apiClient;
public async Task LogicMethod()
{
var receivedData = await _api.GetAsync<MyData>("endpoint/data/uri");
}
}
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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.Http (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.1)
- Newtonsoft.Json (>= 13.0.1)
- System.Text.Json (>= 6.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added check for "data" being a subclass of HttpContent and then passing it to request as-is. This allows (as one exmaple) to submit files as MultipartFormDataContent.