Example #1
0
        private async Task <MercadoPagoRequest> BuildRequestAsync(
            string path,
            HttpMethod httpMethod,
            object request,
            RequestOptions requestOptions)
        {
            var mercadoPagoRequest = BuildRequest(path, httpMethod, requestOptions);
            var queryString        = string.Empty;

            if (request != null)
            {
                AddIdempotencyKey(mercadoPagoRequest, request);

                if (httpMethod == HttpMethod.POST || httpMethod == HttpMethod.PUT)
                {
                    mercadoPagoRequest.Content = Serializer.SerializeToJson(request);
                }
                else if (httpMethod == HttpMethod.GET)
                {
                    string parameters = await Serializer.SerializeToQueryStringAsync(request);

                    if (!string.IsNullOrEmpty(parameters))
                    {
                        queryString = $"?{parameters}";
                    }
                }
            }

            mercadoPagoRequest.Url = $"{mercadoPagoRequest.Url}{queryString}";

            return(mercadoPagoRequest);
        }
Example #2
0
        private async Task <TResponse> SendAsync <TResponse>(
            string path,
            HttpMethod httpMethod,
            object request,
            RequestOptions requestOptions,
            CancellationToken cancellationToken)
            where TResponse : IResource, new()
        {
            MercadoPagoResponse response;

            requestOptions = BuildRequestOptions(requestOptions);

            try
            {
                MercadoPagoRequest mercadoPagoRequest = await BuildRequestAsync(
                    path,
                    httpMethod,
                    request,
                    requestOptions);

                response = await HttpClient.SendAsync(
                    mercadoPagoRequest,
                    requestOptions.RetryStrategy,
                    cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw new MercadoPagoException(ex);
            }

            return(ParseResponse <TResponse>(response));
        }
Example #3
0
 /// <summary>
 /// Send a request to api <paramref name="path"/> with HTTP method <paramref name="httpMethod"/>.
 /// The content body is in <paramref name="request"/>.
 /// </summary>
 /// <param name="path">Path of the endpoint.</param>
 /// <param name="httpMethod">HTTP method.</param>
 /// <param name="request">Object with request data.</param>
 /// <param name="requestOptions"><see cref="RequestOptions"/></param>
 /// <returns>A resource that represents the API response.</returns>
 protected TResource Send(
     string path,
     HttpMethod httpMethod,
     object request,
     RequestOptions requestOptions = null)
 {
     return(SendAsync(path, httpMethod, request, requestOptions, default)
            .ConfigureAwait(false).GetAwaiter().GetResult());
 }
Example #4
0
 /// <summary>
 /// Send a async request to api <paramref name="path"/> with HTTP method <paramref name="httpMethod"/>.
 /// The content body is in <paramref name="request"/>.
 /// </summary>
 /// <param name="path">Path of the endpoint.</param>
 /// <param name="httpMethod">HTTP method.</param>
 /// <param name="request">Object with request data.</param>
 /// <param name="requestOptions"><see cref="RequestOptions"/></param>
 /// <param name="cancellationToken">Cancellation token.</param>
 /// <returns>
 /// A task whose result is a resource that represents the API response.
 /// </returns>
 protected Task <TResource> SendAsync(
     string path,
     HttpMethod httpMethod,
     object request,
     RequestOptions requestOptions       = null,
     CancellationToken cancellationToken = default)
 {
     return(SendAsync <TResource>(
                path,
                httpMethod,
                request,
                requestOptions,
                cancellationToken));
 }
Example #5
0
        private MercadoPagoRequest BuildRequest(
            string path,
            HttpMethod httpMethod,
            RequestOptions requestOptions)
        {
            var mercadoPagoRequest = new MercadoPagoRequest
            {
                Url    = $"{MercadoPagoConfig.BaseUrl}{path}",
                Method = httpMethod,
            };

            AddRequestHeaders(path, mercadoPagoRequest, requestOptions);

            return(mercadoPagoRequest);
        }