-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ public class BaseClient : HttpClient | |
private readonly object _lock = new(); | ||
private readonly ILogger _logger; | ||
private readonly ISleepService _sleepService; | ||
private const int RateLimitAttempts = 50; | ||
Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs GitHub Actions / build
Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs GitHub Actions / build
Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs GitHub Actions / call / test
Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs GitHub Actions / call / test
Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs GitHub Actions / call / test
Check warning on line 36 in src/Jellyfin.Plugin.ListenBrainz.Api/BaseClient.cs GitHub Actions / call / test
|
||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BaseClient"/> class. | ||
|
@@ -100,11 +101,12 @@ protected BaseClient(IHttpClientFactory httpClientFactory, ILogger logger, ISlee | |
private async Task<TResponse?> DoRequest<TResponse>(HttpRequestMessage requestMessage, CancellationToken cancellationToken) | ||
where TResponse : IListenBrainzResponse | ||
{ | ||
HttpResponseMessage response; | ||
// Compiler complains that the variable won't be initialized in the loop, so here we go | ||
HttpResponseMessage? response = null; | ||
try | ||
{ | ||
Monitor.Enter(_lock); | ||
while (true) | ||
for (int i = 0; i < RateLimitAttempts; i++) | ||
{ | ||
response = await SendRequest(requestMessage, cancellationToken); | ||
if (response.StatusCode == HttpStatusCode.TooManyRequests) | ||
|
@@ -123,6 +125,8 @@ protected BaseClient(IHttpClientFactory httpClientFactory, ILogger logger, ISlee | |
Monitor.Exit(_lock); | ||
} | ||
|
||
if (response is null) throw new InvalidResponseException("Response is NULL"); | ||
|
||
var stringContent = await response.Content.ReadAsStringAsync(cancellationToken); | ||
var result = JsonConvert.DeserializeObject<TResponse>(stringContent, SerializerSettings); | ||
if (result is null) throw new InvalidResponseException("Response deserialized to NULL"); | ||
|