Beispiel #1
0
            private async Task <HttpResponseMessage> SendAsyncInternal()
            {
                try
                {
                    if (_contentBuilder != null)
                    {
                        _request.Content = _contentBuilder.Build();
                    }
                }
                catch (Exception ex)
                {
                    throw new ApiException(this, ex.Message, ex, RequestStatus.Aborted);
                }

                _apiRequestLogger.LogStart(_request);
                if (_contentBuilder != null)
                {
                    _apiPayloadLogger.LogRequestPayload(_contentBuilder);
                }
                var response = await _apiClient._client.SendAsync(_request);

                _apiRequestLogger.LogEnd(_request, response);

                if (!response.IsSuccessStatusCode)
                {
                    if (response.Content != null &&
                        string.Equals(response.Content.Headers.ContentType?.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
                    {
                        ApiError error;
                        try
                        {
                            using (var stream = await response.Content.ReadAsStreamAsync())
                            {
                                _apiPayloadLogger.LogResponsePayload(stream);
                                using (JsonTextReader jsonTextReader = new JsonTextReader(new StreamReader(stream)))
                                {
                                    JsonSerializer jsonSerializer = JsonSerializer.Create(JsonConstants.JsonSerializerSettings);
                                    error = jsonSerializer.Deserialize <ApiError>(jsonTextReader);
                                }
                            }
                        }
                        catch
                        {
                            throw new ApiException(this, "Request Failed with status code: " + response.StatusCode.ToString(), response.StatusCode, RequestStatus.Failed);
                        }

                        throw new ApiException(this, "Request Failed with status code: " + response.StatusCode.ToString(), response.StatusCode, error, RequestStatus.Failed);
                    }
                }
                return(response);
            }