示例#1
0
        public ProxyUriDefinition CreateUriDefinition(ProxyDescriptor descriptor, string regionKey, string targetMethodName)
        {
            var uriBuilder = RoundRobinManager.RoundRobinUri(regionKey);

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

            var uriDefinition = new ProxyUriDefinition(uriBuilder);

            if (!string.IsNullOrEmpty(descriptor.Route))
            {
                if (targetMethodName.ToLower() == HttpMethod.Get.Method.ToLower())
                {
                    uriDefinition.UriBuilder.Path = $"{descriptor.Route}/";
                }
                else
                {
                    if (targetMethodName.StartsWith("/"))
                    {
                        targetMethodName = targetMethodName.Substring(1);
                    }

                    var routeTemplate = TemplateParser.Parse(targetMethodName);
                    uriDefinition.ResolveTemplate(routeTemplate, descriptor.Route, targetMethodName);
                }
            }

            return(uriDefinition);
        }
示例#2
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);
             }
         }
     }
 }
示例#3
0
        public async Task <RequestDescriptor> CreateDescriptorAsync(RequestContext context)
        {
            HttpRequestMessage request = CreateHttpRequest(context);
            var proxyDescriptor        = _typeManager.ProxyDescriptors.FirstOrDefault(x => x.ProxyType == context.ProxyType);

            if (proxyDescriptor == null)
            {
                throw new ArgumentOutOfRangeException("Proxy type could not be found!");
            }

            var regionKey = proxyDescriptor.RegionKey;

            ProxyMethodDescriptor methodDescriptor;

            if (!proxyDescriptor.Methods.TryGetValue(context.TargetMethod, out methodDescriptor))
            {
                throw new ArgumentOutOfRangeException("Method (Action) info could not be found!");
            }

            request.Method = methodDescriptor.HttpMethod;
            var methodPath = context.TargetMethod.Name;

            if (methodDescriptor.Template.HasValue())
            {
                methodPath = methodDescriptor.Template;
            }

            ProxyUriDefinition proxyUriDefinition = _endpointManager.CreateUriDefinition(proxyDescriptor, regionKey, methodPath);
            TimeSpan?          timeout            = methodDescriptor.Timeout;

            request.RequestUri = proxyUriDefinition.UriBuilder.Uri;
            await _streamProvider.CreateRequestContentAsync(context, request, methodDescriptor, proxyUriDefinition);

            return(new RequestDescriptor(request,
                                         methodDescriptor,
                                         proxyDescriptor.RegionKey,
                                         timeout));
        }
示例#4
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);
            }
        }