SmartHttpClient 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SmartHttpClient --version 1.0.0
                    
NuGet\Install-Package SmartHttpClient -Version 1.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="SmartHttpClient" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SmartHttpClient" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SmartHttpClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SmartHttpClient --version 1.0.0
                    
#r "nuget: SmartHttpClient, 1.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.
#:package SmartHttpClient@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SmartHttpClient&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SmartHttpClient&version=1.0.0
                    
Install as a Cake Tool

SmartHttpClient

SmartHttpClient is a powerful library for .NET 8.0 that simplifies working with HttpClient. It abstracts away repetitive tasks like creating new HttpClient instances, handling JSON serialization/deserialization, and managing headers, authentication, and timeouts.

With SmartHttpClient, developers no longer need to manually handle JSON (it does it automatically for you). The library is flexible and supports both strongly typed (T) and non-typed requests.


Features

  • Simplifies HttpClient usage.
  • Automatically serializes and deserializes JSON requests and responses.
  • Supports dependency injection for easy integration.
  • Includes built-in support for authentication (e.g., Bearer tokens).
  • Handles headers, request timeouts, and endpoint parameters effortlessly.
  • Flexible API to support both strongly-typed (T) and non-typed calls.

Installation

Install the package via NuGet:

dotnet add package SmartHttpClient --version 1.0.0

Setup

1. Register Services

Add the following services in your Program.cs file to enable dependency injection for SmartHttpClient:

builder.Services.AddHttpClient();
builder.Services.AddTransient<IHTTPClientWrapper, HTTPClientWrapper>();

2. Dependency Injection Example

To use SmartHttpClient, inject IHTTPClientWrapper into your services, controllers, or classes:

namespace HTTPClientService.Library;

/// <summary>
/// Represents a service for sending HTTP requests.
/// </summary>
public class MyService
{
    private readonly IHTTPClientWrapper _httpClientWrapper;

    public MyService(IHTTPClientWrapper httpClientWrapper)
    {
        _httpClientWrapper = httpClientWrapper;
    }

    /// <summary>
    /// Sends an HTTP request asynchronously and returns the response.
    /// </summary>
    /// <typeparam name="T">The type of the response.</typeparam>
    /// <param name="requestParam">The parameters for the endpoint.</param>
    /// <returns>The task representing the asynchronous operation, containing the response.</returns>
    public async Task<T> SendRequestAsync<T>(object requestParam) where T : class
    {
        var request = new HTTPClientRequest
        {
            BaseUri = new Uri("https://localhost:7081/api/Application"),
            Method = HttpMethod.Get,
            RequestBody = new { Id = 1 },
            EndpointParams = requestParam,
            Timeout = TimeSpan.FromSeconds(5),
            Authenticator = new Authenticator
            {
                AccessToken = "TOKEN-ABC1234",
                Auth = AuthenticationMethod.Bearer
            },
            Headers = new Dictionary<string, string> { { "api-version", "2" } }
        };

        return await _httpClientWrapper.SendAsync<T>(request);
    }
}

Usage Examples

1. Strongly-Typed Request (T)

If you expect a specific response type, like a custom class MyResponseModel, you can use the generic T parameter to deserialize the response automatically:

var requestParams = new { Name = "Test", Value = 42 };
var result = await _httpClientWrapper.SendAsync<MyResponseModel>(new HTTPClientRequest
{
    BaseUri = new Uri("https://api.example.com"),
    Method = HttpMethod.Post,
    RequestBody = requestParams,
    Headers = new Dictionary<string, string>
    {
        { "Authorization", "Bearer your-access-token" }
    }
});

// `result` is automatically deserialized into the MyResponseModel class.
Console.WriteLine(result.PropertyName);

2. Non-Typed Request (No T)

If you don't want to use a specific type for the response, you can retrieve the raw response as a string or a dynamic object:

var result = await _httpClientWrapper.SendAsync<dynamic>(new HTTPClientRequest
{
    BaseUri = new Uri("https://api.example.com"),
    Method = HttpMethod.Get,
    Headers = new Dictionary<string, string>
    {
        { "api-version", "1.0" }
    }
});

// Access the dynamic response.
Console.WriteLine(result.SomeProperty);

3. Request Without Returning a Response

If you don’t need a response or just want to send a request, you can use void:

await _httpClientWrapper.SendAsync(new HTTPClientRequest
{
    BaseUri = new Uri("https://api.example.com"),
    Method = HttpMethod.Delete,
    Headers = new Dictionary<string, string>
    {
        { "Authorization", "Bearer your-access-token" }
    }
});

Dependencies

  • Microsoft.Extensions.Http (>= 8.0.1)

Contributing

We welcome contributions! Please feel free to:


License

This project is licensed under the MIT License. See the LICENSE file for details.


Why Use SmartHttpClient?

With SmartHttpClient, you no longer need to:

  • Create and manage HttpClient instances.
  • Write repetitive boilerplate for JSON serialization/deserialization.
  • Manually handle authentication and headers.

SmartHttpClient is built to save developers time and effort, making HTTP requests clean, efficient, and easy to manage.


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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
1.0.4 105 1/17/2025
1.0.3 99 1/16/2025
1.0.2 93 1/16/2025
1.0.1 111 1/16/2025
1.0.0 98 1/16/2025

- Added new methods for HTTP GET and POST.
- Fixed bugs with retry policies.
- Improved error handling and logging.