2024-06-28 16:30:09 -05:00
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
|
|
|
|
|
namespace AnthropicClient.Models;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents headers included in Anthropic API responses.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AnthropicHeaders
|
|
|
|
|
{
|
|
|
|
|
private const string RequestIdHeaderKey = "request-id";
|
|
|
|
|
private const string RateLimitRequestsLimitHeaderKey = "anthropic-ratelimit-requests-limit";
|
|
|
|
|
private const string RateLimitRequestsRemainingHeaderKey = "anthropic-ratelimit-requests-remaining";
|
|
|
|
|
private const string RateLimitRequestsResetHeaderKey = "anthropic-ratelimit-requests-reset";
|
|
|
|
|
private const string RateLimitTokensLimitHeaderKey = "anthropic-ratelimit-tokens-limit";
|
|
|
|
|
private const string RateLimitTokensRemainingHeaderKey = "anthropic-ratelimit-tokens-remaining";
|
|
|
|
|
private const string RateLimitTokensResetHeaderKey = "anthropic-ratelimit-tokens-reset";
|
|
|
|
|
private const string RetryAfterHeaderKey = "retry-after";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the request ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string RequestId { get; init; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the rate limit requests limit.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int RateLimitRequestsLimit { get; init; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the rate limit requests remaining.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int RateLimitRequestsRemaining { get; init; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the time of the rate limit requests reset.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DateTimeOffset RateLimitRequestsReset { get; init; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the rate limit tokens limit.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int RateLimitTokensLimit { get; init; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the rate limit tokens remaining.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int RateLimitTokensRemaining { get; init; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the time of the rate limit tokens reset.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DateTimeOffset RateLimitTokensReset { get; init; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the retry after value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int RetryAfter { get; init; }
|
|
|
|
|
|
|
|
|
|
internal AnthropicHeaders()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="AnthropicHeaders"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="headers">The HTTP response headers.</param>
|
|
|
|
|
/// <returns>A new instance of the <see cref="AnthropicHeaders"/> class.</returns>
|
|
|
|
|
public AnthropicHeaders(HttpResponseHeaders headers)
|
|
|
|
|
{
|
2024-06-29 09:48:09 -05:00
|
|
|
RequestId = headers.TryGetValues(RequestIdHeaderKey, out var requestIdValues)
|
|
|
|
|
? requestIdValues.FirstOrDefault()
|
|
|
|
|
: string.Empty;
|
2024-07-01 22:46:02 -05:00
|
|
|
|
2024-06-29 09:48:09 -05:00
|
|
|
RateLimitRequestsLimit = int.Parse(
|
|
|
|
|
headers.TryGetValues(RateLimitRequestsLimitHeaderKey, out var requestLimitValues)
|
|
|
|
|
? requestLimitValues.FirstOrDefault()
|
|
|
|
|
: "0"
|
|
|
|
|
);
|
2024-07-01 22:46:02 -05:00
|
|
|
|
2024-06-29 09:48:09 -05:00
|
|
|
RateLimitRequestsRemaining = int.Parse(
|
|
|
|
|
headers.TryGetValues(RateLimitRequestsRemainingHeaderKey, out var remainingRequestsValues)
|
|
|
|
|
? remainingRequestsValues.FirstOrDefault()
|
|
|
|
|
: "0"
|
|
|
|
|
);
|
2024-07-01 22:46:02 -05:00
|
|
|
|
2024-06-29 09:48:09 -05:00
|
|
|
RateLimitRequestsReset = DateTimeOffset.Parse(
|
|
|
|
|
headers.TryGetValues(RateLimitRequestsResetHeaderKey, out var requestResetValues)
|
|
|
|
|
? requestResetValues.FirstOrDefault()
|
|
|
|
|
: DateTimeOffset.MinValue.ToString()
|
|
|
|
|
);
|
2024-07-01 22:46:02 -05:00
|
|
|
|
|
|
|
|
|
2024-06-29 09:48:09 -05:00
|
|
|
RateLimitTokensLimit = int.Parse(
|
|
|
|
|
headers.TryGetValues(RateLimitTokensLimitHeaderKey, out var limitValues)
|
|
|
|
|
? limitValues.FirstOrDefault()
|
|
|
|
|
: "0"
|
|
|
|
|
);
|
2024-07-01 22:46:02 -05:00
|
|
|
|
2024-06-29 09:48:09 -05:00
|
|
|
RateLimitTokensRemaining = int.Parse(
|
|
|
|
|
headers.TryGetValues(RateLimitTokensRemainingHeaderKey, out var remainingTokensValues)
|
|
|
|
|
? remainingTokensValues.FirstOrDefault()
|
|
|
|
|
: "0"
|
|
|
|
|
);
|
2024-07-01 22:46:02 -05:00
|
|
|
|
2024-06-29 09:48:09 -05:00
|
|
|
RateLimitTokensReset = DateTimeOffset.Parse(
|
2024-07-01 22:46:02 -05:00
|
|
|
headers.TryGetValues(RateLimitTokensResetHeaderKey, out var tokenResetValues)
|
|
|
|
|
? tokenResetValues.FirstOrDefault()
|
2024-06-29 09:48:09 -05:00
|
|
|
: DateTimeOffset.MinValue.ToString()
|
|
|
|
|
);
|
2024-07-01 22:46:02 -05:00
|
|
|
|
|
|
|
|
|
2024-06-29 09:48:09 -05:00
|
|
|
RetryAfter = int.Parse(
|
2024-07-01 22:46:02 -05:00
|
|
|
headers.TryGetValues(RetryAfterHeaderKey, out var retryValues)
|
|
|
|
|
? retryValues.FirstOrDefault()
|
2024-06-29 09:48:09 -05:00
|
|
|
: "0"
|
|
|
|
|
);
|
2024-06-28 16:30:09 -05:00
|
|
|
}
|
|
|
|
|
}
|