private async static Task <HttpResponseMessage> AttemptRequestAync(IronSharpConfig sharpConfig, HttpRequestMessage request, int attempt = 0) { if (attempt > HttpClientOptions.RetryLimit) { throw new MaximumRetryAttemptsExceededException(request, HttpClientOptions.RetryLimit); } ILog logger = LogManager.GetLogger <RestClient>(); using (var client = CreateHttpClient()) { if (logger.IsDebugEnabled) { using (var sw = new StringWriter()) { sw.WriteLine("{0} {1}", request.Method, request.RequestUri); if (request.Content != null) { sw.WriteLine(await request.Content.ReadAsStringAsync()); } logger.Debug(sw.ToString()); } } HttpResponseMessage response = await client.SendAsync(request); if (logger.IsDebugEnabled) { if (response.Content != null) { logger.Debug(await response.Content.ReadAsStringAsync()); } } if (response.IsSuccessStatusCode) { return(response); } if (HttpClientOptions.EnableRetry && RestUtility.IsRetriableStatusCode(response)) { attempt++; return(await ExponentialBackoff.Sleep(sharpConfig.BackoffFactor, attempt). ContinueWith(task => AttemptRequestAync(sharpConfig, request, attempt)). Unwrap()); } return(response); } }
private HttpResponseMessage AttemptRequest(IronSharpConfig sharpConfig, HttpRequestMessageBuilder requestBuilder, int attempt = 0) { var request = requestBuilder.Build(); if (attempt > HttpClientOptions.RetryLimit) { throw new MaximumRetryAttemptsExceededException(request, HttpClientOptions.RetryLimit); } ILog logger = LogManager.GetLogger <RestClient>(); if (logger.IsDebugEnabled) { using (var sw = new StringWriter()) { sw.WriteLine("{0} {1}", request.Method, request.RequestUri); if (request.Content != null) { sw.WriteLine(request.Content.ReadAsStringAsync().Result); } logger.Debug(sw.ToString()); } } HttpResponseMessage response = httpClient.SendAsync(request).Result; if (logger.IsDebugEnabled) { if (response.Content != null) { logger.Debug(response.Content.ReadAsStringAsync().Result); } } if (response.IsSuccessStatusCode) { return(response); } if (HttpClientOptions.EnableRetry && IsRetriableStatusCode(response)) { attempt++; ExponentialBackoff.Sleep(sharpConfig.BackoffFactor, attempt); return(AttemptRequest(sharpConfig, requestBuilder, attempt)); } return(response); }
public RestResponse <T> Post <T>(IronClientConfig config, string endPoint, object payload = null, NameValueCollection query = null) where T : class { var requestBuilder = new HttpRequestMessageBuilder(config, this, new RestClientRequest { EndPoint = endPoint, Query = query, Method = HttpMethod.Post }); IronSharpConfig sharpConfig = config.SharpConfig; if (payload != null) { requestBuilder.Payload = payload; } return(new RestResponse <T>(AttemptRequest(sharpConfig, requestBuilder))); }
public static async Task <RestResponse <T> > Put <T>(IronClientConfig config, string endPoint, object payload, NameValueCollection query = null) where T : class { HttpRequestMessage request = RestUtility.BuildIronRequest(config, new RestClientRequest { EndPoint = endPoint, Query = query, Method = HttpMethod.Put }); IronSharpConfig sharpConfig = config.SharpConfig; if (payload != null) { request.Content = new JsonContent(payload); } return(new RestResponse <T>(await AttemptRequestAync(sharpConfig, request))); }