Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new price list client with the provided configuration
        /// </summary>
        /// <param name="config"></param>
        public PriceListClient(PriceListClientConfig config)
        {
            this.Config = config ?? throw new ArgumentNullException("config");


            handler        = new HttpClientHandler();
            backoffHandler = new ExponentialBackoffAndRetryHandler((IExponentialBackoffAndRetry) new ExponentialBackoffAndRetryClient(new ExponentialBackoffAndRetryConfig()
            {
                MaximumRetries           = this.Config.MaxErrorRetry,
                DelayInMilliseconds      = 100,
                MaxBackoffInMilliseconds = this.Config.Timeout.HasValue ? (int)this.Config.Timeout.Value.TotalMilliseconds : 10000,
                ExceptionHandlingLogic   = (ex => ex is HttpResponseException && ((HttpResponseException)ex).StatusCode >= HttpStatusCode.InternalServerError || (ex is TimeoutException || ex is OperationCanceledException) || ex is HttpRequestException)
            }), this.handler);
            this.httpClient = new HttpClient(this.backoffHandler);
        }
Ejemplo n.º 2
0
        private async Task <T> ExecuteRequestAsync <T>(string relativePath, string parameterName, Func <HttpResponseMessage, T> func)
        {
            if (string.IsNullOrEmpty(relativePath))
            {
                throw new ArgumentNullException(parameterName);
            }

            HttpResponseMessage response = await this.httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, PriceListClientConfig.GetBaseUrlString(PriceListClientConfig.PRICE_LIST_DEFAULT_URL) + relativePath), HttpCompletionOption.ResponseHeadersRead);

            try
            {
                if (response.IsSuccessStatusCode)
                {
                    return(func(response));
                }

                throw new PriceListException(response, await response.Content.ReadAsStringAsync());
            }
            finally
            {
                response.Dispose();
            }
        }