Example #1
0
        public async Task <IApiResponse <T> > SendRequestAsync <T>(
            Uri uri,
            HttpMethod method,
            object body,
            IDictionary <string, IEnumerable <string> > headers,
            CancellationToken cancellationToken)
        {
            Requires.ArgumentNotNull(uri, nameof(uri));
            Requires.ArgumentNotNull(method, nameof(method));

            using (var request = new HttpRequestMessage {
                RequestUri = uri, Method = method
            })
            {
                var contentType = "application/hal+json";
                headers = headers ?? new Dictionary <string, IEnumerable <string> >();
                foreach (var header in headers)
                {
                    if (header.Key == "Content-Type")
                    {
                        contentType = header.Value.FirstOrDefault();
                        continue;
                    }

                    request.Headers.Add(header.Key, header.Value);
                }
                request.Content = GetRequestContent(method, body, contentType);

                var responseMessage = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(Configuration);

                return(await _responseFactory.CreateApiResponseAsync <T>(responseMessage).ConfigureAwait(Configuration));
            }
        }
Example #2
0
        private async Task <ApiException> GetApiErrorException(HttpResponseMessage response)
        {
            var errorResponse = await _responseFactory.CreateApiResponseAsync <ApiError>(response).ConfigureAwait(_configuration);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(new ResourceNotFoundException(errorResponse));
            }

            // We need to deserialize the Body here because the IApiResponseFactory won't deserialize error responses
            var apiError = _jsonSerializer.Deserialize <ApiError>(errorResponse.Body);

            Func <IApiResponse, ApiError, ApiErrorException> exceptionFactoryFunc;

            if (apiError.Code != null &&
                ExceptionFactoryMap.TryGetValue(apiError.Code, out exceptionFactoryFunc))
            {
                return(exceptionFactoryFunc(errorResponse, apiError));
            }

            return(new ApiErrorException(errorResponse, apiError));
        }
Example #3
0
        private async Task <IReadOnlyList <IApiResponse <TResponse> > > ParseBatchResponse <TResponse>(HttpResponseMessage httpBatchResponse)
        {
            var innerResponses = BatchResponseParser.Parse(await httpBatchResponse.Content.ReadAsStringAsync().ConfigureAwait(_configuration));

            var apiResponses = new List <IApiResponse <TResponse> >();

            foreach (var response in innerResponses)
            {
                apiResponses.Add(await _responseFactory.CreateApiResponseAsync <TResponse>(response).ConfigureAwait(_configuration));
            }

            return(apiResponses);
        }