private async Task <TApi> getSingleObjectAsync <TApi, TJson>( HttpClient httpClient, IThrottler throttler, String endpointUri) where TJson : TApi { Queue <Exception> exceptions = new Queue <Exception>(); for (var attempts = 0; attempts < throttler.MaxAttempts; ++attempts) { throttler.WaitToProceed(); try { using (var stream = await httpClient.GetStreamAsync(endpointUri)) using (var reader = new JsonTextReader(new StreamReader(stream))) { var serializer = new JsonSerializer(); return(serializer.Deserialize <TJson>(reader)); } } catch (HttpRequestException ex) { exceptions.Enqueue(ex); } } throw new AggregateException(exceptions); }
private static async Task <TApi> callAndDeserializeSingleObjectAsync <TApi, TJson>( HttpClient httpClient, IThrottler throttler, HttpMethod method, Uri endpointUri, CancellationToken cancellationToken) where TJson : TApi { for (var attempts = 0; attempts < throttler.MaxRetryAttempts; ++attempts) { await throttler.WaitToProceed(cancellationToken).ConfigureAwait(false); using var request = new HttpRequestMessage(method, endpointUri); using var response = await httpClient .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) .ConfigureAwait(false); // Check response for server and caller specified waits and retries if (throttler.CheckHttpResponse(response)) { return(await response.DeserializeAsync <TApi, TJson>() .ConfigureAwait(false)); } } throw new RestClientErrorException( $"Unable to successfully call REST API endpoint `{endpointUri}` after {throttler.MaxRetryAttempts} attempts."); }
private async Task <TApi> callAndDeserializeSingleObjectAsync <TApi, TJson>( HttpClient httpClient, IThrottler throttler, Uri endpointUri, CancellationToken cancellationToken, HttpMethod method = null) where TJson : TApi { var exceptions = new Queue <Exception>(); for (var attempts = 0; attempts < throttler.MaxRetryAttempts; ++attempts) { await throttler.WaitToProceed(cancellationToken).ConfigureAwait(false); try { using (var request = new HttpRequestMessage(method ?? HttpMethod.Get, endpointUri)) using (var response = await httpClient .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) .ConfigureAwait(false)) { // Check response for server and caller specified waits and retries if (!throttler.CheckHttpResponse(response)) { continue; } return(await deserializeAsync <TApi, TJson>(response).ConfigureAwait(false)); } } catch (HttpRequestException ex) { if (attempts >= throttler.MaxRetryAttempts) { exceptions.Enqueue(ex); break; } else { continue; } } catch (Exception) { if (attempts < throttler.MaxRetryAttempts) { continue; } else { throw; } } } throw new AggregateException(exceptions); }
private async Task <TApi> getSingleObjectAsync <TApi, TJson>( HttpClient httpClient, IThrottler throttler, String endpointUri) where TJson : TApi { var exceptions = new Queue <Exception>(); for (var attempts = 0; attempts < throttler.MaxRetryAttempts; ++attempts) { await throttler.WaitToProceed(); try { using (var response = await httpClient.GetAsync(endpointUri, HttpCompletionOption.ResponseHeadersRead)) { // Check response for server and caller specified waits and retries if (!throttler.CheckHttpResponse(response)) { continue; } using (var stream = await response.Content.ReadAsStreamAsync()) using (var reader = new JsonTextReader(new StreamReader(stream))) { var serializer = new JsonSerializer(); if (response.IsSuccessStatusCode) { return(serializer.Deserialize <TJson>(reader)); } try { throw new RestClientErrorException( serializer.Deserialize <JsonError>(reader)); } catch (Exception exception) { throw new RestClientErrorException(response, exception); } } } } catch (HttpRequestException ex) { exceptions.Enqueue(ex); break; } } throw new AggregateException(exceptions); }