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); } } }