Пример #1
0
        public async Task <Response <HttpResponseMessage> > GetResponse(DownstreamContext context)
        {
            var builder = new HttpClientBuilder(_factory, _cacheHandlers, _logger);

            var httpClient = builder.Create(context);

            try
            {
                var response = await httpClient.SendAsync(context.DownstreamRequest.ToHttpRequestMessage());

                return(new OkResponse <HttpResponseMessage>(response));
            }
            catch (TimeoutRejectedException exception)
            {
                return(new ErrorResponse <HttpResponseMessage>(new RequestTimedOutError(exception)));
            }
            catch (BrokenCircuitException exception)
            {
                return(new ErrorResponse <HttpResponseMessage>(new RequestTimedOutError(exception)));
            }
            catch (Exception exception)
            {
                return(new ErrorResponse <HttpResponseMessage>(new UnableToCompleteRequestError(exception)));
            }
            finally
            {
                builder.Save();
            }
        }
Пример #2
0
        public async Task <Response <HttpResponseMessage> > GetResponse(HttpContext httpContext)
        {
            var builder = new HttpClientBuilder(_factory, _cacheHandlers, _logger);

            var downstreamRoute = httpContext.Items.DownstreamRoute();

            var downstreamRequest = httpContext.Items.DownstreamRequest();

            var httpClient = builder.Create(downstreamRoute);

            try
            {
                var response = await httpClient.SendAsync(downstreamRequest.ToHttpRequestMessage(), httpContext.RequestAborted);

                return(new OkResponse <HttpResponseMessage>(response));
            }
            catch (Exception exception)
            {
                var error = _mapper.Map(exception);
                return(new ErrorResponse <HttpResponseMessage>(error));
            }
            finally
            {
                builder.Save();
            }
        }
Пример #3
0
        public async Task <Response <HttpResponseMessage> > GetResponse(DownstreamContext context)
        {
            var builder = new HttpClientBuilder(_factory, _cacheHandlers, _logger);

            var httpClient = builder.Create(context);

            try
            {
                var message = context.DownstreamRequest.ToHttpRequestMessage();

                /**
                 * According to https://tools.ietf.org/html/rfc7231
                 * GET,HEAD,DELETE,CONNECT,TRACE
                 * Can have body but server can reject the request.
                 * And MS HttpClient in Full Framework actually rejects it.
                 * see #366 issue
                 **/

                if (message.Method == HttpMethod.Get ||
                    message.Method == HttpMethod.Head ||
                    message.Method == HttpMethod.Delete ||
                    message.Method == HttpMethod.Trace)
                {
                    message.Content = null;
                }
                _logger.LogDebug(string.Format("Sending {0}", message));
                var response = await httpClient.SendAsync(message);

                return(new OkResponse <HttpResponseMessage>(response));
            }
            catch (TimeoutRejectedException exception)
            {
                return(new ErrorResponse <HttpResponseMessage>(new RequestTimedOutError(exception)));
            }
            catch (TaskCanceledException exception)
            {
                return(new ErrorResponse <HttpResponseMessage>(new RequestTimedOutError(exception)));
            }
            catch (BrokenCircuitException exception)
            {
                return(new ErrorResponse <HttpResponseMessage>(new RequestTimedOutError(exception)));
            }
            catch (Exception exception)
            {
                return(new ErrorResponse <HttpResponseMessage>(new UnableToCompleteRequestError(exception)));
            }
            finally
            {
                builder.Save();
            }
        }