CSharpEssentials.HttpHelper 2.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package CSharpEssentials.HttpHelper --version 2.0.5
                    
NuGet\Install-Package CSharpEssentials.HttpHelper -Version 2.0.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="CSharpEssentials.HttpHelper" Version="2.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.HttpHelper" Version="2.0.5" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.HttpHelper" />
                    
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 CSharpEssentials.HttpHelper --version 2.0.5
                    
#r "nuget: CSharpEssentials.HttpHelper, 2.0.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.
#:package CSharpEssentials.HttpHelper@2.0.5
                    
#: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=CSharpEssentials.HttpHelper&version=2.0.5
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.HttpHelper&version=2.0.5
                    
Install as a Cake Tool

๐Ÿ”— CSharpEssentials.HttpHelper

A robust and extensible HTTP helper designed for modern .NET applications that require:

  • HTTP Client pooling (via HttpClientFactory)
  • Rate limiting
  • Retry with Polly
  • Fully testable, SOLID-friendly structure
  • Plug & play integration via IhttpsClientHelperFactory

๐Ÿš€ Setup

Install the package and configure via:

services.AddHttpClients(Configuration);

Optionally provide an external configuration file:

appsettings.httpHelper.json

Sample:

{
  "HttpClientOptions": [
    {
      "Name": "GitHubClient",
      "RateLimitOptions": {
        "PermitLimit": 5,
        "QueueLimit": 10,
        "Window": "00:00:10",
        "SegmentsPerWindow": 1,
        "AutoReplenishment": true
      }
    }
  ]
}

๐Ÿ”ง Usage

Initialize the client:

var httpHelper = httpFactory
    .CreateOrGet("GitHubClient")
    .AddRequestAction((req, res, retry, ts) => {
        Console.WriteLine($"[Retry: {retry}] {req.RequestUri}");
        return Task.CompletedTask;
    });

Add headers and authentication:

httpHelper.setHeadersAndBearerAuthentication(
    new Dictionary<string, string> { { "User-Agent", "MyGitHubApp" } },
    new httpsClientHelper.httpClientAuthenticationBearer("your_github_token"));

Send request (GET / POST / PUT / DELETE):

IContentBuilder contentBuilder = new NoBodyContentBuilder(); // or JsonContentBuilder
HttpResponseMessage response = await httpHelper.SendAsync(
    url: "https://api.github.com/search/repositories?q=recap+in:name",
    httpMethod: HttpMethod.Get,
    body: null,
    contentBuilder: contentBuilder
);

๐Ÿ”„ Retry Handling

httpHelper.addRetryCondition(
    response => !response.IsSuccessStatusCode, retryCount: 3, backoffFactor: 2.0
);

๐Ÿงช GitHub API Usage Example

Minimal API Endpoint (search repos):

app.MapGet("/repos/search", async (
    [FromQuery] string Pattern,
    [FromServices] IhttpsClientHelperFactory httpFactory) => {

    var httpHelper = httpFactory.CreateOrGet("GitHubClient")
        .AddRequestAction((req, res, retry, ts) => {
            Console.WriteLine($"Executed request to {req.RequestUri}");
            return Task.CompletedTask;
        });

    httpHelper.setHeadersAndBearerAuthentication(
        new Dictionary<string, string> { { "User-Agent", "MyGitHubApp" } },
        new httpsClientHelper.httpClientAuthenticationBearer("your_github_token"));

    var url = $"https://api.github.com/search/repositories?q={Pattern}+in:name&per_page=10";

    var response = await httpHelper.SendAsync(url, HttpMethod.Get, null, new NoBodyContentBuilder());
    var json = await response.Content.ReadAsStringAsync();

    using var doc = JsonDocument.Parse(json);
    var repos = doc.RootElement
        .GetProperty("items")
        .EnumerateArray()
        .Select(repo => new {
            Name = repo.GetProperty("full_name").GetString(),
            Url = repo.GetProperty("html_url").GetString(),
            Description = repo.TryGetProperty("description", out var descProp) && descProp.ValueKind != JsonValueKind.Null
                ? descProp.GetString()
                : "(no description)"
        })
        .ToList();

    return Results.Ok(repos);
});

๐Ÿ“ฆ Supported Content Builders

  • JsonContentBuilder โ†’ for application/json
  • FormUrlEncodedContentBuilder โ†’ for form data
  • XmlContentBuilder โ†’ for application/xml
  • NoBodyContentBuilder โ†’ for GET / DELETE

๐Ÿ“ˆ Built-in Features

Feature Description
Retry Polly-based retry with exponential backoff
Rate Limiting Sliding window limiter per client instance
Headers/Auth Bearer / Basic / Custom headers
Logging Handler Custom DelegatingHandler logs all requests
Retry Info Injects X-Retry-Attempt and duration

๐Ÿ“ Folder Structure

  • httpsClientHelper.cs โ€“ main engine
  • httpsClientHelperFactory.cs โ€“ factory + DI integration
  • HttpRequestBuilder.cs โ€“ fluent builder pattern
  • IContentBuilder.cs โ€“ pluggable request body strategies
  • HttpClientHandlerLogging.cs โ€“ optional delegating handler
  • httpClientOptions.cs โ€“ config-based client tuning

๐Ÿค Contributing

Pull requests are welcome. Please make sure to run unit tests and respect project structure before submitting.


๐Ÿ“œ License

MIT

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 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on CSharpEssentials.HttpHelper:

Package Downloads
CSharpEssentials.HangFireHelper

A library to help you to use HangFire

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.2 15 9/12/2025
4.0.0 73 9/10/2025
3.0.1 69 9/10/2025
2.0.5 98 9/7/2025
2.0.4 90 9/7/2025
2.0.3 92 9/7/2025
2.0.2 191 6/14/2025
2.0.1 149 6/14/2025
2.0.0 148 6/14/2025
1.2.4 252 3/3/2025
1.2.3 119 3/2/2025
1.2.2 112 3/2/2025
1.2.0 112 3/2/2025
1.1.1 130 2/22/2025
1.1.0 123 2/22/2025
1.0.0 339 2/2/2025