Esempio n. 1
0
        /// <summary>Sends a request to Stripe's API as an asynchronous operation.</summary>
        /// <typeparam name="T">Type of the Stripe entity returned by the API.</typeparam>
        /// <param name="method">The HTTP method.</param>
        /// <param name="path">The path of the request.</param>
        /// <param name="options">The parameters of the request.</param>
        /// <param name="requestOptions">The special modifiers of the request.</param>
        /// <param name="cancellationToken">The cancellation token to cancel operation.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        /// <exception cref="StripeException">Thrown if the request fails.</exception>
        public async Task <T> RequestAsync <T>(
            HttpMethod method,
            string path,
            BaseOptions options,
            RequestOptions requestOptions,
            CancellationToken cancellationToken = default(CancellationToken))
            where T : IStripeEntity
        {
            var request = new StripeRequest(this, method, path, options, requestOptions);

            var response = await this.HttpClient.MakeRequestAsync(request, cancellationToken)
                           .ConfigureAwait(false);

            return(ProcessResponse <T>(response));
        }
Esempio n. 2
0
        private System.Net.Http.HttpRequestMessage BuildRequestMessage(StripeRequest request)
        {
            var requestMessage = new System.Net.Http.HttpRequestMessage(request.Method, request.Uri);

            // Standard headers
            requestMessage.Headers.TryAddWithoutValidation("User-Agent", this.userAgentString);
            requestMessage.Headers.Authorization = request.AuthorizationHeader;

            // Custom headers
            requestMessage.Headers.Add("X-Stripe-Client-User-Agent", this.stripeClientUserAgentString);
            foreach (var header in request.StripeHeaders)
            {
                requestMessage.Headers.Add(header.Key, header.Value);
            }

            // Request body
            requestMessage.Content = request.Content;

            return(requestMessage);
        }
        /// <inheritdoc/>
        public async Task <Stream> RequestStreamingAsync(
            HttpMethod method,
            string path,
            BaseOptions options,
            RequestOptions requestOptions,
            CancellationToken cancellationToken = default)
        {
            var request = new StripeRequest(this, method, path, options, requestOptions);

            var response = await this.HttpClient.MakeStreamingRequestAsync(request, cancellationToken)
                           .ConfigureAwait(false);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return(response.Body);
            }

            var readResponse = await response.ToStripeResponseAsync().ConfigureAwait(false);

            throw BuildStripeException(readResponse);
        }
Esempio n. 4
0
        /// <summary>Sends a request to Stripe's API as an asynchronous operation.</summary>
        /// <param name="request">The parameters of the request to send.</param>
        /// <param name="cancellationToken">The cancellation token to cancel operation.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public async Task <StripeResponse> MakeRequestAsync(
            StripeRequest request,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            TimeSpan            duration;
            Exception           requestException = null;
            HttpResponseMessage response         = null;
            int retry = 0;

            this.requestTelemetry.MaybeAddTelemetryHeader(request.StripeHeaders);

            while (true)
            {
                requestException = null;

                var httpRequest = this.BuildRequestMessage(request);

                var stopwatch = Stopwatch.StartNew();

                try
                {
                    response = await this.httpClient.SendAsync(httpRequest, cancellationToken)
                               .ConfigureAwait(false);
                }
                catch (HttpRequestException exception)
                {
                    requestException = exception;
                }
                catch (OperationCanceledException exception)
                    when(!cancellationToken.IsCancellationRequested)
                    {
                        requestException = exception;
                    }

                stopwatch.Stop();

                duration = stopwatch.Elapsed;

                if (!this.ShouldRetry(
                        retry,
                        requestException != null,
                        request.Method,
                        response?.StatusCode))
                {
                    break;
                }

                retry += 1;
                await Task.Delay(this.SleepTime(retry)).ConfigureAwait(false);
            }

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

            this.requestTelemetry.MaybeEnqueueMetrics(response, duration);

            var reader = new StreamReader(
                await response.Content.ReadAsStreamAsync().ConfigureAwait(false));

            return(new StripeResponse(
                       response.StatusCode,
                       response.Headers,
                       await reader.ReadToEndAsync().ConfigureAwait(false))
            {
                NumRetries = retry,
            });
        }