public async Task <Response <HttpResponseMessage> > GetResponse(Request.Request request)
        {
            var builder = new HttpClientBuilder();

            var cacheKey = GetCacheKey(request, builder);

            var httpClient = GetHttpClient(cacheKey, builder, request.UseCookieContainer, request.AllowAutoRedirect);

            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)));
            }
            finally
            {
                _cacheHandlers.Set(cacheKey, httpClient, TimeSpan.FromHours(24));
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        public async Task <HttpResponseMessage> GetResponse(HttpRequestMessage request)
        {
            var builder = new HttpClientBuilder();

            var cacheKey = GetCacheKey(request, builder);

            var httpClient = GetHttpClient(cacheKey, builder);

            try
            {
                return(await httpClient.SendAsync(request));
            }
            catch (Exception exception)
            {
                _logger.LogError("Error making http request, exception:" + exception.Message);
                throw;
            }
            finally
            {
                _cacheHandlers.Set(cacheKey, httpClient, TimeSpan.FromHours(24));
            }
        }
 public void Save()
 {
     _cacheHandlers.Set(_cacheKey, _client, TimeSpan.FromHours(24));
 }
        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));
                }
            }
        }