Salix.RestClient 2.0.1

There is a newer version of this package available.
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
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Salix.RestClient" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Salix.RestClient --version 2.0.1
#r "nuget: Salix.RestClient, 2.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.3.3 2,063 9/14/2022
3.3.2 837 8/13/2022
3.3.1 1,080 5/22/2022
3.3.0 851 5/22/2022
3.2.0 865 5/15/2022
3.1.0 956 2/22/2022
2.0.1 885 1/15/2022
2.0.0 752 8/13/2021
1.0.0 805 8/12/2021
1.0.0-rc1 694 7/9/2021

Intellisense comments. Comment fixes. Test project package updates.