Ejemplo n.º 1
0
        public WsModel <string, Tresponse> Invoke <Tresponse>(string requestUri, string data)
        {
            WsModel <string, Tresponse> model = new WsModel <string, Tresponse>();

            model.Request = data;
            var api = QuickWebApiFactory.Instance.Get(_service);

            if (api == null)
            {
                model.ERROR(-9999998, string.Format("未找到{0}->{1}的webapi配置", _service, requestUri));
                return(model);
            }
            if (string.IsNullOrWhiteSpace(api.Uri))
            {
                model.ERROR(-9999997, "未指定服务地址");
                return(model);
            }
            if (string.IsNullOrWhiteSpace(api.Url(requestUri)))
            {
                model.ERROR(-9999996, "未指定接口路由");
                return(model);
            }
            using (WebApiClient client = new WebApiClient(api.Uri))
            {
                var mtd = api.Method(requestUri);
                return(client.Invoke <Tresponse>(api.Url(requestUri), data, mtd.Method));
            }
        }
Ejemplo n.º 2
0
        protected WsModel <string, Tresponse> _invoke <T, Tresponse>(Expression exp, params object[] args)
        {
            var    method = ((exp as UnaryExpression).Operand as MethodCallExpression);
            string code   = ((method.Object as ConstantExpression).Value as MethodInfo).Name;

            foreach (var m in method.Arguments)
            {
                if (m.Type == typeof(T))
                {
                    var           attr = m.Type.GetCustomAttribute <WebApiAttribute>();
                    StringBuilder sb   = new StringBuilder();
                    var           pis  = m.Type.GetMethod(code).GetParameters();

                    for (int i = 0; i < pis.Length; i++)
                    {
                        sb.AppendFormat("{0}={1}&", pis[i].Name, args[i] is DateTime ? ((DateTime)args[i]).ToString("yyyy-MM-dd HH:mm:ss") : args[i]);
                    }

                    if (attr != null)
                    {
                        return(new Invoker(build_server(attr.service)).Invoke <Tresponse>(code, sb.ToString()));
                    }
                }
            }
            var model = new WsModel <string, Tresponse>();

            model.ERROR(-9999999, "未能找到合适的api定义");
            return(model);
        }
Ejemplo n.º 3
0
        public WsModel Invoke(string requestUri, WsModel model, MethodType mtd)
        {
            using (var client = new HttpClient())
            {
                Append_Header(client);
                HttpResponseMessage ret = null;
                client.BaseAddress = _uri;
                BuildHeader(client.DefaultRequestHeaders, client.BaseAddress.Authority, requestUri, "POST");
                if (mtd == MethodType.HTTPPOST)
                {
                    ret = client.PostAsJsonAsync(requestUri, model).Result;
                }
                else if (mtd == MethodType.HTTPPUT)
                {
                    ret = client.PutAsJsonAsync(requestUri, model).Result;
                }
                else
                {
                    if (model == null)
                    {
                        model = new WsModel();
                    }
                    model.ERROR(-9999990, string.Format("{0}/{1}未配置{2}请求", _uri.AbsoluteUri, requestUri, mtd.ToString()));
                    return(model);
                }

                return(HttpResponseMessage2WSModel(ret));
            }
        }
Ejemplo n.º 4
0
        public WsModel <string, Tresponse> Invoke <Tresponse>(string requestUri, string data, MethodType mtd)
        {
            WsModel <string, Tresponse> model = null;

            using (var client = new HttpClient())
            {
                Append_Header(client);
                HttpResponseMessage ret = null;
                client.BaseAddress = _uri;
                BuildHeader(client.DefaultRequestHeaders, client.BaseAddress.Authority, requestUri, "POST");
                if (mtd == MethodType.HTTPGET)
                {
                    ret = client.GetAsync(string.Format("{0}?{1}", requestUri, data)).Result;
                }
                else if (mtd == MethodType.HTTPPOST)
                {
                    ret = client.PostAsync(string.Format("{0}?{1}", requestUri, data), new StringContent(string.Empty)).Result;
                }
                else if (mtd == MethodType.HTTPDEL)
                {
                    ret = client.DeleteAsync(string.Format("{0}?{1}", requestUri, data)).Result;
                }
                else
                {
                    model = new WsModel <string, Tresponse>();
                    model.ERROR(-9999990, string.Format("{0}/{1}未配置{2}请求", _uri.AbsoluteUri, requestUri, mtd.ToString()));
                    return(model);
                }
                model         = HttpResponseMessage2WSModel <string, Tresponse>(ret);
                model.Request = data;// string.Format("{0}:{1}/{2}", mtd.ToString(), _uri.AbsoluteUri, requestUri);
                return(model);
            }
        }
