Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        public static Task <HttpResponseMessage> Execute(IronClientConfig config, IRestClientRequest request)
        {
            HttpRequestMessage httpRequest = RestUtility.BuildIronRequest(config, new RestClientRequest
            {
                EndPoint = request.EndPoint,
                Query    = request.Query,
                Method   = request.Method,
                Content  = request.Content
            });

            using (var client = CreateHttpClient())
            {
                return(client.SendAsync(httpRequest));
            }
        }
Beispiel #3
0
        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)));
        }