private async Task <RestResponse> SendInternalAsync(string method, string endpoint, RequestOptions options) { RestResponse response = null; bool retry = true; while (retry) { try { response = await Config.RestClient.SendAsync(method, endpoint, options.CancellationToken).TimeoutAfter(options.Timeout); } catch (TimeoutException) when(options.RetryMode == RetryMode.RetryTimeouts) { continue; } if (response.Status == HttpStatusCode.BadGateway && !options.RetryMode.HasFlag(RetryMode.Retry502)) { throw await GfycatException.CreateFromResponseAsync(response); } else { if (!response.Status.IsSuccessfulStatus() && !(options.IgnoreCodes?.Any(code => code == response.Status) ?? false)) { throw await GfycatException.CreateFromResponseAsync(response); } retry = false; } } return(response); }
internal static async Task <T> ReadAsJsonAsync <T>(this RestResponse response, GfycatClientConfig config) { string responseContentAsString = await response.ReadAsStringAsync().ConfigureAwait(false); if (GfycatException.ContainsError(responseContentAsString)) { throw await GfycatException.CreateFromResponseAsync(response, responseContentAsString).ConfigureAwait(false); } T resultObject = JsonConvert.DeserializeObject <T>(responseContentAsString, config.SerializerSettings); return(resultObject); }
private async Task <RestResponse> SendInternalAsync(Func <Task <RestResponse> > restFunction, RequestOptions options) { RestResponse response = null; Config.RestClient.SetHeader("Authorization", options.UseAccessToken ? $"Bearer {Client.AccessToken}" : null); bool retry = true, first401 = true; while (retry) { try // create the task and run it { Task <RestResponse> task = restFunction(); response = await task.TimeoutAfter(options.Timeout).ConfigureAwait(false); } catch (TimeoutException) when(options.RetryMode.HasFlag(RetryMode.RetryTimeouts)) // catch the timeout if specified, then try again by continuing { continue; } if (response.Status == HttpStatusCode.BadGateway && !options.RetryMode.HasFlag(RetryMode.Retry502)) // if there was a bad gateway and we don't retry them, throw { throw await GfycatException.CreateFromResponseAsync(response).ConfigureAwait(false); } else if (response.Status == HttpStatusCode.Unauthorized) { if (options.UseAccessToken && (first401 && options.RetryMode.HasFlag(RetryMode.RetryFirst401))) // make sure we don't get in a refresh loop due to not having an access token when using an invalid client ID { await Client.RefreshTokenAsync().ConfigureAwait(false); Config.RestClient.SetHeader("Authorization", options.UseAccessToken ? $"Bearer {Client.AccessToken}" : null); first401 = false; continue; } else { throw await GfycatException.CreateFromResponseAsync(response).ConfigureAwait(false); } } else { if (!response.Status.IsSuccessfulStatus() && !(options.IgnoreCodes?.Any(code => code == response.Status) ?? false)) // make sure the status, if an error, isn't ignored { throw await GfycatException.CreateFromResponseAsync(response).ConfigureAwait(false); } retry = false; } } return(response); }