Ejemplo n.º 5
0
        public static WsModel ApiModel(this HttpContextBase context, string devicecode = "web")
        {
            WsModel model = new WsModel();

            model.Client = context.GetClientInfo(devicecode);// new Client(context.GetIP(), null, null, devicecode, context.Request.Browser.Id);
            model.User   = context.GetUser();
            model.Secret = context.GetSecret();
            return(model);
        }
Ejemplo n.º 6
0
        public string ValidRequest(string requestdata)
        {
            WsModel model = Prepare(requestdata);

            if (model == null)
            {
                return("Unauthorized Data");
            }
            return(ValidAccessToken(model.User.SysCode, model.Client.Ip, model.Secret.AccessToken) ??
                   ValidUser(model.User.Ticket, model.User.Uid, model.User.SessionId));
        }
Ejemplo n.º 7
0
 public WsModel HttpResponseMessage2WSModel(HttpResponseMessage response)
 {
     if (response == null)
     {
         WsModel model = new WsModel();
         model.ERROR(-9999990, "Null HttpResponseMessage");
         return(model);
     }
     if (!response.IsSuccessStatusCode)
     {
         WsModel model = new WsModel();
         model.ERROR((int)response.StatusCode, response.ReasonPhrase);
         return(model);
     }
     return(response.Content.ReadAsAsync <WsModel>().Result);
 }
Ejemplo n.º 8
0
        protected WsModel <Trequest, Tresponse> _invoke_data <T, Trequest, Tresponse>(Expression exp, WsModel <Trequest, Tresponse> model)
        {
            var    method = ((exp as UnaryExpression).Operand as MethodCallExpression);
            string code   = ((method.Object as ConstantExpression).Value as MethodInfo).Name;

            foreach (var m in method.Arguments)
            {
                if (m.Type == typeof(T))
                {
                    var attr = m.Type.GetCustomAttribute <WebApiAttribute>();
                    if (attr != null)
                    {
                        //if (model.Request.GetType().IsValueType || model.Request is string)
                        //{
                        //    //var ret = _invoke<T, Tresponse>(exp, model.Request);
                        //    var ret = new Invoker(build_server(attr.service)).Invoke<Tresponse>(code, model.Request);
                        //    model = new WsModel<Trequest, Tresponse>();
                        //    model.Response = ret.Response; ;
                        //    model.User = ret.User;
                        //    model.Secret = ret.Secret;
                        //    model.Client = ret.Client;
                        //    model.Time = ret.Time;
                        //    model.Signature = ret.Signature;
                        //    model.ErrCode = ret.ErrCode;
                        //    model.ErrMsg = ret.ErrMsg;
                        //    return model;
                        //}
                        return(new Invoker(build_server(attr.service)).Invoke <Trequest, Tresponse>(code, model));
                    }
                }
            }
            if (model == null)
            {
                model = new WsModel <Trequest, Tresponse>();
            }
            model.ERROR(-9999999, "未能找到合适的api定义");
            return(model);
        }
Ejemplo n.º 9
0
        protected WsModel <Trequest> _invoke_data <T, Trequest>(Expression exp, WsModel <Trequest> model)
        {
            var    method = ((exp as UnaryExpression).Operand as MethodCallExpression);
            string code   = ((method.Object as ConstantExpression).Value as MethodInfo).Name;

            foreach (var m in method.Arguments)
            {
                if (m.Type == typeof(T))
                {
                    var attr = m.Type.GetCustomAttribute <WebApiAttribute>();
                    if (attr != null)
                    {
                        return(new Invoker(build_server(attr.service)).Invoke <Trequest>(code, model));
                    }
                }
            }
            if (model == null)
            {
                model = new WsModel <Trequest>();
            }
            model.ERROR(-9999999, "未能找到合适的api定义");
            return(model);
        }
Ejemplo n.º 10
0
 public WsModel <Trequest, Tresponse> invoke <T, Trequest, Tresponse>(Expression <Func <T, ApiActionO <WsModel <Trequest, Tresponse> > > > func,
                                                                      WsModel <Trequest, Tresponse> model)
 {
     return(_invoke_data <T, Trequest, Tresponse>(func.Body, model));
 }