public static string getURL(this APITypes type) { switch (type) { case APITypes.SignUp: return(REGISTER_API); case APITypes.SignIn: return(LOGIN_API); case APITypes.CreateSong: return(SONG_API); case APITypes.GetSongs: return(SONG_API); case APITypes.GetSavedSongs: return(MY_SONG_API); case APITypes.GetInfo: return(MY_INFO_API); case APITypes.GetUpload: return(UPLOAD_API); default: return(""); } }
public async static Task <string> Call(APITypes type, T item) { string URL = APIExtensions.getURL(type); HttpClient httpClient = new HttpClient(); if (APIExtensions.isTokenNeeded(type)) { httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + await FileHandle.GetToken()); } if (item == null) { string content = await httpClient.GetAsync(URL).Result.Content.ReadAsStringAsync(); Debug.WriteLine("GET: " + content); return(content); // HTTP GET } try { string data = JsonConvert.SerializeObject(item); string content = await httpClient.PostAsync(URL, new StringContent(data, System.Text.Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync(); Debug.WriteLine("POST: " + content); return(content); } catch (JsonSerializationException) { ExceptionHandle.ThrowDebug("json error"); return(null); } }
public void TestSearchOptionAccessEndpoint(APITypes type, HttpMethod method, AccessState accessState, HttpStatusCode httpStatusCode, ResultCode resultCode) { AccessEndpointManager accessEndpointHandler = new AccessEndpointManager(); var endpoint = "/privateapi/file/" + FileHelper.APIMapper[type]; var response = accessEndpointHandler.AccessEndpoint <object>(accessState, endpoint, new Dictionary <string, string>(), method.ToNetHttpMethod()); PrAssert.That(response, PrIs.ErrorResponse().And.HttpCode(httpStatusCode).And.ErrorCode((int)resultCode)); }
public static bool isTokenNeeded(this APITypes type) { switch (type) { case APITypes.CreateSong: case APITypes.GetSongs: case APITypes.GetSavedSongs: case APITypes.GetInfo: return(true); default: return(false); } }
/// <summary> /// 扩展方法:获取API类型的编码 /// </summary> /// <param name="pCaller"></param> /// <returns></returns> public static string ToCode(this APITypes pCaller) { switch (pCaller) { case APITypes.Demo: return("Demo"); case APITypes.Product: return("Product"); case APITypes.Project: return("Project"); default: throw new NotImplementedException(); } }
/// <summary> /// 获取处理器 /// </summary> /// <param name="pType">接口类型</param> /// <param name="pAction">接口请求操作</param> /// <param name="pVersion">接口版本号</param> /// <returns>接口请求处理器</returns> public static IActionHandler GetActionHandler(APITypes pType, string pAction) { var sections = pAction.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); switch (pType) { case APITypes.Product: #region 产品接口处理器创建 { if (sections.Length < 3) { throw new APIException(ERROR_CODES.INVALID_REQUEST_INVALID_ACTION_FORMAT, "action值的格式错误,在产品接口下,action的格式为:[模块名].[对象名].[操作名]"); } var className = string.Format("{0}.{1}.{2}.{3}AH", ActionHandlerFactory.PRODUCT_ACTION_HANDLER_BASE_NAMESPACE, sections[0], sections[1], sections[2]); IActionHandler handler = null; try { var oh = Activator.CreateInstance(null, className); if (oh != null) { handler = oh.Unwrap() as IActionHandler; } } catch (Exception ex) { throw new APIException(ERROR_CODES.INVALID_REQUEST_CAN_NOT_FIND_ACTION_HANDLER, string.Format("根据action找不到指定的ActionHandler类.原因:[{0}]", ex.Message), ex); } // return(handler); } #endregion case APITypes.Project: #region 项目接口处理器创建 { if (sections.Length < 4) { throw new APIException(ERROR_CODES.INVALID_REQUEST_INVALID_ACTION_FORMAT, "action值的格式错误,在产品接口下,action的格式为:[客户ID].[模块名].[对象名].[操作名]"); } var className = string.Format("{0}.{1}.{2}.{3}.{4}AH", ActionHandlerFactory.PROJECT_ACTION_HANDLER_BASE_NAMESPACE, sections[0], sections[1], sections[2], sections[3]); IActionHandler handler = null; try { var oh = Activator.CreateInstance(null, className); if (oh != null) { handler = oh.Unwrap() as IActionHandler; } } catch (Exception ex) { throw new APIException(ERROR_CODES.INVALID_REQUEST_CAN_NOT_FIND_ACTION_HANDLER, string.Format("根据action找不到指定的ActionHandler类.原因:[{0}]", ex.Message), ex); } // return(handler); } #endregion default: throw new NotImplementedException(); } }
public static APIResponse <TResponseData> CallAPI <TRequestParameter, TResponseData>(APITypes pType, string pPath, string pAction, APIRequest <TRequestParameter> pRequest) where TRequestParameter : IAPIRequestParameter, new() where TResponseData : IAPIResponseData { //string queryString = string.Format("?type={0}&action={1}", pType.ToCode(), pAction); string queryString = string.Format("{0}?type={1}&action={2}", pPath, pType.ToCode(), pAction); var content = string.Format("req={0}", HttpUtility.UrlEncode(pRequest.ToJSON())); var strRsp = APIClientProxy.CallAPI(queryString, content); if (!string.IsNullOrWhiteSpace(strRsp)) { if (!string.IsNullOrWhiteSpace(pRequest.JSONP)) { strRsp = strRsp.Substring(pRequest.JSONP.Length + 1); strRsp = strRsp.Substring(0, strRsp.Length - 1); } return(strRsp.DeserializeJSONTo <APIResponse <TResponseData> >()); } // return(null); }