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); } }
private void AddDebugInfoToResponse(HttpContext context, HttpResponse response, ApiProxyContext apiProxyContext, IApiProxyUriResolver apiProxyUriResolver) { response.Headers["api-proxy-outbound-url"] = apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy).ToString(); response.Headers["api-proxy-fetched-from-cache"] = apiProxyContext.Response.FetchedFromCache.ToString().ToLower(); response.Headers["api-proxy-middleware-execution-time"] = apiProxyContext.ExecutionEnd .Subtract(apiProxyContext.ExecutionStart).TotalMilliseconds.ToString(CultureInfo.InvariantCulture); }
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); } } }
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; }