Salix.RestClient
2.0.1
See the version list below for details.
dotnet add package Salix.RestClient --version 2.0.1
NuGet\Install-Package Salix.RestClient -Version 2.0.1
<PackageReference Include="Salix.RestClient" Version="2.0.1" />
paket add Salix.RestClient --version 2.0.1
#r "nuget: Salix.RestClient, 2.0.1"
// Install Salix.RestClient as a Cake Addin #addin nuget:?package=Salix.RestClient&version=2.0.1 // Install Salix.RestClient as a Cake Tool #tool nuget:?package=Salix.RestClient&version=2.0.1
Salix.RestClient
Wrapper for RESTful service (API) client, using IHttpClientFactory in ASP.NET framework. Package provides abstract base class to be used in actual client implementations.
Usage
Create your own API client, deriving from AbstractRestClient
class.
public Interface IMyServiceClient : IAbstractRestClient
{
}
public class MyServiceClient : AbstractRestClient, IMyServiceClient
{
/// <summary>
/// Client to work with MyService API.
/// </summary>
/// <param name="httpClient">The HTTP client (.Net Framework).</param>
/// <param name="parameters">The parameters for MyService API client.</param>
/// <param name="logger">The logging object (ILogger in MS.Extensions.Logging).</param>
public MyServiceClient(HttpClient httpClient, MyServiceClientSettings parameters, ILogger<MyServiceClient> logger)
: base(httpClient, parameters, logger)
{
}
}
Client needs some settings (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 ServiceUrl
. All other are optional if you want to change authentication mechanism or add some default header key-values for all API calls.
new MyServiceClientSettings {
ServiceUrl = "https://api.myservice.com",
Authentication = new RestServiceAuthentication
{
AuthenticationType = ApiAuthenticationType.Basic,
UserName = "apiuser",
Password = "secret"
},
RequestHeaders = new Dictionary<string, string> { { "key", "value" } }
}
Using MS Extensions for Dependency Injection, register them appropriately
services.AddHttpClient(); // This registers IHttpClientFactory
services.AddSingleton(myServiceClientSettings); // instance of configuration for API client
services.AddScoped<IMyServiceClient, MyServiceClient>();
Then use client injected instance in your business logic:
public class BusinessLogic
{
private readonly IMyServiceClient _api;
public BusinessLogic(IMyServiceClient apiClient) => _api = apiClient;
public async Task LogicMethod()
{
var receivedData = await _api.GetAsync<MyData>("endpoint/data/uri");
}
}
If you need to unit-test your business logic, mock your client dependencies:
_clientMock = new Mock<IMyServiceClient>();
_clientMock.Setup(x => x.GetAsync<MyData>("endpoint/data/uri"))
.Returns(Task.FromResult(new MyData
{
SomeProp = "la la la"
}));
Using Bearer authentication with Identity Server 4
Refer to documentation in Official site.
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 (>= 3.1.18)
- Microsoft.Extensions.Logging.Abstractions (>= 3.1.18)
- Newtonsoft.Json (>= 11.0.2)
- System.Text.Json (>= 5.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Intellisense comments. Comment fixes. Test project package updates.