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);
        }
        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));
        }
 /// <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)
            .ConfigureAwait(false).GetAwaiter().GetResult());
 }
 /// <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));
 }
 /// <summary>
 /// List the resources from <paramref name="path"/>.
 /// </summary>
 /// <param name="path">Path of API.</param>
 /// <param name="httpMethod">HTTP method.</param>
 /// <param name="request">Object with request data.</param>
 /// <param name="requestOptions"><see cref="RequestOptions"/></param>
 /// <returns>
 /// A task whose result is a list of resources.
 /// </returns>
 protected ResourcesList <TResource> List(
     string path,
     HttpMethod httpMethod,
     object request,
     RequestOptions requestOptions = null)
 {
     return(SendAsync <ResourcesList <TResource> >(
                path,
                httpMethod,
                request,
                requestOptions,
                default)
            .ConfigureAwait(false).GetAwaiter().GetResult());
 }
        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);
        }