Пример #1
0
        public HttpClient(string host)
        {
            mHost = HttpHost.GetHttpHost(host);
            Uri uri = new Uri(host);

            RequestUrl = uri.PathAndQuery;
            if (string.IsNullOrEmpty(RequestUrl))
            {
                throw new HttpClientException("Request URL invalid!");
            }
        }
Пример #2
0
        public IApiNode Add(string host, int weight)
        {
            Uri url = new Uri(host);

            if (mClients.Find(c => c.Host == url.Host && c.Port == url.Port) == null)
            {
                var item = HttpHost.GetHttpHost(host);//new HttpHost(host);
                item.ID     = mID << mClients.Count;
                item.Weight = weight;
                item.MaxRPS = 0;
                mClients.Add(item);
                RefreshWeightTable();
            }
            return(this);
        }
Пример #3
0
        public ClientActionHanler(MethodInfo method)
        {
            MethodInfo    = method;
            Method        = "GET";
            Name          = method.Name;
            DeclaringType = method.DeclaringType;
            var host = DeclaringType.GetCustomAttribute <HostAttribute>(false);

            MethodType = MethodInfo.ReturnType;
            var mhost = method.GetCustomAttribute <HostAttribute>(false);

            if (mhost != null)
            {
                host = mhost;
            }
            if (host != null)
            {
                this.Host = HttpHost.GetHttpHost(host.Host);// new HttpHost(host.Host);
            }
            Async = false;
            if (MethodInfo.ReturnType != typeof(void))
            {
                if (MethodInfo.ReturnType.Name == "Task`1" || MethodInfo.ReturnType == typeof(Task))
                {
                    Async = true;
                    if (MethodInfo.ReturnType.IsGenericType)
                    {
                        ReturnType = MethodInfo.ReturnType.GetGenericArguments()[0];
                    }
                }
                else
                {
                    ReturnType = MethodInfo.ReturnType;
                }
            }
            foreach (HeaderAttribute h in DeclaringType.GetCustomAttributes <HeaderAttribute>())
            {
                if (!string.IsNullOrEmpty(h.Name) && !string.IsNullOrEmpty(h.Value))
                {
                    mHeaders[h.Name] = h.Value;
                }
            }

            foreach (HeaderAttribute h in method.GetCustomAttributes <HeaderAttribute>())
            {
                if (!string.IsNullOrEmpty(h.Name) && !string.IsNullOrEmpty(h.Value))
                {
                    mHeaders[h.Name] = h.Value;
                }
            }

            foreach (QueryAttribute q in DeclaringType.GetCustomAttributes <QueryAttribute>())
            {
                if (!string.IsNullOrEmpty(q.Name) && !string.IsNullOrEmpty(q.Value))
                {
                    mQueryString[q.Name] = q.Value;
                }
            }

            foreach (QueryAttribute q in method.GetCustomAttributes <QueryAttribute>())
            {
                if (!string.IsNullOrEmpty(q.Name) && !string.IsNullOrEmpty(q.Value))
                {
                    mQueryString[q.Name] = q.Value;
                }
            }

            Formater = method.GetCustomAttribute <FormaterAttribute>();
            if (Formater == null)
            {
                Formater = DeclaringType.GetCustomAttribute <FormaterAttribute>();
            }
            if (Formater == null)
            {
                Formater = new JsonFormater();
            }
            var get = method.GetCustomAttribute <GetAttribute>();

            if (get != null)
            {
                Method = Request.GET;
                if (!string.IsNullOrEmpty(get.Route))
                {
                    RouteTemplateMatch = new RouteTemplateMatch(get.Route);
                }
            }
            var post = method.GetCustomAttribute <PostAttribute>();

            if (post != null)
            {
                Method = Request.POST;
                if (!string.IsNullOrEmpty(post.Route))
                {
                    RouteTemplateMatch = new RouteTemplateMatch(post.Route);
                }
            }
            var del = method.GetCustomAttribute <DelAttribute>();

            if (del != null)
            {
                Method = Request.DELETE;
                if (!string.IsNullOrEmpty(del.Route))
                {
                    RouteTemplateMatch = new RouteTemplateMatch(del.Route);
                }
            }
            var put = method.GetCustomAttribute <PutAttribute>();

            if (put != null)
            {
                Method = Request.PUT;
                if (!string.IsNullOrEmpty(put.Route))
                {
                    RouteTemplateMatch = new RouteTemplateMatch(put.Route);
                }
            }
            Controller = this.DeclaringType.GetCustomAttribute <ControllerAttribute>();
            if (Controller != null)
            {
                if (!string.IsNullOrEmpty(Controller.BaseUrl))
                {
                    BaseUrl = Controller.BaseUrl;
                }
            }
            if (string.IsNullOrEmpty(BaseUrl))
            {
                BaseUrl = "/";
            }
            if (BaseUrl[0] != '/')
            {
                BaseUrl = "/" + BaseUrl;
            }
            if (BaseUrl.Substring(BaseUrl.Length - 1, 1) != "/")
            {
                BaseUrl += "/";
            }
            int index = 0;

            foreach (var p in method.GetParameters())
            {
                ClientActionParameter cap = new ClientActionParameter();
                cap.Name          = p.Name;
                cap.ParameterType = p.ParameterType;
                cap.Index         = index;
                index++;
                HeaderAttribute cHeader = p.GetCustomAttribute <HeaderAttribute>();
                if (cHeader != null)
                {
                    if (!string.IsNullOrEmpty(cHeader.Name))
                    {
                        cap.Name = cHeader.Name;
                    }
                    mHeaderParameters.Add(cap);
                }
                else
                {
                    QueryAttribute cQuery = p.GetCustomAttribute <QueryAttribute>();
                    if (cQuery != null)
                    {
                        if (!string.IsNullOrEmpty(cQuery.Name))
                        {
                            cap.Name = cQuery.Name;
                        }
                        mQueryStringParameters.Add(cap);
                    }
                    else
                    {
                        if (RouteTemplateMatch != null && RouteTemplateMatch.Items.Find(i => i.Name == p.Name) != null)
                        {
                            mRouteParameters.Add(cap);
                        }
                        else
                        {
                            mDataParameters.Add(cap);
                        }
                    }
                }
            }
        }
Пример #4
0
 internal HttpApiClient(string host)
 {
     Host = HttpHost.GetHttpHost(host); //new HttpHost(host);
 }