Example #1
0
        private IEnumerator GenMethodCache()
        {
            RestMethodInfo restMethodInfo = null;

            foreach (MethodInfo methodInfo in iRestInterface.GetMethods())
            {
                if (!methodInfoCache.TryGetValue(methodInfo.ToString(), out restMethodInfo))
                {
                    restMethodInfo = ParseRequestInfoByAttribute(methodInfo);
                    methodInfoCache.Add(methodInfo.ToString(), restMethodInfo);
                }
            }
            genMethodInfoComplete = true;
            yield return(null);
        }
Example #2
0
        public IObservable <T> CreateRequestObservable <T>(RestMethodInfo methodInfo, string url, object[] arguments)
        {
            var ob = Observable.Create <T>(o =>
            {
                object request = rxHttpImpl.RxBuildRequest(o, convert, methodInfo, url);
                if (interceptor != null)
                {
                    interceptor.Intercept(request);
                }
                rxHttpImpl.RxSendRequest(o, convert, request);
                return(Disposable.Create((() => rxHttpImpl.Cancel(request))));
            });

            return(ob);
        }
Example #3
0
        protected IEnumerator Request <T>(RestMethodInfo methodInfo, string url, object[] arguments, Callback <T> cb)
        {
            object request = httpImpl.BuildRequest(methodInfo, url);

            InterceptRequest(request);
            CoroutineWithData cd = new CoroutineWithData(this, httpImpl.SendRequest(this, request));

            yield return(cd.coroutine);

            string errorMessage;

            if (httpImpl.IsRequestError(cd.result, out errorMessage))
            {
                cb.errorCB(errorMessage);
                yield break;
            }
            string result = httpImpl.GetSuccessResponse(cd.result);

            //                        result = "[]";
            //                        result = "[asd..s]";
            Log("Response:" + result);

            //Parse Json By Type
            if (typeof(T) == typeof(string))
            {
                var response = (T)(object)result;
                cb.successCB(response);
                yield break;
            }
            T    data        = default(T);
            bool formatError = false;

            try
            {
                data = convert.FromBody <T>(result);
            }
            catch (ConversionException e)
            {
                formatError = true;
                cb.errorCB(e.Message);
            }
            if (!formatError)
            {
                cb.successCB(data);
            }
        }
        private RestMethodInfo GetRestMethodInfo(MethodBase method)
        {
            RestMethodInfo methodInfo = null;

            if (!genMethodInfoComplete && !methodInfoCache.TryGetValue(method.ToString(), out methodInfo))
            {
                //gen cache has not completed and can't be found in cache, just gen and use, but not add to cache;
                methodInfo = ParseRequestInfoByAttribute(method);
            }
            else if (genMethodInfoComplete && !methodInfoCache.TryGetValue(method.ToString(), out methodInfo))
            {
                //gen cache has completed and can't be found in cache, something was wrong!
                Log("RestAdapter Gen Cache Process Error!!!!");
                methodInfo = ParseRequestInfoByAttribute(method);
                methodInfoCache.Add(method.ToString(), methodInfo);
            }
            return(methodInfo);
        }
Example #5
0
        protected void ParseParameters(MethodBase methodInfo, RestMethodInfo info)
        {
            foreach (ParameterInfo parameter in methodInfo.GetParameters())
            {
                var attribute = (ValueAttribute)parameter.GetCustomAttributes(false).FirstOrDefault();


                if (attribute == null)
                {
                    bool isCallback = parameter.ParameterType.IsGenericType && parameter.ParameterType.GetGenericTypeDefinition() == typeof(Callback <>);
                    if (!isCallback)
                    {
                        Log("No annotation found on parameter " + parameter.Name + " of " + methodInfo.Name);
                    }
                    continue;
                }
                var type = attribute.GetType();
                if (type == typeof(PathAttribute))
                {
                    info.ParameterUsage.Add(RestMethodInfo.ParamUsage.Path);
                    info.ParameterNames.Add(attribute.Value);
                }
                else if (type == typeof(BodyAttribute))
                {
                    info.ParameterUsage.Add(RestMethodInfo.ParamUsage.Body);
                    info.ParameterNames.Add(null);
                    info.HasBody = true;
                }
                else if (type == typeof(QueryAttribute))
                {
                    info.ParameterUsage.Add(RestMethodInfo.ParamUsage.Query);
                    info.ParameterNames.Add(attribute.Value);
                }
                else if (type == typeof(QueryMapAttribute))
                {
                    info.ParameterUsage.Add(RestMethodInfo.ParamUsage.QueryMap);
                    info.ParameterNames.Add(attribute.Value);
                }
                else if (type == typeof(FieldAttribute))
                {
                    info.ParameterUsage.Add(RestMethodInfo.ParamUsage.Field);
                    info.ParameterNames.Add(attribute.Value);
                }
                else if (type == typeof(HeaderAttribute))
                {
                    info.ParameterUsage.Add(RestMethodInfo.ParamUsage.Header);
                    info.ParameterNames.Add(attribute.Value);
                }
                else if (type == typeof(PartAttribute))
                {
                    if (!info.IsMultipart)
                    {
                        //rule1:[Part] parameters can only be used with multipart encoding
                        throw new ArgumentException("[Part] parameters can only be used with multipart encoding.");
                    }
                    if (parameter.ParameterType == typeof(FileInfo) ||
                        parameter.ParameterType == typeof(MultipartBody))
                    {
                        info.ParameterUsage.Add(RestMethodInfo.ParamUsage.Part);
                        info.ParameterNames.Add(null);
                    }
                    else
                    {
                        //rule2:[Part] parameter type can only be FileInfo or string or MultipartBody
                        throw new Exception("[Part] parameter type can only be FileInfo or string or MultipartBody.");
                    }
                }
            }
            if (info.IsMultipart)
            {
                var partParams = methodInfo.GetParameters()
                                 .Select(x => new { Parameter = x, PartAttribute = x.GetCustomAttributes(true).OfType <PartAttribute>().FirstOrDefault() })
                                 .Where(x => x.PartAttribute != null)
                                 .ToList();
                if (partParams.Count > 1)
                {
                    //rule3:Multipart requests can only contain One Part parameter
                    throw new ArgumentException("Multipart requests can only contain One Part parameter at most");
                }
                if (partParams.Count < 1)
                {
                    //rule4:Multipart must contain Part parameter
                    throw new ArgumentException("Multipart must contain Part parameter");
                }
                if (info.HasBody)
                {
                    //rule5:Multipart requests may not contain a Body parameter
                    throw new ArgumentException("Multipart requests may not contain a Body parameter");
                }
                info.GotPart = true;
            }
        }
