예제 #1
0
        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);
        }
예제 #2
0
        public RequestContext(HttpRequestMessage request,
                              ProxyMethodDescriptor methodDescriptor,
                              string regionKey,
                              TimeSpan?timeout = null)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (methodDescriptor == null)
            {
                throw new ArgumentNullException(nameof(methodDescriptor));
            }

            if (!regionKey.HasValue())
            {
                throw new ArgumentNullException(nameof(regionKey));
            }

            Request          = request;
            MethodDescriptor = methodDescriptor;
            RegionKey        = regionKey;
            Timeout          = timeout;
        }
예제 #3
0
        public UriBuilder CreateUriBuilder(ProxyMethodDescriptor methodDescriptor, string route, string regionKey, string targetMethodName)
        {
            var uriBuilder = RoundRobinManager.RoundRobinUri(regionKey);

            if (uriBuilder == null)
            {
                throw new ArgumentNullException(nameof(uriBuilder));
            }

            if (targetMethodName.ToLower() == HttpMethod.Get.Method.ToLower())
            {
                var path = uriBuilder.Path ?? string.Empty;
                path           += string.IsNullOrEmpty(route) ? "" : $"{route}/";
                uriBuilder.Path = path;
            }
            else
            {
                if (targetMethodName.StartsWith("/"))
                {
                    targetMethodName = targetMethodName.Substring(1);
                }

                ResolveTemplate(uriBuilder, methodDescriptor, route, targetMethodName);
            }

            return(uriBuilder);
        }
예제 #4
0
        private void ResolveTemplate(UriBuilder uriBuilder, ProxyMethodDescriptor methodDescriptor, string route, string targetMethodName)
        {
            var path = uriBuilder.Path ?? string.Empty;

            if (methodDescriptor.RouteTemplate != null)
            {
                if (methodDescriptor.RouteTemplate.Parameters.Count > 0)
                {
                    var methodName = string.Join("/", methodDescriptor.TemplateKeys);
                    path           += ConcatRoute(route, methodName);
                    uriBuilder.Path = path;
                    return;
                }
            }

            path           += ConcatRoute(route, targetMethodName);
            uriBuilder.Path = path;
        }
예제 #5
0
 protected virtual void EnsureTemplate(ProxyMethodDescriptor descriptor,
                                       ProxyUriDefinition proxyUriDefinition,
                                       RequestContext requestContext,
                                       IDictionary <string, object> argsDic,
                                       List <string> keys)
 {
     if (descriptor.Template.HasValue())
     {
         if (proxyUriDefinition.HasParameter)
         {
             for (int i = 0; i < proxyUriDefinition.ParameterParts.Count; i++)
             {
                 var key = keys[i];
                 proxyUriDefinition.UriBuilder.Path += ($"/{WebUtility.UrlEncode(requestContext.Args[i]?.ToString())}");
                 argsDic.Remove(key);
             }
         }
     }
 }
        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;
        }
예제 #7
0
        public async Task CreateRequestContentAsync(RequestContext requestContext,
                                                    HttpRequestMessage request,
                                                    ProxyMethodDescriptor descriptor,
                                                    ProxyUriDefinition proxyUriDefinition)
        {
            await Task.CompletedTask;

            var uriBuilder = proxyUriDefinition.UriBuilder;
            var argsDic    = descriptor.Resolve(requestContext.Args);
            var argsCount  = argsDic.Count;
            var keys       = new List <string>(argsDic.Keys);

            if (descriptor.HttpMethod == HttpMethod.Post)
            {
                if (descriptor.IsMultiPartFormData)
                {
                    request.Content = GetMultipartFormDataContent(argsDic);
                    return;
                }

                if (argsCount == 1)
                {
                    request.Content = SerializeToString(argsDic.First().Value);
                }
                else
                {
                    request.Content = SerializeToString(argsDic);
                }

                return;
            }
            else if (descriptor.HttpMethod == HttpMethod.Put)
            {
                EnsureTemplate(descriptor, proxyUriDefinition, requestContext, argsDic, keys);
                argsCount          = argsDic.Count;
                request.RequestUri = uriBuilder.Uri;
                if (argsCount == 1)
                {
                    request.Content = SerializeToString(argsDic.First().Value);
                }
                else if (argsCount == 2)
                {
                    var firstParameter  = argsDic[keys[0]];
                    var secondParameter = argsDic[keys[1]];

                    // PUT Request first parameter should be Id or Key
                    if (firstParameter.GetType().IsPrimitive())
                    {
                        uriBuilder.Query += string.Format("&{0}={1}", keys[0], firstParameter);
                    }

                    request.RequestUri = uriBuilder.Uri;
                    request.Content    = SerializeToString(secondParameter);
                }
            }
            if (descriptor.HttpMethod == HttpMethod.Get || descriptor.HttpMethod == HttpMethod.Delete)
            {
                EnsureTemplate(descriptor, proxyUriDefinition, requestContext, argsDic, keys);
                request.RequestUri = QueryStringResolver.Parse(uriBuilder, argsDic);
            }
        }
예제 #8
0
 public ContentModelBindingContext(HttpMethod httpMethod, ProxyMethodDescriptor methodDescriptor, UriBuilder uriBuilder)
 {
     HttpMethod       = httpMethod ?? throw new ArgumentNullException(nameof(httpMethod));
     MethodDescriptor = methodDescriptor ?? throw new ArgumentNullException(nameof(methodDescriptor));
     UriBuilder       = uriBuilder ?? throw new ArgumentNullException(nameof(uriBuilder));
 }