devdeer.Libraries.RestClient 4.1.5

dotnet add package devdeer.Libraries.RestClient --version 4.1.5                
NuGet\Install-Package devdeer.Libraries.RestClient -Version 4.1.5                
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="devdeer.Libraries.RestClient" Version="4.1.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add devdeer.Libraries.RestClient --version 4.1.5                
#r "nuget: devdeer.Libraries.RestClient, 4.1.5"                
#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 devdeer.Libraries.RestClient as a Cake Addin
#addin nuget:?package=devdeer.Libraries.RestClient&version=4.1.5

// Install devdeer.Libraries.RestClient as a Cake Tool
#tool nuget:?package=devdeer.Libraries.RestClient&version=4.1.5                

devdeer.Libraries.RestClient

Disclaimer

If you want to use this package you should be aware of some principles and practices we at DEVDEER use. So be aware that this is not backed by a public repository. The purpose here is to give out customers easy access to our logic. Nevertheless you are welcome to use this lib if it provides any advantages.

Summary

This package provides a simple JSON REST client which is based on the HttpClient class. It is designed to be used in a .NET Core environment. The main purpose of this lib is to provide a simple way to call REST APIs and to handle the responses in a simple way. The lib is designed to be used in a dependency injection context.

Usage

Setup

In order to use the functionallity install the nuget package into your project. The you need to create a new type which inherits from JsonRestClient as follows:

public class MyRestClient : JsonRestClient
{
    public MyJsonClient(HttpClient httpClient) : base(httpClient)
    {
    }
}

The above example shows only the simples of 9 available constructor overloads. You need to pick only one of them in order to devide which DI (dependency injection) you want to use.

If you want to use your REST client conveniently you usually add it to your DI:

services.AddHttpClient<MyJsonClient>(
    (sp, client) =>
    {        
        var config = sp.GetRequiredService<IConfiguration>();        
        client.BaseAddress = new Uri($"https://{config["OtherApi:Hostname"]}");        
        client.DefaultRequestHeaders.Add("Accept", "application/json");
        // add more initialization if needed
    });

In the example shown you would have some value in your appsettings.json like:

{
    "OtherApi": {
        "Hostname": "other.api.com"
    }
}

Now lets say you have some component which needs to make JSON-REST-requests. You could do it like this:

public class MyComponent
{
    private MyJsonClient _client;

    public MyComponent(MyJsonClient client)
    {
        _client = client;
    }

    public async ValueTask<string> GetSomeDataAsync()
    {
        return await _client.GetAsync<string>("The/Relative/Endpoint");
    }

    public async ValueTask<SomeDataType> GetSomeDataAsync()
    {
        return await _client.GetAsync<SomeDataType>("Other/Relative/Endpoint");
    }
}

Passing in configuration options

There are other constructor overloads which allow you to pass the client configuration as well. Here is one example using the default options-injection:

public class MyRestClient : JsonRestClient
{
    public MyJsonClient(HttpClient httpClient, IOptions<JsonRestClientOptions> options) : base(httpClient, options)
    {
    }
}

In order for this to work you need to register the options in your startup-DI. This examples uses the helper method from devdeer.Libraries.Abstractions which is a transient Nuget package that you get with this one automatically:

using devdeer.Libraries.Abstractions.Extensions;

services.RegisterOption<JsonRestClientOptions>("MyRestClient");

and the appsettings.json:

{
    "OtherApi": {
        "Hostname": "other.api.com"
    },
    "MyRestClient": {
        "Resiliency": {
            "UseRetryPolicy": true,
            "RetryTimeoutBase": 1,
            "RetryCount": 5
        }
    }

This assumes that you only need 1 JsonRestClient in your project and so you can use the default JsonRestClientOptions.

If you however need to have several implementations of JsonRestClient in your project you need to do a little more work.

Lets say you want to have 2 clients like this:

public class MyRestClient1 : JsonRestClient
{
    public MyJsonClient(HttpClient httpClient, IOptions<JsonRestClientOptions> options) : base(httpClient, options)
    {
    }
}

public class MyRestClient2 : JsonRestClient
{
    public MyJsonClient(HttpClient httpClient, IOptions<JsonRestClientOptions> options) : base(httpClient, options)
    {
    }
}

This would work but both clients would then share the same JsonRestClientOptions instance. So lets create 2 new types:

public class JsonRestClientOptions1 : JsonRestClientOptions
{
}

public class JsonRestClientOptions2 : JsonRestClientOptions
{
}

Change your app settings now:

{
    "OtherApi": {
        "Hostname": "other.api.com"
    },
    "RestClients": {
        "1": {
            "Resiliency": {
                "UseRetryPolicy": true,
                "RetryTimeoutBase": 1,
                "RetryCount": 5
            }
        },
        "2": {            
        },
    }

The first client should use resiliency and the other should not. Now change your startup:

using devdeer.Libraries.Abstractions.Extensions;

services.RegisterOption<JsonRestClientOptions1>("RestClients:1");
services.RegisterOption<JsonRestClientOptions2>("RestClients:2");

Finally change the constructors of your types:

public class MyRestClient1 : JsonRestClient
{
    public MyJsonClient(HttpClient httpClient, IOptions<JsonRestClientOptions1> options) : base(httpClient, options)
    {
    }
}

public class MyRestClient2 : JsonRestClient
{
    public MyJsonClient(HttpClient httpClient, IOptions<JsonRestClientOptions2> options) : base(httpClient, options)
    {
    }
}

Calling methods

There are now several ways to use the client functionallity. The easiest way is to use wrapper functions like GetAsync<T> and PostAsync<T> as shown above.

One peculiar thing about our package are the *Oneway* functions which are there for you if just want to send a request to an endpoint and don't expect any result.

Also we provide a lot of overloads on the methods. In principal here are the ways to call our methods:

  1. Provide relative path, HTTP method and an optional request-body represented as a string.
  2. Provide relative path, HTTP method and an optional request-body represented as a pre-build custom HttpContent.
  3. Provide the pre-build HttpRequestMessage directly.

This scheme applies to all wrappers and direct Invoke* methods.

On excemption are the PerformDynamicRequestAsync overloads. This can be used like this:

var genericTemp = await _client.PerformDynamicRequestAsync($"path", HttpMethod.Get);
if (result == null)
{
    // Whatever
}
 return result["jsonRoute"].GetProperty("someProp").GetDouble();

In other words: the dynamic methods will return a dynamic type and allow you to call an API without need to map the data to .NET types. Be aware that this has performance implications and should be avoided normally.

Resiliency

This package uses Polly and Microsoft.Extensions.Resilience to incorporate resiliency optionally.

A documentation of this is upcoming...

About DEVDEER

DEVDEER is a company from Magdeburg, Germany which is specialized in consulting, project management and development. We are very focussed on Microsoft Azure as a platform to run our products and solutions in the cloud. So all of our backend components usually runs in this environment and is developed using .NET. In the frontend area we are using react and a huge bunch of packages to build our UIs.

You can find us online:

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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
4.1.5 102 12/13/2024
4.1.4 93 12/13/2024
4.1.3 76 12/13/2024
4.1.2 80 12/12/2024
4.1.1 73 12/12/2024
4.1.0 77 12/12/2024
4.0.0 174 12/11/2024
3.1.1 370 10/12/2024
3.1.0 139 10/3/2024
3.0.6 453 8/18/2024
3.0.5 205 6/12/2024
3.0.4 116 5/29/2024
3.0.3 154 5/28/2024
3.0.2 112 5/24/2024
3.0.1 107 5/24/2024
3.0.0 151 5/14/2024
2.1.5 126 4/19/2024
2.1.4 219 3/10/2024
2.1.3 118 3/10/2024
2.1.2 120 2/18/2024
2.1.1 253 1/18/2024
2.1.0 230 12/31/2023
2.0.0 174 12/10/2023
1.1.2 186 10/29/2023
1.1.1 174 9/19/2023
1.1.0 140 9/16/2023
1.0.8 183 9/14/2023
1.0.7 153 9/14/2023
1.0.6 161 9/13/2023
1.0.4 143 9/13/2023
1.0.3 148 9/13/2023
1.0.2 174 9/7/2023
1.0.1 176 9/7/2023
1.0.0 179 9/6/2023
0.2.4 293 8/15/2023
0.2.3 279 8/15/2023
0.2.2 185 8/15/2023
0.2.1 192 8/15/2023
0.2.0 188 8/15/2023
0.0.1 198 8/14/2023

- Polly is now integrated into this package and can be switched on using the new JsonRestClientOptions.
- Added more content to the package README.