Kampute.HttpClient 2.0.0

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

// Install Kampute.HttpClient as a Cake Tool
#tool nuget:?package=Kampute.HttpClient&version=2.0.0                

Kampute.HttpClient

Kampute.HttpClient is a .NET library designed to simplify HTTP communication with RESTful APIs by enhancing the native HttpClient capabilities. Tailored for developers seeking a potent yet flexible HTTP client for API integration within .NET applications, it combines ease of use with a wide array of functionalities to address the complexities of web service consumption.

Key Features

  • Shared HttpClient Instances: Facilitates the reuse of a single HttpClient instance across multiple HttpRestClient instances, promoting efficient resource and connection management. This approach significantly boosts performance in scenarios involving concurrent access to multiple services or API endpoints.

  • Flexible HttpClient Configuration: Allows the integration of custom or shared HttpClient instances, complete with configurations for message handlers, timeouts, and advanced authentication mechanisms to fit specific application needs.

  • Dynamic Request Customization: Offers the capability to define headers and properties scoped to specific request blocks, allowing for temporary changes that do not affect the global configuration. Scoped headers and properties ensure that modifications are contextually isolated, enhancing maintainability and reducing the risk of configuration errors during runtime.

  • Custom Error Handling and Exception Management: Converts HTTP response errors into detailed, meaningful exceptions, streamlining the process of interpreting API-specific errors with the aid of a customizable error response type set through the ResponseErrorType property. Furthermore, it enhances flexibility in error management with the ErrorHandlers collection, allowing for response status code-specific handling. Developers can craft and utilize custom IHttpErrorHandler implementations to address distinct HTTP errors directly, facilitating the development of refined retry strategies and precise error responses tailored to specific needs.

  • Retry Strategies with Backoff Mechanisms: Implements backoff strategies to handle transient failures and network interruptions effectively. These strategies, configurable via the BackoffStrategy property, ensure resilient communication by dictating the logic for retrying requests, thereby preventing server overload and optimizing resource use.

  • Modular Content Processing: Supports extendable serialization/deserialization modules for seamless integration with common and custom content types. It uses a collection of response deserializers that automatically convert HTTP response content into .NET objects based on the response's Content-Type, and proactively informs the service of the content types it is configured to accept by setting the appropriate Accept headers. This dual-functionality simplifies the process of working with API responses and ensures seamless data integration by aligning expected response formats with the client’s capabilities.

  • Streamlined Authentication and Authorization: Simplifies the process of integrating various authentication schemes and dynamic reauthorization, facilitating straightforward implementation of authentication strategies.

  • Request and Response Interception: Provides events such as BeforeSendingRequest and AfterReceivingResponse for executing custom logic before sending a request or after receiving a response. This feature enables detailed request modification, response inspection, and logging, offering developers full control over the HTTP communication process.

  • Asynchronous API for Enhanced Performance: Promotes fully asynchronous network operations with support for cancellation tokens, ensuring efficient management of long-running requests in line with modern asynchronous programming practices in .NET.

Serialization Support

By default, Kampute.HttpClient does not include any content deserializer. To accommodate popular content types, the following extension packages are available:

  • Kampute.HttpClient.Json: Utilizes the System.Text.Json library for handling JSON content types, offering high-performance serialization and deserialization that integrates tightly with the .NET ecosystem.

  • Kampute.HttpClient.NewtonsoftJson: Leverages the Newtonsoft.Json library for handling JSON content types, providing extensive customization options and compatibility with a vast number of JSON features and formats.

  • Kampute.HttpClient.Xml: Employs the XmlSerializer for handling XML content types, enabling straightforward serialization and deserialization of XML into .NET objects using custom class structures.

  • Kampute.HttpClient.DataContract: Utilizes the DataContractSerializer for handling XML content types, focusing on serialization and deserialization of .NET objects into XML based on data contract attributes for fine-grained control over the XML output.

For scenarios where the provided serialization packages do not meet specific requirements, Kampute.HttpClient allows the implementation of custom deserializers. Developers can create their own serialization modules by implementing interfaces for deserialization, thus enabling support for custom content types or proprietary data formats.

