Make rate limit parser more resilient

This commit is contained in:
Ske 2021-02-08 16:30:18 +01:00
parent ef9b69a997
commit 18cf863834

View File

@ -13,22 +13,27 @@ namespace Myriad.Rest.Ratelimit
ServerDate = response.Headers.Date; ServerDate = response.Headers.Date;
if (response.Headers.TryGetValues("X-RateLimit-Limit", out var limit)) if (response.Headers.TryGetValues("X-RateLimit-Limit", out var limit))
Limit = int.Parse(limit!.First()); if (int.TryParse(limit.First(), out var limitNum))
Limit = limitNum;
if (response.Headers.TryGetValues("X-RateLimit-Remaining", out var remaining)) if (response.Headers.TryGetValues("X-RateLimit-Remaining", out var remaining))
Remaining = int.Parse(remaining!.First()); if (int.TryParse(remaining!.First(), out var remainingNum))
Remaining = remainingNum;
if (response.Headers.TryGetValues("X-RateLimit-Reset", out var reset)) if (response.Headers.TryGetValues("X-RateLimit-Reset", out var reset))
Reset = DateTimeOffset.FromUnixTimeMilliseconds((long) (double.Parse(reset!.First()) * 1000)); if (double.TryParse(reset!.First(), out var resetNum))
Reset = DateTimeOffset.FromUnixTimeMilliseconds((long) (resetNum * 1000));
if (response.Headers.TryGetValues("X-RateLimit-Reset-After", out var resetAfter)) if (response.Headers.TryGetValues("X-RateLimit-Reset-After", out var resetAfter))
ResetAfter = TimeSpan.FromSeconds(double.Parse(resetAfter!.First())); if (double.TryParse(resetAfter!.First(), out var resetAfterNum))
ResetAfter = TimeSpan.FromSeconds(resetAfterNum);
if (response.Headers.TryGetValues("X-RateLimit-Bucket", out var bucket)) if (response.Headers.TryGetValues("X-RateLimit-Bucket", out var bucket))
Bucket = bucket.First(); Bucket = bucket.First();
if (response.Headers.TryGetValues("X-RateLimit-Global", out var global)) if (response.Headers.TryGetValues("X-RateLimit-Global", out var global))
Global = bool.Parse(global!.First()); if (bool.TryParse(global!.First(), out var globalBool))
Global = globalBool;
} }
public bool Global { get; init; } public bool Global { get; init; }