public async Task <Response <HttpResponseMessage> > GetResponse(DownstreamContext request) { var builder = new HttpClientBuilder(_factory); var cacheKey = GetCacheKey(request); var httpClient = GetHttpClient(cacheKey, builder, request); try { var response = await httpClient.SendAsync(request.DownstreamRequest); 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 { _cacheHandlers.Set(cacheKey, httpClient, TimeSpan.FromHours(24)); } }
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(); } }
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 (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(); } }
private IHttpClient GetHttpClient(Request.Request request) { var builder = new HttpClientBuilder(); var cacheKey = GetCacheKey(request, builder); var httpClient = _cacheHandlers.Get(cacheKey); if (httpClient == null) { httpClient = builder.Create(); } _cacheHandlers.Set(cacheKey, httpClient, TimeSpan.FromHours(6)); return(httpClient); }
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(); } }
public async Task <Response <HttpResponseMessage> > GetResponse(Request.Request request) { var builder = new HttpClientBuilder(); using (var handler = new HttpClientHandler { CookieContainer = request.CookieContainer }) { if (request.IsQos) { builder.WithQoS(request.QosProvider, _logger, handler); } using (var httpClient = builder.Build(handler)) { try { var response = await httpClient.SendAsync(request.HttpRequestMessage); 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))); } } } }
public async Task <Response <HttpResponseMessage> > GetResponseAsync(DownstreamContext request) { IHttpClient httpClient; //if (_discoveryClient != null) //{ // _handler = new DiscoveryHttpClientHandler(_discoveryClient); // var discoveryClientBuilder = new DiscoveryHttpClientBuilder().Create(_handler, request.DownstreamReRoute); // var httpDiscoveryClient = discoveryClientBuilder.Client; // try // { // var response = await httpDiscoveryClient.SendAsync(request.DownstreamRequest); // 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 // { // //_cacheHandlers.Set(cacheKey, httpClient, TimeSpan.FromHours(24)); // } //} //else { var builder = new HttpClientBuilder(_house); var cacheKey = GetCacheKey(request); //var httpClient = GetHttpClient(cacheKey, builder, request); httpClient = GetHttpClient(cacheKey, builder, request); try { var response = await httpClient.SendAsync(request.DownstreamRequest); 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 { _cacheHandlers.Set(cacheKey, httpClient, TimeSpan.FromHours(24)); } } }