Class HttpResponseMessageExtensions
- Namespace
- RateLimitHeaders.Http
- Assembly
- RateLimitHeaders.dll
Extension methods for accessing rate limit information from HTTP responses.
public static class HttpResponseMessageExtensions
- Inheritance
-
HttpResponseMessageExtensions
- Inherited Members
Methods
GetRateLimitInfo(HttpResponseMessage)
Gets the parsed rate limit information from the response headers.
public static RateLimitInfo GetRateLimitInfo(this HttpResponseMessage response)
Parameters
responseHttpResponseMessageThe HTTP response message.
Returns
- RateLimitInfo
A RateLimitInfo with parsed values. Check IsValid to determine if parsing was successful.
Examples
var response = await httpClient.GetAsync("/api/resource");
var info = response.GetRateLimitInfo();
if (info.IsValid)
{
Console.WriteLine($"Remaining: {info.Remaining}/{info.Quota}");
}
Remarks
This method parses the response headers on each call. For repeated access, consider using TryGetRateLimitInfo(HttpResponseMessage, out RateLimitInfo) and caching the result.
TryGetRateLimitInfo(HttpResponseMessage, out RateLimitInfo)
Attempts to get the parsed rate limit information from the response headers.
public static bool TryGetRateLimitInfo(this HttpResponseMessage response, out RateLimitInfo rateLimitInfo)
Parameters
responseHttpResponseMessageThe HTTP response message.
rateLimitInfoRateLimitInfoWhen this method returns
true, contains the parsed rate limit information.
Returns
- bool
trueif rate limit headers were successfully parsed; otherwise,false.
Examples
var response = await httpClient.GetAsync("/api/resource");
if (response.TryGetRateLimitInfo(out var info))
{
Console.WriteLine($"Policy: {info.PolicyName}");
Console.WriteLine($"Remaining: {info.Remaining}/{info.Quota}");
if (info.IsQuotaLow(0.1))
{
Console.WriteLine("Warning: Quota is low!");
}
}
TryGetStoredRateLimitInfo(HttpResponseMessage, out RateLimitInfo)
Gets the rate limit info that was stored in the request's options during response processing.
public static bool TryGetStoredRateLimitInfo(this HttpResponseMessage response, out RateLimitInfo rateLimitInfo)
Parameters
responseHttpResponseMessageThe HTTP response message.
rateLimitInfoRateLimitInfoWhen this method returns
true, contains the rate limit info that was stored during response processing.
Returns
- bool
trueif rate limit info was found in the request options; otherwise,false.
Examples
// When using RateLimitAwareHandler, rate limit info is automatically stored
var response = await httpClient.GetAsync("/api/resource");
if (response.TryGetStoredRateLimitInfo(out var info))
{
Console.WriteLine($"Cached info: {info.Remaining}/{info.Quota}");
}
Remarks
This retrieves rate limit info that was stored by the RateLimitAwareHandler during response processing. This is useful when the response object has been passed through multiple handlers and you want to access the rate limit info without re-parsing.