Exemplo n.º 1
0
        private static HttpRequestMessage CreateHttpRequest(MercadoPagoRequest request)
        {
            var httpRequest = new HttpRequestMessage
            {
                Method     = MapHttpMethod(request.Method),
                RequestUri = new Uri(request.Url),
            };

            if (request.Content != null)
            {
                httpRequest.Content = new StringContent(request.Content, Encoding.UTF8, JSON_CONTENT_TYPE);
            }

            if (request.Headers != null)
            {
                foreach (var header in request.Headers)
                {
                    httpRequest.Headers.Add(header.Key, header.Value);
                }
            }

            return(httpRequest);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends a HTTP request to MercadoPago's APIs.
        /// </summary>
        /// <param name="request">Request data.</param>
        /// <param name="retryStrategy">Strategy to be used when it is necessary to retry the request.</param>
        /// <param name="cancellationToken">Cancellation token to cancel operation.</param>
        /// <returns>A Task with response data.</returns>
        public async Task <MercadoPagoResponse> SendAsync(
            MercadoPagoRequest request,
            IRetryStrategy retryStrategy,
            CancellationToken cancellationToken)
        {
            HttpResponseMessage httpResponse = null;
            int                numberRetries = 0;
            Exception          exception;
            HttpRequestMessage httpRequest;

            while (true)
            {
                httpRequest = CreateHttpRequest(request);

                try
                {
                    httpResponse = await HttpClient.SendAsync(httpRequest,
                                                              HttpCompletionOption.ResponseHeadersRead,
                                                              cancellationToken).ConfigureAwait(false);

                    exception = null;
                }
                catch (Exception ex)
                {
                    exception = ex;
                }

                RetryResponse retryResponse = retryStrategy.ShouldRetry(
                    httpRequest,
                    httpResponse,
                    IsRetryableError(exception, cancellationToken),
                    numberRetries);

                if (!retryResponse.Retry)
                {
                    break;
                }

                // Dispose HTTP response if if will retry
                if (httpResponse != null)
                {
                    httpResponse.Dispose();
                }

                numberRetries++;
                await Task.Delay(retryResponse.Delay).ConfigureAwait(false);
            }

            // Dispose HTTP request
            if (httpRequest != null)
            {
                httpRequest.Dispose();
            }

            if (exception != null)
            {
                throw exception;
            }

            return(await MapResponse(httpResponse));
        }