diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Network/DefaultWarpHttpClient.cs b/src/ZonyLrcTools.Cli/Infrastructure/Network/DefaultWarpHttpClient.cs index 52aeb0d..d6d2825 100644 --- a/src/ZonyLrcTools.Cli/Infrastructure/Network/DefaultWarpHttpClient.cs +++ b/src/ZonyLrcTools.Cli/Infrastructure/Network/DefaultWarpHttpClient.cs @@ -35,12 +35,7 @@ namespace ZonyLrcTools.Cli.Infrastructure.Network using var responseMessage = await BuildHttpClient().SendAsync(requestMessage); var responseContentString = await responseMessage.Content.ReadAsStringAsync(); - return responseMessage.StatusCode switch - { - HttpStatusCode.OK => responseContentString, - HttpStatusCode.ServiceUnavailable => throw new ErrorCodeException(ErrorCodes.ServiceUnavailable), - _ => throw new ErrorCodeException(ErrorCodes.ServiceUnavailable, attachObj: new {parametersStr, responseContentString}) - }; + return ValidateHttpResponse(responseMessage, parameters, responseContentString); } public async ValueTask PostAsync(string url, @@ -49,19 +44,7 @@ namespace ZonyLrcTools.Cli.Infrastructure.Network Action requestOption = null) { var responseString = await PostAsync(url, parameters, isQueryStringParam, requestOption); - var throwException = new ErrorCodeException(ErrorCodes.HttpResponseConvertJsonFailed, attachObj: new {parameters, responseString}); - - try - { - var responseObj = JsonConvert.DeserializeObject(responseString); - if (responseObj != null) return responseObj; - - throw throwException; - } - catch (JsonSerializationException) - { - throw throwException; - } + return ConvertHttpResponseToObject(parameters, responseString); } public async ValueTask GetAsync(string url, @@ -72,36 +55,18 @@ namespace ZonyLrcTools.Cli.Infrastructure.Network var requestMsg = new HttpRequestMessage(HttpMethod.Get, new Uri($"{url}?{requestParamsStr}")); requestOption?.Invoke(requestMsg); - using (var responseMsg = await BuildHttpClient().SendAsync(requestMsg)) - { - var responseContent = await responseMsg.Content.ReadAsStringAsync(); + using var responseMessage = await BuildHttpClient().SendAsync(requestMsg); + var responseContentString = await responseMessage.Content.ReadAsStringAsync(); - return responseMsg.StatusCode switch - { - HttpStatusCode.OK => responseContent, - HttpStatusCode.ServiceUnavailable => throw new ErrorCodeException(ErrorCodes.ServiceUnavailable), - _ => throw new ErrorCodeException(ErrorCodes.ServiceUnavailable, attachObj: new {requestParamsStr, responseContent}) - }; - } + return ValidateHttpResponse(responseMessage, parameters, responseContentString); } public async ValueTask GetAsync(string url, object parameters = null, Action requestOption = null) { - var responseStr = await GetAsync(url, parameters, requestOption); - var throwException = new ErrorCodeException(ErrorCodes.HttpResponseConvertJsonFailed, attachObj: new {parameters, responseStr}); - try - { - var responseObj = JsonConvert.DeserializeObject(responseStr); - if (responseObj != null) return responseObj; - - throw throwException; - } - catch (JsonSerializationException) - { - throw throwException; - } + var responseString = await GetAsync(url, parameters, requestOption); + return ConvertHttpResponseToObject(parameters, responseString); } protected virtual HttpClient BuildHttpClient() @@ -143,5 +108,47 @@ namespace ZonyLrcTools.Cli.Infrastructure.Network return JsonConvert.SerializeObject(parameters); } + + /// + /// 校验 Http 响应 是否合法。 + /// + /// 执行 Http 请求之后的响应实例。 + /// 执行 Http 请求时传递的参数。 + /// 执行 Http 请求之后响应内容。 + /// 如果响应正常,则返回具体的响应内容。 + /// 如果 Http 响应不正常,则可能抛出本异常。 + private string ValidateHttpResponse(HttpResponseMessage responseMessage, object requestParameters, string responseString) + { + return responseMessage.StatusCode switch + { + HttpStatusCode.OK => responseString, + HttpStatusCode.ServiceUnavailable => throw new ErrorCodeException(ErrorCodes.ServiceUnavailable), + _ => throw new ErrorCodeException(ErrorCodes.HttpRequestFailed, attachObj: new {requestParameters, responseString}) + }; + } + + /// + /// 将 Http 响应的字符串反序列化为指定类型 的对象。 + /// + /// 执行 Http 请求时传递的参数。 + /// 执行 Http 请求之后响应内容。 + /// 需要将响应结果反序列化的目标类型。 + /// 如果反序列化失败,则可能抛出本异常。 + private TResponse ConvertHttpResponseToObject(object requestParameters, string responseString) + { + var throwException = new ErrorCodeException(ErrorCodes.HttpResponseConvertJsonFailed, attachObj: new {requestParameters, responseString}); + + try + { + var responseObj = JsonConvert.DeserializeObject(responseString); + if (responseObj != null) return responseObj; + + throw throwException; + } + catch (JsonSerializationException) + { + throw throwException; + } + } } } \ No newline at end of file