Beispiel #1
0
        public async Task <TResult> Post <TResult>(
            string path,
            SdkAuthorization authorization,
            IDictionary <int, Type> resultTypeMappings,
            object request = null, CancellationToken cancellationToken = default, string idempotencyKey = null)
            where TResult : HttpMetadata
        {
            var httpResponse = await SendRequestAsync(
                HttpMethod.Post,
                path,
                authorization,
                request,
                cancellationToken,
                idempotencyKey
                );

            resultTypeMappings.TryGetValue((int)httpResponse.StatusCode, out var responseType);

            if (responseType == null)
            {
                throw new InvalidOperationException(
                          $"The status code {(int)httpResponse.StatusCode} is not mapped to a result type");
            }

            return(await DeserializeResponseAsync(httpResponse, responseType));
        }
Beispiel #2
0
        private async Task <HttpResponseMessage> Invoke(
            HttpMethod httpMethod,
            string path,
            SdkAuthorization authorization,
            HttpContent httpContent,
            CancellationToken cancellationToken,
            string idempotencyKey)
        {
            CheckoutUtils.ValidateParams("httpMethod", httpMethod, "authorization", authorization);
            var httpRequest = new HttpRequestMessage(httpMethod, path)
            {
                Content = httpContent
            };

            _log.LogInformation(@"{HttpMethod}: {Path}", httpMethod, path);

            httpRequest.Headers.UserAgent.ParseAdd(
                "checkout-sdk-net/" + CheckoutUtils.GetAssemblyVersion <CheckoutSdk>());
            httpRequest.Headers.TryAddWithoutValidation("Authorization", authorization.GetAuthorizationHeader());

            if (!string.IsNullOrWhiteSpace(idempotencyKey))
            {
                httpRequest.Headers.Add("Cko-Idempotency-Key", idempotencyKey);
            }

            return(await _httpClient.SendAsync(httpRequest, cancellationToken));
        }
Beispiel #3
0
        public async Task <TResult> Query <TResult>(
            string path,
            SdkAuthorization authorization,
            object request = null,
            CancellationToken cancellationToken = default)
            where TResult : HttpMetadata
        {
            var dictionary = new Dictionary <string, string>();

            if (request != null)
            {
                var json = _serializer.Serialize(request);
                dictionary =
                    (Dictionary <string, string>)_serializer.Deserialize(json, typeof(IDictionary <string, string>));
            }

            var httpResponse = await SendRequestAsync(
                HttpMethod.Get,
                QueryHelpers.AddQueryString(path, dictionary),
                authorization,
                null,
                cancellationToken,
                null
                );

            return(await DeserializeResponseAsync <TResult>(httpResponse));
        }
Beispiel #4
0
        public async Task <TResult> Get <TResult>(
            string path,
            SdkAuthorization authorization,
            CancellationToken cancellationToken = default)
            where TResult : HttpMetadata
        {
            var httpResponse = await SendRequestAsync(
                HttpMethod.Get,
                path,
                authorization,
                null,
                cancellationToken,
                null
                );

            return(await DeserializeResponseAsync <TResult>(httpResponse));
        }
Beispiel #5
0
        public async Task <TResult> Patch <TResult>(
            string path,
            SdkAuthorization authorization,
            object request = null,
            CancellationToken cancellationToken = default,
            string idempotencyKey = null)
            where TResult : HttpMetadata
        {
            var httpResponse = await SendRequestAsync(
                new HttpMethod("PATCH"),
                path,
                authorization,
                request,
                cancellationToken,
                null
                );

            return(await DeserializeResponseAsync <TResult>(httpResponse));
        }
Beispiel #6
0
        private async Task <HttpResponseMessage> SendRequestAsync(
            HttpMethod httpMethod,
            string path,
            SdkAuthorization authorization,
            object requestBody,
            CancellationToken cancellationToken,
            string idempotencyKey)
        {
            CheckoutUtils.ValidateParams("httpMethod", httpMethod, "authorization", authorization);

            HttpContent httpContent = null;

            if (requestBody != null)
            {
                if (requestBody is MultipartFormDataContent content)
                {
                    httpContent = content;
                }
                else
                {
                    httpContent = new StringContent(_serializer.Serialize(requestBody), Encoding.UTF8,
                                                    "application/json");
                }
            }

            HttpResponseMessage httpResponse = await Invoke(
                httpMethod,
                path,
                authorization,
                httpContent,
                cancellationToken,
                idempotencyKey);

            await ValidateResponseAsync(httpResponse);

            return(httpResponse);
        }