コード例 #1
0
        public async Task Invoke(HttpContext context, IApiProxyContextService apiProxyContextService,
                                 IEnumerable <IApiProxyResponseInterceptorProvider> apiProxyResponseInterceptorProviders,
                                 IEnumerable <IApiProxyErrorResponseInterceptorProvider> apiProxyErrorResponseInterceptorProviders,
                                 IApiProxyUriResolver apiProxyUriResolver)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            await _next.Invoke(context);

            var apiProxyContext = apiProxyContextService.Get(context);

            if (apiProxyContext.Response.ApiProxyResponse.StatusCode >= 400)
            {
                apiProxyErrorResponseInterceptorProviders.FirstOrDefault(x => x.Identifier == _apiProxy.Identifier)
                ?.Intercept(
                    apiProxyUriResolver.ResolveInboundUri(context),
                    apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy),
                    apiProxyContext.Response.ApiProxyResponse);
            }
            else
            {
                apiProxyResponseInterceptorProviders.FirstOrDefault(x => x.Identifier == _apiProxy.Identifier)
                ?.Intercept(
                    apiProxyUriResolver.ResolveInboundUri(context),
                    apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy),
                    apiProxyContext.Response.ApiProxyResponse);
            }
        }
        public async Task Invoke(HttpContext context, IApiProxyContextService apiProxyContextService,
                                 IEnumerable <IApiProxyRequestCachingProvider> apiProxyCachingProviders,
                                 IApiProxyResponseCachingService apiProxyResponseCachingService,
                                 IApiProxyUriResolver apiProxyUriResolver)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var provider = apiProxyCachingProviders.FirstOrDefault(x => x.Identifier == _apiProxy.Identifier);

            if (provider == null)
            {
                // No caching provider defined for Api proxy, call next middleware...
                await _next.Invoke(context);

                return;
            }

            var isCachable = provider.IsCachable(
                apiProxyUriResolver.ResolveInboundUri(context),
                apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy),
                context, out var key, out var options);

            var apiProxyContext = isCachable ? apiProxyContextService.Get(context) : null;

            if (isCachable)
            {
                // Try get Api proxy response from cache...
                var apiProxyResponse = apiProxyResponseCachingService.Get(key);
                if (apiProxyResponse != null)
                {
                    // Api proxy response found in cache...
                    apiProxyContext.Response.HasResponse      = true;
                    apiProxyContext.Response.FetchedFromCache = true;
                    apiProxyContext.Response.ApiProxyResponse = apiProxyResponse;
                    return;
                }
            }

            // Not found in cache or not cachable, call next middleware......
            await _next.Invoke(context);

            if (isCachable)
            {
                if (apiProxyContext.Response.HasResponse &&
                    apiProxyContext.Response.ApiProxyResponse.StatusCode == 200)
                {
                    // Add Api proxy response to cache if status code is 200...
                    apiProxyResponseCachingService.Set(key,
                                                       apiProxyContext.Response.ApiProxyResponse,
                                                       options);
                }
            }
        }
コード例 #3
0
        public Uri ResolveOutboundUri(HttpContext context, IApiProxy apiProxy)
        {
            var pathWithNoInboundPathBase = context.Request.Path.Value.Replace(apiProxy.Options.InboundPathBase, "/");
            var path = new PathString(pathWithNoInboundPathBase);

            var url = UriHelper.BuildAbsolute(apiProxy.Options.Scheme, apiProxy.Options.Host,
                                              apiProxy.Options.OutboundPathBase, path, context.Request.QueryString);

            url = QueryHelpers.AddQueryString(url, _apiProxyContextService.Get(context).Request.AppendQueryStringToApiRequest);

            return(new Uri(url));
        }
コード例 #4
0
        public async Task Invoke(HttpContext context, IApiProxyContextService apiProxyContextService, IProxyRequestService proxyRequestService, IApiProxyUriResolver apiProxyUriResolver)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var apiProxyContext = apiProxyContextService.Get(context);

            var outboundUri = apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy);

            apiProxyContext.Response.ApiProxyResponse = await proxyRequestService.ProxyRequest(context, outboundUri);

            apiProxyContext.Response.HasResponse = true;
        }
コード例 #5
0
        public async Task Invoke(HttpContext context, IApiProxyContextService apiProxyContextService, IApiProxyConfiguration apiProxyConfiguration,
                                 IApiProxyUriResolver apiProxyUriResolver)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var apiProxyContext = apiProxyContextService.Get(context);

            apiProxyContext.ExecutionStart = DateTime.Now;

            await _next.Invoke(context);

            apiProxyContext.ExecutionEnd = DateTime.Now;

            if (apiProxyContext.Response.HasResponse)
            {
                await CopyApiProxyResponseToHttpResponse(context, apiProxyContext, apiProxyUriResolver, apiProxyConfiguration.DebugMode);
            }
        }