public RequestDataCreator(object target, ApiStructure apiStructure, ApiMethodInfo apiMethod, object[] arguments, ApiData apiData) { _target = target; _apiStructure = apiStructure; _apiMethod = apiMethod; _arguments = arguments; _apiData = apiData; }
private void SetMethodReturnType(ApiMethodInfo apiMethodInfo) { if (_methodInfo.ReturnType.IsGenericType && _methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(Task <>)) { var genericArgument = _methodInfo.ReturnType.GetGenericArguments().First(); if (genericArgument.IsGenericParameter) { apiMethodInfo.ReturnType = ApiMethodReturnType.GenericValue; return; } if (typeof(XmlRpcValue).IsAssignableFrom(genericArgument)) { apiMethodInfo.ReturnType = ApiMethodReturnType.XmlRpcValue; apiMethodInfo.ValueType = genericArgument; return; } if (genericArgument == typeof(string) || genericArgument.IsValueType) { apiMethodInfo.ReturnType = ApiMethodReturnType.Value; apiMethodInfo.ValueType = genericArgument; return; } // ReSharper disable once ConvertIfStatementToSwitchStatement if (genericArgument.IsGenericType && genericArgument.GetGenericTypeDefinition() == typeof(IDictionary <,>)) { apiMethodInfo.ReturnType = ApiMethodReturnType.Dictionary; apiMethodInfo.ValueType = genericArgument.GetGenericArguments().Last(); return; } if (genericArgument.IsGenericType && genericArgument.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { apiMethodInfo.ReturnType = ApiMethodReturnType.Enumerable; apiMethodInfo.ValueType = genericArgument.GetGenericArguments().First(); return; } if (genericArgument.IsClass || genericArgument.IsInterface) { apiMethodInfo.ReturnType = ApiMethodReturnType.ObjectValue; apiMethodInfo.ValueType = genericArgument; return; } } if (_methodInfo.ReturnType != typeof(Task)) { throw new WrongReturnTypeException(_methodInfo); } apiMethodInfo.ReturnType = ApiMethodReturnType.Void; }
public ApiMethodInfo Analyze() { var apiMethodInfo = new ApiMethodInfo { Method = _methodInfo, MethodName = GetMethodName(), DefaultResult = _methodAttr.DefaultResult, ExceptionHandler = GetExceptionHandler() }; SetMethodReturnType(apiMethodInfo); return(apiMethodInfo); }
public ApiMethodInfo GetMethodInfo() { var methodReturnType = GetMethodReturnType(); var headerDefinitions = GetHeaderDefinitions().ToArray(); var apiMethodInfo = new ApiMethodInfo { Method = _methodInfo, ReturnType = methodReturnType, RequestMethod = _apiMethodAttribute.RequestMethod, HeaderDefinitions = headerDefinitions, MethodUri = _apiMethodAttribute.Uri, DefaultCompletionOption = _apiMethodAttribute.GetCompletionOption(HttpCompletionOption.ResponseContentRead) }; apiMethodInfo.ArgumentInfos = new ApiCallArgumentsAnalyzer(apiMethodInfo).Analyze(_methodInfo).ToArray(); return(apiMethodInfo); }
static Utils() { foreach (MethodInfo method in typeof(WebApiController).GetMethods()) { ApiMethodAttribute customAttribute = method.GetCustomAttribute <ApiMethodAttribute>(); if (customAttribute != null) { Type parameterType = ((IEnumerable <ParameterInfo>)method.GetParameters()).First <ParameterInfo>().ParameterType; IRequest <IResponse> request = (IRequest <IResponse>)Activator.CreateInstance(parameterType); ApiMethodInfo apiMethodInfo = new ApiMethodInfo() { ApiName = request.ApiName, IsCheckSession = customAttribute.IsCheckSession, Method = method, RequestType = parameterType }; Utils.apiMethods.Add(apiMethodInfo.ApiName, apiMethodInfo); } } }
public object Execute(ApiMethodInfo apiMethodInfo, IInvocation invocation) { var requestHandler = _requestHandlers.FirstOrDefault(x => x.ReturnType == apiMethodInfo.ReturnType); if (requestHandler == null) { throw new ReturnTypeNotSupportedException(apiMethodInfo.ReturnType); } return(requestHandler.HandleRequest( new RequestData { ValueType = apiMethodInfo.ValueType, Arguments = invocation.Arguments, Client = _xmlRpcClient, InvocationMethod = invocation.Method, MethodName = apiMethodInfo.MethodName, ExceptionHandler = apiMethodInfo.ExceptionHandler, DefaultResult = apiMethodInfo.DefaultResult })); }
public ApiCallArgumentsAnalyzer(ApiMethodInfo apiMethodInfo) { _apiMethodInfo = apiMethodInfo; }
public object Invoke(object target, ApiMethodInfo apiMethod, object[] arguments, ApiData apiData) { var requestData = new RequestDataCreator(target, _apiStructure, apiMethod, arguments, apiData).Create(); return(ExecuteRequest(requestData)); }
public ActionResult Index() { IResponse response; try { string appKey = null; string key = null; string str = null; string strB = null; string accessToken = null; DateTime dateTime = DateTime.MinValue; NameValueCollection form = this.Request.Form; Dictionary <string, string> dictionary = new Dictionary <string, string>(); foreach (string allKey in form.AllKeys) { string s = form[allKey]; switch (allKey) { case "app_key": appKey = s; break; case "param_json": str = s; break; case "method": key = s; break; case "timestamp": long result; if (long.TryParse(s, out result)) { dateTime = new DateTime(1970, 1, 1).ToLocalTime().AddMilliseconds((double)result); break; } break; case "sign": strB = s; break; case "access_token": accessToken = s; break; } if (allKey != "sign") { dictionary.Add(allKey, s); } } if (dateTime < DateTime.Now.AddMinutes(-15.0)) { response = new BaseResponse() { ErrCode = "005" }; } else { AppInfo app = ServiceHelper.LoadService <IAppService>().GetApp(appKey); if (app != null) { if (string.Compare(WebApiUtils.SignYrqRequest(dictionary, app.AppSecret), strB, true) == 0) { Dictionary <string, ApiMethodInfo> apiMethods = Util.Utils.GetApiMethods(); if (apiMethods.ContainsKey(key)) { ApiMethodInfo apiMethodInfo = apiMethods[key]; IRequest <IResponse> request = (IRequest <IResponse>)JsonConvert.DeserializeObject(str ?? "{}", apiMethodInfo.RequestType, WebApiUtils.GetJsonConverters()); request.Validate(); this.Context.AppId = app.AppId; if (request is ICraftsReqeust) { ServiceHelper.LoadService <ICraftDbFactory>().CraftNO = ((ICraftsReqeust)request).CraftNO; } if (request is IUploadRequest) { IUploadRequest uploadRequest = (IUploadRequest)request; IDictionary <string, FileItem> fileParameters = new Dictionary <string, FileItem>(); foreach (string allKey in this.Request.Files.AllKeys) { HttpPostedFileBase httpPostedFileBase = this.Request.Files[allKey]; byte[] numArray = new byte[httpPostedFileBase.InputStream.Length]; httpPostedFileBase.InputStream.Read(numArray, 0, numArray.Length); fileParameters.Add(allKey, new FileItem(httpPostedFileBase.FileName, numArray)); httpPostedFileBase.InputStream.Dispose(); } uploadRequest.SetFileParamaters(fileParameters); } if (apiMethodInfo.IsCheckSession) { AuthInfo auth = ServiceHelper.LoadService <IAuthService>().GetAuth(accessToken); if (auth != null && auth.AppId == this.Context.AppId) { this.Context.AuthId = auth.AppId; this.Context.UserId = auth.UserId; response = (IResponse)apiMethodInfo.Method.Invoke(this, new object[1] { request }); } else { response = new BaseResponse() { ErrCode = "004" } }; } else { response = (IResponse)apiMethodInfo.Method.Invoke(this, new object[1] { request }); } } else { response = new BaseResponse() { ErrCode = "003" } }; } else { response = new BaseResponse() { ErrCode = "002" } }; } else { response = new BaseResponse() { ErrCode = "008" } }; } } catch (TargetInvocationException ex) { LogUtil.LogError("处理请求异常", ex.GetBaseException()); response = new BaseResponse() { ErrCode = "001", ErrMsg = ex.GetBaseException().Message }; } catch (Exception ex) { LogUtil.LogError("处理请求异常", ex); response = new BaseResponse() { ErrCode = "001", ErrMsg = ex.Message }; } return(Content(JsonConvert.SerializeObject(response, WebApiUtils.GetJsonConverters()))); }