CSharpEssentials.HttpHelper 2.0.0

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

๐Ÿ”— CSharpEssentials.HttpHelper

A powerful and fluent HTTP helper for .NET built on top of HttpClientFactory, designed to simplify REST API calls with built-in support for retry policies, rate limiting, logging, timeouts, and dynamic configuration from appsettings.


๐Ÿš€ Features

โœ… Fully configurable via appsettings.httpHelper.json
โœ… Integrated with HttpClientFactory
โœ… Automatic retry with Polly
โœ… Rate Limiting using SlidingWindowRateLimiter
โœ… Support for FormUrlEncodedContent and JSON
โœ… Custom event-based logging
โœ… Fluent API: .addTimeout(), .addRetryCondition(), .addHeaders(), etc.


๐Ÿ› ๏ธ Setup

1. In Program.cs:

builder.Services.AddHttpClients(builder.Configuration);

2. Create or extend appsettings.httpHelper.json:

{
  "HttpClientOptions": [
    {
      "Name": "Test1",
      "RateLimitOptions": {
        "AutoReplenishment": true,
        "PermitLimit": 1,
        "QueueLimit": 0,
        "Window": "00:00:15",
        "SegmentsPerWindow": 1
      }
    }
  ]
}

๐Ÿงฉ Usage Example

var httpsClientHelper = (httpsClientHelper)factory.CreateOrGet("Test1");

var response = await httpsClientHelper
    .addTimeout(TimeSpan.FromSeconds(30))
    .AddRequestAction(async (req, res, retry, ts) => {
        Console.WriteLine($"[{req.Method}] {req.RequestUri} โ†’ {(int)res.StatusCode} | RETRY: {retry} | RL Wait: {ts}");
    })
    .addRetryCondition(
        res => res.StatusCode != HttpStatusCode.OK,
        retryCount: 3,
        backoffFactor: 2
    )
    .SendAsync(
        "https://example.com/api",
        HttpMethod.Get,
        null,
        new NoBodyContentBuilder()
    );

string body = await response.Content.ReadAsStringAsync();

๐Ÿ“ก Auto-generated Headers

Header Description
X-Retry-Attempt Number of retries attempted
X-RateLimit-TimeSpanElapsed Elapsed wait time due to rate limiter (if any)

โš™๏ธ Fluent Extensions

httpsClientHelper
    .addTimeout(TimeSpan.FromSeconds(15))
    .addHeaders("Authorization", "Bearer your-token")
    .addFormData(new List<KeyValuePair<string, string>> { ... })
    .addRetryCondition(...)
    .AddRequestAction(...);

๐Ÿงช Rate Limit Testing

To test rate limiting, trigger multiple concurrent calls using the same configured client (e.g., "Test1") and observe how the helper handles the cooldown period automatically using SlidingWindowRateLimiter.


๐Ÿงฐ Dynamic Registration

All HTTP clients are dynamically registered based on your appsettings.httpHelper.json configuration โ€” no code changes required to add more.


๐Ÿค Contributing

Pull requests, feedback, and improvements are welcome. This package is part of the CSharpEssentials ecosystem.


๐Ÿ“ฆ Requirements

  • .NET 8.0 or higher
  • NuGet Packages: Polly, Microsoft.Extensions.Http, Microsoft.Extensions.Options

๐Ÿ“„ 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 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 (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
2.0.2 108 6/14/2025
2.0.1 101 6/14/2025
2.0.0 104 6/14/2025
1.2.4 220 3/3/2025
1.2.3 99 3/2/2025
1.2.2 95 3/2/2025
1.2.0 94 3/2/2025
1.1.1 112 2/22/2025
1.1.0 109 2/22/2025
1.0.0 309 2/2/2025