public static async Task <TRes> RestApi <TRes>(string absoluateApiUrl, object reqContent, HttpMethod mothed) where TRes : ResultMo, new() { var httpReq = new OsHttpRequest { HttpMethod = mothed, AddressUrl = absoluateApiUrl, CustomBody = reqContent == null ? null : JsonConvert.SerializeObject(reqContent, Formatting.None, new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore }), RequestSet = r => { var ticket = MemberShiper.AppAuthorize.ToTicket(appSource, appVersion, secretKey); r.Headers.Add(GlobalKeysUtil.AuthorizeTicketName, ticket); r.Headers.Add("Accept", "application/json"); if (r.Content != null) { r.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json") { CharSet = "UTF-8" }; } } }; return(await httpReq.RestCommonJson <TRes>()); }
/// <summary> /// post一个Api请求 /// </summary> /// <typeparam name="TRes"></typeparam> /// <param name="apiRoute"></param> /// <param name="req"></param> /// <param name="funcFormat"></param> /// <returns></returns> public static async Task <TRes> PostApi <TRes>(string apiRoute, object req = null, Func <HttpResponseMessage, Task <TRes> > funcFormat = null) where TRes : ResultMo, new() { var sysInfo = MemberShiper.AppAuthorize; var secretKeyRes = ApiSourceKeyUtil.GetAppSecretKey(sysInfo.AppSource); if (!secretKeyRes.IsSuccess()) { return(secretKeyRes.ConvertToResult <TRes>()); } var httpReq = new OsHttpRequest { HttpMothed = HttpMothed.POST, AddressUrl = string.Concat(apiUrlPre, apiRoute), CustomBody = JsonConvert.SerializeObject(req, Formatting.None, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }), RequestSet = r => { r.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json") { CharSet = "UTF-8" }; var ticket = MemberShiper.AppAuthorize.ToSignData(secretKeyRes.data); r.Content.Headers.Add(GlobalKeysUtil.AuthorizeTicketName, ticket); } }; return(await httpReq.RestCommonJson <TRes>()); }
/// <summary> /// 处理远程请求方法,并返回需要的实体 /// </summary> /// <typeparam name="T">需要返回的实体类型</typeparam> /// <param name="request">远程请求组件的request基本信息</param> /// <param name="client">自定义 HttpClient </param> /// <returns>实体类型</returns> public async Task <T> RestCommonJson <T>(OsHttpRequest request, HttpClient client = null) where T : WxBaseResp, new() { var t = await request.RestCommonJson <T>(client); if (!t.IsSuccess()) { t.msg = t.errmsg; } return(t); }
/// <summary> /// 处理远程请求方法,并返回需要的实体 /// </summary> /// <typeparam name="T">需要返回的实体类型</typeparam> /// <param name="request">远程请求组件的request基本信息</param> /// <param name="client">自定义 HttpClient </param> /// <returns>实体类型</returns> public async Task <T> RestCommonJson <T>(OsHttpRequest request, HttpClient client = null) where T : ResultMo, new() { var t = await request.RestCommonJson <T>(client); if (!t.IsSuccess()) { t.message = GetErrMsg(t.ret); } return(t); }
public async void TestRestCommonJson() { var req = new OsHttpRequest { AddressUrl = "http://localhost:62936", HttpMethod = HttpMethod.Get }; MemberShiper.SetAppAuthrizeInfo(new AppAuthorizeInfo()); var res = await req.RestCommonJson <ResultMo>(); Assert.True(res.IsSuccess()); }