2021-08-27 15:03:47 +00:00
|
|
|
using System;
|
2021-02-08 18:53:06 +00:00
|
|
|
using System.Globalization;
|
2020-12-22 12:15:26 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
|
|
namespace Myriad.Rest.Ratelimit
|
|
|
|
{
|
|
|
|
public record RatelimitHeaders
|
|
|
|
{
|
2021-02-08 18:53:06 +00:00
|
|
|
private const string LimitHeader = "X-RateLimit-Limit";
|
|
|
|
private const string RemainingHeader = "X-RateLimit-Remaining";
|
|
|
|
private const string ResetHeader = "X-RateLimit-Reset";
|
|
|
|
private const string ResetAfterHeader = "X-RateLimit-Reset-After";
|
|
|
|
private const string BucketHeader = "X-RateLimit-Bucket";
|
|
|
|
private const string GlobalHeader = "X-RateLimit-Global";
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
public bool Global { get; private set; }
|
|
|
|
public int? Limit { get; private set; }
|
|
|
|
public int? Remaining { get; private set; }
|
|
|
|
public DateTimeOffset? Reset { get; private set; }
|
|
|
|
public TimeSpan? ResetAfter { get; private set; }
|
|
|
|
public string? Bucket { get; private set; }
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
public DateTimeOffset? ServerDate { get; private set; }
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
public bool HasRatelimitInfo =>
|
|
|
|
Limit != null && Remaining != null && Reset != null && ResetAfter != null && Bucket != null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
public RatelimitHeaders() { }
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
public static RatelimitHeaders Parse(HttpResponseMessage response)
|
|
|
|
{
|
|
|
|
var headers = new RatelimitHeaders
|
|
|
|
{
|
|
|
|
ServerDate = response.Headers.Date,
|
|
|
|
Limit = TryGetInt(response, LimitHeader),
|
|
|
|
Remaining = TryGetInt(response, RemainingHeader),
|
|
|
|
Bucket = TryGetHeader(response, BucketHeader)
|
|
|
|
};
|
2020-12-22 12:15:26 +00:00
|
|
|
|
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
var resetTimestamp = TryGetDouble(response, ResetHeader);
|
|
|
|
if (resetTimestamp != null)
|
2021-08-27 15:03:47 +00:00
|
|
|
headers.Reset = DateTimeOffset.FromUnixTimeMilliseconds((long)(resetTimestamp.Value * 1000));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
var resetAfterSeconds = TryGetDouble(response, ResetAfterHeader);
|
|
|
|
if (resetAfterSeconds != null)
|
|
|
|
headers.ResetAfter = TimeSpan.FromSeconds(resetAfterSeconds.Value);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
var global = TryGetHeader(response, GlobalHeader);
|
|
|
|
if (global != null && bool.TryParse(global, out var globalBool))
|
|
|
|
headers.Global = globalBool;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
return headers;
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
private static string? TryGetHeader(HttpResponseMessage response, string headerName)
|
|
|
|
{
|
|
|
|
if (!response.Headers.TryGetValues(headerName, out var values))
|
|
|
|
return null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
return values.FirstOrDefault();
|
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
private static int? TryGetInt(HttpResponseMessage response, string headerName)
|
|
|
|
{
|
|
|
|
var valueString = TryGetHeader(response, headerName);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
if (!int.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
|
|
|
|
return null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
return value;
|
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
private static double? TryGetDouble(HttpResponseMessage response, string headerName)
|
|
|
|
{
|
|
|
|
var valueString = TryGetHeader(response, headerName);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
if (!double.TryParse(valueString, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
|
|
|
|
return null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-02-08 18:53:06 +00:00
|
|
|
return value;
|
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|
|
|
|
}
|