CSharpEssentials.HttpHelper
2.0.0
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
<PackageReference Include="CSharpEssentials.HttpHelper" Version="2.0.0" />
<PackageVersion Include="CSharpEssentials.HttpHelper" Version="2.0.0" />
<PackageReference Include="CSharpEssentials.HttpHelper" />
paket add CSharpEssentials.HttpHelper --version 2.0.0
#r "nuget: CSharpEssentials.HttpHelper, 2.0.0"
#addin nuget:?package=CSharpEssentials.HttpHelper&version=2.0.0
#tool nuget:?package=CSharpEssentials.HttpHelper&version=2.0.0
๐ 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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.Extensions.Configuration.Json (>= 9.0.2)
- Microsoft.Extensions.Http (>= 9.0.1)
- Polly (>= 8.5.2)
- System.Threading.RateLimiting (>= 9.0.1)
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.