Tago.Extensions.HttpClient 3.1.1-preview-20231020-01

This is a prerelease version of Tago.Extensions.HttpClient.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Tago.Extensions.HttpClient --version 3.1.1-preview-20231020-01                
NuGet\Install-Package Tago.Extensions.HttpClient -Version 3.1.1-preview-20231020-01                
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="Tago.Extensions.HttpClient" Version="3.1.1-preview-20231020-01" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tago.Extensions.HttpClient --version 3.1.1-preview-20231020-01                
#r "nuget: Tago.Extensions.HttpClient, 3.1.1-preview-20231020-01"                
#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 Tago.Extensions.HttpClient as a Cake Addin
#addin nuget:?package=Tago.Extensions.HttpClient&version=3.1.1-preview-20231020-01&prerelease

// Install Tago.Extensions.HttpClient as a Cake Tool
#tool nuget:?package=Tago.Extensions.HttpClient&version=3.1.1-preview-20231020-01&prerelease                

Tago.Extensions.HttpClient

Tago.Extensions.HttpClient is a powerful NuGet package that simplifies the implementation of HTTP REST API methods in .NET projects. It provides extensions to the HttpClient class, allowing you to easily add and manage REST API methods. With Tago.Extensions.HttpClient, you can reduce code duplication and improve the maintainability of your codebase.

Key Features

  • Effortless REST API Method Management: Tago.Extensions.HttpClient allows you to add REST API methods with different HTTP verbs (GET, POST, PUT, DELETE, etc.) and endpoints, making it easy to organize and maintain your API integration code.
  • Flexible Callbacks: You can attach custom callback actions to handle the API response for each method, providing the flexibility to process the response according to your application's specific needs.
  • Support for Generic Deserialization: Tago.Extensions.HttpClient leverages generics to enable seamless deserialization of the API response content into strongly typed objects, eliminating the need for manual parsing.
  • Cancellation Support: The library supports cancellation using CancellationToken, allowing you to cancel ongoing API requests when needed.
  • Global HTTP Call Inspection and Modification: Tago.Extensions.HttpClient provides the ability to intercept, inspect, modify, or even mock HTTP calls globally. This feature allows you to easily add custom logic or modify requests and responses at a global level, enabling advanced scenarios such as request/response logging, adding authentication headers, or simulating different responses for testing purposes.
  • Integration with Dependency Injection: Tago.Extensions.HttpClient can be easily integrated into your application's dependency injection container, facilitating the management and configuration of the HTTP client instances.

Getting Started

To get started with Tago.Extensions.HttpClient, follow these steps:

  1. Install the Tago.Extensions.HttpClient NuGet package into your project.
  2. Register the RestClient (or Tago.Extensions.HttpClient) as a service in your dependency injection container.
  3. Inject the IRestClient instance (or the corresponding interface) into your classes or components that require HTTP client functionality.
  4. Use the provided methods to add REST API methods and make HTTP requests.

Example Usage

Here's an example that demonstrates the basic usage of Tago.Extensions.HttpClient:

DI registration:
// In your startup configuration or composition root
using Tago.Extensions.Http;

public void ConfigureServices(IServiceCollection services)
{
    // Register TagoHttpClient as a transient service
    services.AddRestClient();
    
    //or
    //services.AddRestClient("clientName");

    // Other service registrations...
}

Using IRestClient:

using Tago.Extensions.Http;

public class MyService
{
    private readonly IRestClient _restClient;

    public MyService(IRestClient restClient)
    {
        _restClient = restClient;
    }

    public async Task DoSomething()
    {
        var response = await _restClient.GetAsync<MyModel>("/api/resource");
        if( response.IsSuccess )
        {
            MyModel model = response.Data;
        
            // Process the response...
        }
    }
}

Or using IRestClientFactory:

public class MyService2
{
    private readonly IRestClient _restClient;

    public MyService2(IRestClientFactory restClientFactory)
    {
        _restClient = restClientFactory.GetClient("clientName");
    }

    public async Task DoSomething(CanncellationToken canncellationToken)
    {
        var response = await _restClient.GetAsync<MyModel>("/api/resource", opts => {
            opts.Headers.Add("Authorization", "token");
        }, canncellationToken);
        
        // Process the response...
    }
}


In the above example, the MyService class depends on the IRestClient interface provided by Tago.Extensions.HttpClient. You can then use the _restClient instance to make HTTP requests and handle the API responses in a strongly typed manner.

GetAsync<T>(string endpoint, Action<HttpRequestOptions<T>> options = null, CancellationToken cancellationToken = default)

Sends an asynchronous GET request to the specified API endpoint and returns an IApiResponse<T>.

Parameters:

  • endpoint: The API endpoint to send the GET request to.
  • options (optional): An action that allows customizing the HTTP request options, such as headers, query parameters, or authentication.
  • cancellationToken (optional): A cancellation token that can be used to cancel the request.

Returns: Task<IApiResponse<T>>

  • A task representing the asynchronous operation.
  • The IApiResponse<T> object that encapsulates the HTTP response and deserialized content of type T.
Global Interception

// With DI
services.AddRestClientRequestInterceptor<T>() where T : class, IRestClientRequestInterceptor
services.AddRestClientResponseInterceptor<T>() where T : class, IRestClientResponseInterceptor
services.SetRestClientMocker<T>() where T : class, IRestClientMocker

// OR
RestClientInterceptor.InterceptRequest = async (name, msg) =>
{
    if (!msg.Headers.Contains("Authorization"))
    {
        msg.Headers.TryAddWithoutValidation("Authorization", "Some Token");
    }
};

The code snippet demonstrates how to use RestClientInterceptor.InterceptRequest to intercept and modify HTTP requests globally in Tago.Extensions.HttpClient. This feature allows you to add custom logic to inspect and modify requests before they are sent.

In the example code, a callback function is assigned to the RestClientInterceptor.InterceptRequest property. This callback function takes two parameters: name and message. Inside the callback, it checks if the request message (msg) does not contain an "Authorization" header. If the header is missing, the code adds an "Authorization" header with a default value of "Some Token".

By utilizing this functionality, you can customize and manipulate HTTP requests at a global level, enabling scenarios such as adding authentication headers or modifying request parameters before they are sent. This gives you greater control and flexibility in managing your API integration logic.

Contributions and Support

Contributions, bug reports, and feature requests are welcome! If you encounter any issues or need assistance, feel free to contact us at support@tago-solutions.com

License

This project is licensed under the MIT License.

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Tago.Extensions.HttpClient:

Package Downloads
Tago.Extensions.Jwt.Core

Generate & Validate Jwt/Jwe tokens

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.5 100 11/7/2024
8.0.4 148 8/8/2024
8.0.3 140 4/30/2024
8.0.2 143 3/17/2024
8.0.1 135 12/25/2023
8.0.0 197 12/5/2023
3.1.1-preview-20231020-01 133 11/2/2023
3.1.1-preview-20231005-01 244 10/14/2023