コード例 #1
0
        private static async Task <Error> TryGetErrorAsync(HttpResponseMessage httpResponseMessage)
        {
            if (!httpResponseMessage.IsSuccessStatusCode)
            {
                try
                {
                    var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    var deserializedContent = await SystemTextJsonSerializer.DeserializeAsync <Error>(contentStream).ConfigureAwait(false);

                    if (deserializedContent == null)
                    {
                        throw new Exception("Could not deserialize JSON response");
                    }

                    return(deserializedContent);
                }
                catch (Exception)
                {
                    // Intentionally left empty
                }
            }

            return(Error.Empty);
        }
        public static async Task <TResult> PostAsync <TRequest, TResult>(this HttpClient httpClient, string url, TRequest request)
        {
            var requestJson    = SystemTextJsonSerializer.Serialize(request);
            var requestContent = GetJsonStringContent(requestJson);

            HttpResponseMessage httpResponseMessage;

            try
            {
                httpResponseMessage = await httpClient.PostAsync(url, requestContent).ConfigureAwait(false);
            }
            catch (HttpRequestException e)
            {
                throw BankIdApiException.Unknown(e);
            }

            await BankIdApiErrorHandler.EnsureSuccessAsync(httpResponseMessage).ConfigureAwait(false);

            var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);

            var deserializedContent = await SystemTextJsonSerializer.DeserializeAsync <TResult>(contentStream).ConfigureAwait(false);

            if (deserializedContent == null)
            {
                throw new Exception("Could not deserialize JSON response");
            }

            return(deserializedContent);
        }