Example #6
0
        private string ParseRestParameters <T>(RestMethodInfo methodInfo, object[] arguments)
        {
            string url = baseUrl + methodInfo.Path;
            //if param has QUERY attribute, add the fileds(POST) or URL parameteters(GET)
            //if param has PATH attribute, parse the URL
            List <string> pathParamNames         = new List <string>();
            List <object> pathParamArgs          = new List <object>();
            List <string> queryParamNames        = new List <string>();
            List <object> queryParamArgs         = new List <object>();
            List <string> fieldParamNames        = new List <string>();
            List <object> fieldParamArgs         = new List <object>();
            List <string> headerParamNames       = new List <string>();
            List <string> headerParamArgs        = new List <string>();
            object        body                   = null;
            object        part                   = null;
            Dictionary <string, string> queryMap = new Dictionary <string, string>();
            int i = 0;

            foreach (var paramType in methodInfo.ParameterUsage)
            {
                if (paramType == RestMethodInfo.ParamUsage.Path)
                {
                    pathParamNames.Add(methodInfo.ParameterNames[i]);
                    pathParamArgs.Add(arguments[i]);
                }
                if (paramType == RestMethodInfo.ParamUsage.Query)
                {
                    queryParamNames.Add(methodInfo.ParameterNames[i]);
                    queryParamArgs.Add(arguments[i]);
                }
                if (paramType == RestMethodInfo.ParamUsage.Field)
                {
                    fieldParamNames.Add(methodInfo.ParameterNames[i]);
                    fieldParamArgs.Add(arguments[i]);
                }
                if (paramType == RestMethodInfo.ParamUsage.Body)
                {
                    body = arguments[i];
                }
                if (paramType == RestMethodInfo.ParamUsage.QueryMap)
                {
                    queryMap = arguments[i] as Dictionary <string, string>;
                }
                if (paramType == RestMethodInfo.ParamUsage.Header)
                {
                    headerParamNames.Add(methodInfo.ParameterNames[i]);
                    headerParamArgs.Add(arguments[i] as string);
                }
                if (methodInfo.IsMultipart && paramType == RestMethodInfo.ParamUsage.Part)
                {
                    part = arguments[i];
                }
                i++;
            }
            //replace PATH in URL
            if (pathParamNames.Count > 0)
            {
                string tmpUrl = url;
                int    j      = 0;
                foreach (var paramName in pathParamNames)
                {
                    tmpUrl = tmpUrl.Replace("{" + paramName + "}", "{" + j + "}");
                    j++;
                }
                tmpUrl = string.Format(tmpUrl, pathParamArgs.ToArray());
                url    = tmpUrl;
                Log("parse PATH url:" + url);
            }
            //add QUERY in URL
            bool hasQuery = false;

            if (queryParamNames.Count > 0)
            {
                hasQuery = true;
                StringBuilder sb = new StringBuilder(url);
                sb.Append("?");
                int l = 0;
                foreach (var queryParamName in queryParamNames)
                {
                    if (l > 0)
                    {
                        sb.Append("&");
                    }
                    sb.Append(queryParamName);
                    sb.Append("=");
                    sb.Append(queryParamArgs[l]);
                    l++;
                }
                url = sb.ToString();
                Log("parse QUERY url:" + url);
            }
            //add QUERYMAP in URL
            if (queryMap != null && (methodInfo.Method == Method.Get && queryMap.Count > 0))
            {
                StringBuilder sb = new StringBuilder(url);
                if (!hasQuery)
                {
                    sb.Append("?");
                }
                int l = 0;
                foreach (KeyValuePair <string, string> keyValuePair in queryMap)
                {
                    if (l > 0 || hasQuery)
                    {
                        sb.Append("&");
                    }
                    sb.Append(keyValuePair.Key);
                    sb.Append("=");
                    sb.Append(keyValuePair.Value);
                    l++;
                }
                url = sb.ToString();
                Log("parse QUERY-MAP url:" + url);
            }
            //add field to RestMethodInfo
            methodInfo.FieldParameterMap = BuildFieldParameterMap(fieldParamNames, fieldParamArgs);
            //convert body to string, and add to RestMethodInfo
            string bodyString = body != null?convert.ToBody(body) : string.Empty;

            methodInfo.bodyString = bodyString;
            //add part
            methodInfo.Part = MultipartBody.Convert(part);
            //add patamter header to RestMethodInfo
            methodInfo.HeaderParameterMap = BuildHeaderParameterMap(headerParamNames, headerParamArgs);
            return(url);
        }
Example #7
0
 private void ParseMethodHeaders(MethodInfo mi, RestMethodInfo methodInfo)
 {
     methodInfo.Headers = ParseHeaders(mi);
 }
 private void CoroutineRequest <T>(RestMethodInfo methodInfo, string url, object[] args, Callback <T> cb)
 {
     StartCoroutine(Request(methodInfo, url, args, cb));
 }