Ejemplo n.º 1
0
 /// <inheritdoc />
 public async Task <IHttpResponseInfo <TResponse> > Get <TResponse>(Uri uri, IResponseContentConverter <TResponse> responseContentConverter,
                                                                    CancellationToken cancellationToken)
     where TResponse : class
 {
     return(await SendRequestExpectingResponseContent <TResponse>(uri, HttpMethod.Get, responseContentConverter, cancellationToken));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Performs a request to the specified URL using the specified method, expecting response content.
        /// </summary>
        /// <typeparam name="TResponse">The type into which the response should be deserialized.</typeparam>
        /// <param name="uri">The Uri to which the request should be sent.</param>
        /// <param name="httpMethod">The HTTP method to send with the request.</param>
        /// <param name="cancellationToken">A CancellationToken to associate with the request.</param>
        /// <returns>An HttpResponseInfo instance of type TResponse.</returns>
        private async Task <IHttpResponseInfo <TResponse> > SendRequestExpectingResponseContent <TResponse>(Uri uri, HttpMethod httpMethod,
                                                                                                            IResponseContentConverter <TResponse> responseContentConverter, CancellationToken cancellationToken)
            where TResponse : class
        {
            TraceRequestIfRequired(uri, httpMethod);

            using (var request = new HttpRequestMessage(httpMethod, uri))
            {
                // Add the headers.
                foreach (var headerPair in defaultHeaders)
                {
                    request.Headers.Add(headerPair.Key, headerPair.Value);
                }

                using (var response = await httpClient.SendAsync(request, cancellationToken))
                {
                    return(await ParseHttpContentAndConstructResponse(
                               new HttpRequestInfo(uri, httpMethod), response.StatusCode, responseContentConverter, response.Content));
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Attempts to parse the supplied HttpContent instance and constructs an IHttpResponseInfo response object.
        /// If parsing fails on a successful response, an exception is thrown.
        /// </summary>
        /// <typeparam name="TResponse">The expected Type of the converted HttpContent.</typeparam>
        /// <param name="httpRequestInfo">The IHttpRequestInfo instance describing the request.</param>
        /// <param name="statusCode">The status code associated with the HTTP response.</param>
        /// <param name="content">The HttpContent object which should be parsed.</param>
        /// <returns>A Task returning the expected response object Type.</returns>
        private async Task <IHttpResponseInfo <TResponse> > ParseHttpContentAndConstructResponse <TResponse>(IHttpRequestInfo httpRequestInfo, HttpStatusCode statusCode,
                                                                                                             IResponseContentConverter <TResponse> responseContentConverter, HttpContent content)
            where TResponse : class
        {
            var is2xxCode = (int)statusCode >= 200 && (int)statusCode <= 299;

            // If this isn't a success code, we don't try to handle the response content and so return an empty error response object.
            if (!is2xxCode)
            {
                return(new HttpResponseInfo <TResponse>(statusCode, httpRequestInfo));
            }

            // Otherwise, we try to parse the content. Failures to parse in 2xx cases here are considered fatal so we let any exceptions bubble up.
            var responseContent = await responseContentConverter.ConvertFrom(content);

            return(new HttpResponseInfo <TResponse>(statusCode, httpRequestInfo, responseContent));
        }