public ResponseContext(HttpResponseMessage response, RequestDescriptor requestDescriptor, Type genericReturnType = null) { Response = response ?? throw new ArgumentNullException(nameof(response)); RequestDescriptor = requestDescriptor ?? throw new ArgumentNullException(nameof(requestDescriptor)); }
private object CreateContextProperties(RequestDescriptor descriptor, MethodInfo targetMethod) { var properties = new { EnvironmentName = Environment.MachineName, Message = $"Proxy call from Sandbox: {Environment.MachineName} to API method: {targetMethod?.Name}", Method = descriptor?.Request?.Method, Authority = descriptor?.Request?.RequestUri?.Authority, LocalPath = descriptor?.Request?.RequestUri?.LocalPath, RequestRegion = descriptor?.RegionKey }; return(properties); }
private async Task <ResponseContext> InternalInvokeAsync(MethodInfo targetMethod, object[] args, Type genericReturnType = null) { var culture = _proxyContext.CultureFactory?.Invoke(); var descriptor = new RequestDescriptor(targetMethod, _proxyContext.ProxyType, _proxyContext.ClientIp, _proxyContext.UserAgent, _proxyContext.Query, culture, args); RequestContext requestContext = await _proxyManager.CreateRequestAsync(descriptor); ResponseContext responseContext = null; try { HttpResponseMessage response = null; string metadata = string.Empty; if (_proxyManager.HasFilter) { await Task.WhenAll(_proxyManager.RequestFilters.Select(t => t.InvokeAsync(requestContext))); } response = await _proxyManager.HttpClient.SendAsync(requestContext.Request); responseContext = await ProxyResultExecutor.ExecuteAsync(response, requestContext, genericReturnType); } catch (Exception ex) { var properties = CreateContextProperties(requestContext, targetMethod); var result = properties.GetConfigurationContextDetail(properties); throw new ProxyException(result, ex); } if ((int)responseContext.Response.StatusCode == StatusCodes.Status404NotFound) { var properties = CreateContextProperties(requestContext, targetMethod); var result = properties.GetConfigurationContextDetail(properties); throw new ProxyException(result, new HttpRequestException()); } return(responseContext); }
private async Task <ResponseContext> InternalInvokeAsync(MethodInfo targetMethod, object[] args, Type genericReturnType = null) { var requestContext = new RequestContext(targetMethod, _proxyContext.ProxyType, _proxyContext.ClientIp, _proxyContext.UserAgent, _proxyContext.TokenCookie, _proxyContext.QueryString, args); RequestDescriptor descriptor = await _proxyManager.CreateDescriptorAsync(requestContext); ResponseContext responseContext = null; try { HttpResponseMessage response = null; var httpClient = _proxyManager.HttpClient; string metadata = string.Empty; response = await httpClient.SendAsync(descriptor.Request); responseContext = await ProxyResultExecutor.ExecuteAsync(response, descriptor, genericReturnType); } catch (Exception ex) { var properties = CreateContextProperties(descriptor, targetMethod); var result = properties.GetConfigurationContextDetail(properties); throw new ProxyException(result, ex); } if ((int)responseContext.Response.StatusCode == StatusCodes.Status404NotFound) { var properties = CreateContextProperties(descriptor, targetMethod); var result = properties.GetConfigurationContextDetail(properties); throw new ProxyException(result, new HttpRequestException()); } return(responseContext); }
public async Task CreateRequestContentAsync(RequestDescriptor requestContext, HttpRequestMessage request, ProxyMethodDescriptor descriptor, UriBuilder uriBuilder) { await Task.CompletedTask; var httpMethod = descriptor.HttpMethod; var contentModelBinder = ContentBinderFactory.GetContentModelBinder(_serviceProvider, httpMethod, descriptor.ContentType); var bindingContext = new ContentModelBindingContext(httpMethod, descriptor, uriBuilder) { ContentType = descriptor.ContentType, ModelContentResolver = ContentResolver, Args = requestContext.Args, }; contentModelBinder.BindContent(bindingContext); bindingContext.TrySetContent(request); request.RequestUri = bindingContext.Uri; }
public async Task <RequestContext> CreateRequestAsync(RequestDescriptor requestDescriptor) { var proxyDescriptor = _typeManager.ProxyDescriptors.FirstOrDefault(x => x.ProxyType == requestDescriptor.ProxyType); if (proxyDescriptor == null) { throw new ArgumentOutOfRangeException("Proxy type could not be found!"); } var regionKey = proxyDescriptor.RegionKey; ProxyMethodDescriptor methodDescriptor; if (!proxyDescriptor.Methods.TryGetValue(requestDescriptor.TargetMethod, out methodDescriptor)) { throw new ArgumentOutOfRangeException("Method (Action) info could not be found!"); } HttpRequestMessage request = CreateHttpRequest(methodDescriptor, requestDescriptor); request.Method = methodDescriptor.HttpMethod; var methodPath = requestDescriptor.TargetMethod.Name; if (methodDescriptor.MethodMarkerTemplate.HasValue()) { methodPath = methodDescriptor.MethodMarkerTemplate; } UriBuilder uriBuilder = _endpointManager.CreateUriBuilder(methodDescriptor, proxyDescriptor.Route, regionKey, methodPath); TimeSpan? timeout = methodDescriptor.Timeout; await _streamProvider.CreateRequestContentAsync(requestDescriptor, request, methodDescriptor, uriBuilder); return(new RequestContext(request, methodDescriptor, proxyDescriptor.RegionKey, timeout)); }
private HttpRequestMessage CreateHttpRequest(ProxyMethodDescriptor methodDescriptor, RequestDescriptor requestDescriptor) { HttpRequestMessage requestMessage = new HttpRequestMessage(); var headers = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(requestDescriptor.ClientIp)) { headers.Add("X-Forwarded-For", requestDescriptor.ClientIp); } if (!string.IsNullOrEmpty(requestDescriptor.UserAgent)) { headers.Add("User-Agent", requestDescriptor.UserAgent); } if (requestDescriptor.Culture != null) { headers.Add("Accept-Language", requestDescriptor.Culture.Name); } headers.Merge(_headerProvider.Headers, true); headers.Merge(methodDescriptor.Headers, true); foreach (KeyValuePair <string, string> entry in headers) { requestMessage.Headers.Add(entry.Key, entry.Value); } return(requestMessage); }