Installation

Install Kampute.HttpClient via NuGet:

dotnet add package Kampute.HttpClient

Usage Examples

The examples below demonstrate how to use the library for common tasks.

Basic Usage

To get started with HttpRestClient, simply instantiate it and use it to perform HTTP requests.

using Kampute.HttpClient;
using Kampute.HttpClient.Json;

// Create a new instance of the HttpRestClient
using var client = new HttpRestClient();

// Configure the client to accept JSON responses, using System.Text.Json library.
// This is an extension method provided by the Kampute.HttpClient.Json package.
client.AcceptJson();

// Perform a GET request.
// The GetAsync<TResponse> method will automatically deserialize the JSON response
// into the specified MyModel type.
var data = await client.GetAsync<MyModel>("https://api.example.com/resource");

Scoped Request Headers

In addition to setting default request headers that apply to all requests, you can also define headers for a specific set of requests using a scope. This allows temporary changes to headers that override the default settings within a defined context, which can be essential for handling varying endpoint requirements or testing scenarios.

The example below demonstrates how to temporarily override the Accept header for a series of requests, ensuring that all requests within the scope explicitly request a specific media type.

using Kampute.HttpClient;

// Create a new instance of the HttpRestClient.
using var client = new HttpRestClient();

// Begin a scoped block where the 'Accept' header is set to 'text/csv'.
// All HTTP requests within this using block will include this 'Accept' header.
using (client.BeginHeaderScope(new Dictionary<string, string> { ["Accept"] = MediaTypeNames.Text.Csv }))
{
    // Perform a GET request to retrieve data as CSV. The 'Accept' header for this request
    // will be 'text/csv', as specified by the scoped header.
    await client.GetAsStringAsync("https://api.example.com/resource/1/csv");

    // Perform another GET request within the same scope. The 'Accept' header remains
    // consistent with the scoped setting, ensuring both requests expect CSV responses.
    await client.GetAsStringAsync("https://api.example.com/resource/2/csv");
}

In addition to headers, it is also possible to scope request properties. This feature is particularly useful in scenarios where you need to maintain state or context-specific information temporarily during a series of HTTP operations. Scoped properties are managed similarly to scoped headers, enabling developers to define temporary data attached to requests that are automatically cleared once the scope is exited.

Custom Retry Strategies

The library offers various retry strategies to manage transient failures, ensuring your application remains resilient during network instability or temporary service unavailability. The example below demonstrates how to apply a Fibonacci backoff strategy, which gradually increases the delay between retries, balancing the need to retry soon against the need to wait longer as the number of attempts increases.

using Kampute.HttpClient;

// Create a new instance of the HttpRestClient
using var client = new HttpRestClient();

// Configure the client's retry mechanism.
// The Fibonacci strategy will retry up to 5 times 
// with an initial delay of 1 second between retries
// and delay increases following the Fibonacci sequence for subsequent retries.
client.BackoffStrategy = BackoffStrategies.Fibonacci(maxAttempts: 5, initialDelay: TimeSpan.FromSeconds(1));

Handling HTTP Errors

The library includes built-in handlers for managing common HTTP errors, streamlining the implementation of custom logic for error responses. Here's how to utilize the built-in '401 Unauthorized' error handler:

using Kampute.HttpClient;
using Kampute.HttpClient.ErrorHandlers;

// Create an instance of the built-in '401 Unauthorized' error handler.
// This handler defines the logic to handle unauthorized responses.
using var unauthorizedErrorHandler = new HttpError401Handler(async (client, challenges, cancellationToken) =>
{
    // In this example, we're handling the unauthorized error by making a POST request to an 
    // authentication endpoint to obtain a new authentication token.
    var auth = await client.PostAsFormAsync<AuthToken>("https://api.example.com/auth",
    [
        KeyValuePair.Create("client_id", MY_APP_ID),
        KeyValuePair.Create("client_secret", MY_APP_SECRET)
    ]);

    // Return a new AuthenticationHeaderValue with the obtained token.
    // This will be used to include the authentication header in subsequent requests.
    return new AuthenticationHeaderValue(AuthSchemes.Bearer, auth.Token);
});

