Beispiel #1
0
        public virtual async Task <T> PostAsync <T>(string method, object postParams = null, IDictionary <string, object> headers = null)
            where T : class
        {
            HttpContent content = null;

            if (postParams != null)
            {
                content = _builder.Build(postParams);
            }

            if (!headers.NullOrEmpty())
            {
                if (content == null)
                {
                    content = new StringContent(string.Empty);
                }

                FillContentHeaders(headers, content);
            }

            var url        = BuildUrl(method);
            var resultPost = await _client.PostAsync(url, content);

            return(await ProcessResultAsync <T>(resultPost));
        }
Beispiel #2
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);
            }
Beispiel #3
0
        private HttpContent BuildContent(object requestParams, IDictionary <string, object> headers)
        {
            HttpContent content = null;

            if (requestParams != null)
            {
                content = _builder.Build(requestParams);
            }

            if (!headers.NullOrEmpty())
            {
                if (content == null)
                {
                    content = new StringContent(string.Empty);
                }

                FillContentHeaders(headers, content);
            }

            return(content);
        }