|
| 1 | +using System.Net.Http.Headers; |
| 2 | +using RobinTTY.NordigenApiClient.Endpoints; |
| 3 | + |
| 4 | +namespace RobinTTY.NordigenApiClient.Models.Responses; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// The rate limits of the GoCardless API. |
| 8 | +/// </summary> |
| 9 | +public class ApiRateLimits |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Indicates the maximum number of allowed requests within the defined time window. |
| 13 | + /// Usually populated in every response. |
| 14 | + /// </summary> |
| 15 | + public int? RequestLimit { get; set; } |
| 16 | + /// <summary> |
| 17 | + /// Indicates the number of remaining requests you can make in the current time window. |
| 18 | + /// Usually populated in every response. |
| 19 | + /// </summary> |
| 20 | + public int? RemainingRequests { get; set; } |
| 21 | + /// <summary> |
| 22 | + /// Indicates the time remaining in the current time window (in seconds). |
| 23 | + /// Usually populated in every response. |
| 24 | + /// </summary> |
| 25 | + public int? RemainingSecondsInTimeWindow { get; set; } |
| 26 | + /// <summary> |
| 27 | + /// Indicates the maximum number of allowed requests to the <see cref="AccountsEndpoint"/> |
| 28 | + /// within the defined time window. Only populated in responses from the <see cref="AccountsEndpoint"/>. |
| 29 | + /// </summary> |
| 30 | + public int? RequestLimitAccountsEndpoint { get; set; } |
| 31 | + /// <summary> |
| 32 | + /// Indicates the number of remaining requests to the <see cref="AccountsEndpoint"/> |
| 33 | + /// you can make in the current time window. Only populated in responses from the <see cref="AccountsEndpoint"/>. |
| 34 | + /// </summary> |
| 35 | + public int? RemainingRequestsAccountsEndpoint { get; set; } |
| 36 | + /// <summary> |
| 37 | + /// Indicates the time remaining in the current time window (in seconds) for requests |
| 38 | + /// to the <see cref="AccountsEndpoint"/>. Only populated in responses from the <see cref="AccountsEndpoint"/>. |
| 39 | + /// </summary> |
| 40 | + public int? RemainingSecondsInTimeWindowAccountsEndpoint { get; set; } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Creates a new instance of <see cref="ApiRateLimits" />. |
| 44 | + /// </summary> |
| 45 | + /// <param name="requestLimit">Indicates the maximum number of allowed requests within the defined time window.</param> |
| 46 | + /// <param name="remainingRequests">Indicates the number of remaining requests you can make in the current time window.</param> |
| 47 | + /// <param name="remainingSecondsInTimeWindow">Indicates the time remaining in the current time window (in seconds).</param> |
| 48 | + /// <param name="requestLimitAccountsEndpoint">Indicates the maximum number of allowed requests to the <see cref="AccountsEndpoint"/> |
| 49 | + /// within the defined time window.</param> |
| 50 | + /// <param name="remainingRequestsAccountsEndpoint">Indicates the number of remaining requests to the <see cref="AccountsEndpoint"/> |
| 51 | + /// you can make in the current time window.</param> |
| 52 | + /// <param name="remainingSecondsInTimeWindowAccountsEndpoint">Indicates the time remaining in the current time window (in seconds) for requests |
| 53 | + /// to the <see cref="AccountsEndpoint"/>.</param> |
| 54 | + public ApiRateLimits(int? requestLimit, int? remainingRequests, int? remainingSecondsInTimeWindow, int? requestLimitAccountsEndpoint, |
| 55 | + int? remainingRequestsAccountsEndpoint, int? remainingSecondsInTimeWindowAccountsEndpoint) |
| 56 | + { |
| 57 | + RequestLimit = requestLimit; |
| 58 | + RemainingRequests = remainingRequests; |
| 59 | + RemainingSecondsInTimeWindow = remainingSecondsInTimeWindow; |
| 60 | + RequestLimitAccountsEndpoint = requestLimitAccountsEndpoint; |
| 61 | + RemainingRequestsAccountsEndpoint = remainingRequestsAccountsEndpoint; |
| 62 | + RemainingSecondsInTimeWindowAccountsEndpoint = remainingSecondsInTimeWindowAccountsEndpoint; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Creates a new instance of <see cref="ApiRateLimits" />. |
| 67 | + /// </summary> |
| 68 | + /// <param name="headers">The headers of the HTTP response containing the rate limit information.</param> |
| 69 | + public ApiRateLimits(HttpHeaders headers) |
| 70 | + { |
| 71 | + RequestLimit = TryParseApiLimit(headers, "HTTP_X_RATELIMIT_LIMIT"); |
| 72 | + RemainingRequests = TryParseApiLimit(headers, "HTTP_X_RATELIMIT_REMAINING"); |
| 73 | + RemainingSecondsInTimeWindow = TryParseApiLimit(headers, "HTTP_X_RATELIMIT_RESET"); |
| 74 | + RequestLimitAccountsEndpoint = TryParseApiLimit(headers, "HTTP_X_RATELIMIT_ACCOUNT_SUCCESS_LIMIT"); |
| 75 | + RemainingRequestsAccountsEndpoint = TryParseApiLimit(headers, "HTTP_X_RATELIMIT_ACCOUNT_SUCCESS_REMAINING"); |
| 76 | + RemainingSecondsInTimeWindowAccountsEndpoint = TryParseApiLimit(headers, "HTTP_X_RATELIMIT_ACCOUNT_SUCCESS_RESET"); |
| 77 | + } |
| 78 | + |
| 79 | + private static int? TryParseApiLimit(HttpHeaders headers, string headerName) |
| 80 | + { |
| 81 | + headers.TryGetValues(headerName, out var values); |
| 82 | + var firstHeaderValue = values?.FirstOrDefault(); |
| 83 | + var parseSuccess = int.TryParse(firstHeaderValue, out var limitValue); |
| 84 | + |
| 85 | + return parseSuccess ? limitValue : null; |
| 86 | + } |
| 87 | +} |
0 commit comments