コード例 #1
0
ファイル: ApiClient.cs プロジェクト: gytmgr/Dry
        /// <summary>
        /// http请求
        /// </summary>
        /// <param name="method"></param>
        /// <param name="apiPath"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        protected virtual async Task RequestAsync(HttpMethod method, string apiPath = null, object param = null)
        {
            using var requester = new HttpRequester(method, ApiUrl + apiPath);
            requester.SetRequestParam(param);
            var response = await requester.GetStringResultAsync();

            if (response.Code == HttpStatusCode.OK || response.Code == HttpStatusCode.NoContent)
            {
                return;
            }
            if (response.Code == HttpStatusCode.BadRequest)
            {
                throw new BizException(response.Data);
            }
            throw new Exception(response.Data);
        }
コード例 #2
0
        /// <summary>
        /// 返回泛型结果
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="requester"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static async Task <Result <HttpStatusCode, TData> > GetResultAsync <TData>(this HttpRequester requester, object param = null)
        {
            requester.SetRequestParam(param);
            var result = await requester.GetStringResultAsync();

            if (result.Code == HttpStatusCode.OK)
            {
                if (string.IsNullOrEmpty(result.Data))
                {
                    return(Result <HttpStatusCode, TData> .Create(result.Code, default));
                }
                var data = JsonSerializer.Deserialize <TData>(result.Data, new JsonSerializerOptions().DefaultConfig());
                return(Result <HttpStatusCode, TData> .Create(result.Code, data));
            }
            return(Result <HttpStatusCode, TData> .Create(result.Code, result.Data, default));
        }