// Create a new instance of the HttpRestClient
using var client = new HttpRestClient();

// Register the unauthorized error handler with the client.
// This allows the client to handle '401 Unauthorized' responses automatically.
client.ErrorHandlers.Add(unauthorizedErrorHandler);

Additionally, handling '503 Service Unavailable' and '429 Too Many Requests' errors is simplified with the built-in handler, ensuring your application can gracefully retry requests during service outages and rate limit encounters.

Handling Content Types

For handling specific content types like JSON or XML, consider using the available extension packages.

In the example below, we assume that both the Kampute.HttpClient.NewtonsoftJson package, which facilitates JSON content handling through the Newtonsoft.Json library, and the Kampute.HttpClient.DataContract package, enabling XML content management via DataContractSerializer, have been installed.

using Kampute.HttpClient;
using Kampute.HttpClient.NewtonsoftJson;
using Kampute.HttpClient.DataContract;

// Create a new instance of the HttpRestClient.
using var client = new HttpRestClient();

// Configure the client to accept JSON responses, using the Newtonsoft.Json library.
// This is an extension method provided by the Kampute.HttpClient.NewtonsoftJson package
client.AcceptJson();

// Configure the client to accept XML responses, using DataContractSerializer.
// This is an extension method provided by the Kampute.HttpClient.DataContract package
client.AcceptXml();

// Execute a GET request. The server may respond in either JSON or XML format.
// The GetAsync<TResponse> method will automatically deserialize the response
// into the specified MyResource type, based on the response content type (JSON or XML).
var result = await client.GetAsync<MyResource>("https://api.example.com/resource");

// Send a PATCH request with a payload in JSON format.
// The PatchAsJsonAsync method is provided by the Kampute.HttpClient.NewtonsoftJson package.
await client.PatchAsJsonAsync("https://api.example.com/resource", new { name = "new name" });

// Send a POST request with a payload in XML format.
// The PostAsXmlAsync method is provided by the Kampute.HttpClient.DataContract package.
var newResource = new MyResource();
await client.PostAsXmlAsync("https://api.example.com/resource", newResource);

Documentation

Explore the Kampute.HttpClient library's API Documentation for an in-depth understanding of its functionalities. You'll find detailed class references, method signatures, and descriptions of properties to guide your implementation and leverage the library's full potential.

Contributing

Contributions are welcomed! Please feel free to fork the repository, make changes, and submit pull requests. For major changes or new features, please open an issue first to discuss what you would like to change.

License

Kampute.HttpClient is licensed under the terms of the MIT license. See the LICENSE file for more details.

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 is compatible. 
.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.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Kampute.HttpClient:

Package Downloads
Kampute.HttpClient.NewtonsoftJson

This package is an extension package for Kampute.HttpClient, enhancing it to manage application/json content types, using Newtonsoft.Json library for serialization and deserialization of JSON responses and payloads.

Kampute.HttpClient.Xml

This package is an extension package for Kampute.HttpClient, enhancing it to manage application/xml content types, using XmlSerializer for serialization and deserialization of XML responses and payloads.

Kampute.HttpClient.DataContract

This package is an extension package for Kampute.HttpClient, enhancing it to manage application/xml content types, using DataContractSerializer for serialization and deserialization of XML responses and payloads.

Kampute.HttpClient.Json

This package is an extension package for Kampute.HttpClient, enhancing it to manage application/json content types, using System.Text.Json library for serialization and deserialization of JSON responses and payloads.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.1 298 7/27/2024
2.3.0 353 5/11/2024
2.2.1 279 5/8/2024
2.2.0 250 5/7/2024
2.1.1 285 5/6/2024
2.1.0 261 5/6/2024
2.0.0 279 5/5/2024
1.6.0 361 4/14/2024
1.5.0 284 4/2/2024
1.4.1 297 3/20/2024
1.4.0 297 3/19/2024
1.3.0 290 3/17/2024
1.2.0 285 3/16/2024
1.1.0 276 3/11/2024
1.0.0 297 3/5/2024

For detailed release notes, please visit https://github.com/kampute/http-client/releases