refactor: Refactor the code. (DRY)

重构代码。(DRY 原则)
This commit is contained in:
real-zony 2021-05-07 11:21:45 +08:00
parent 6ec03d0ec3
commit abdb1dea23

View File

@ -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<TResponse> PostAsync<TResponse>(string url,
@ -49,19 +44,7 @@ namespace ZonyLrcTools.Cli.Infrastructure.Network
Action<HttpRequestMessage> 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<TResponse>(responseString);
if (responseObj != null) return responseObj;
throw throwException;
}
catch (JsonSerializationException)
{
throw throwException;
}
return ConvertHttpResponseToObject<TResponse>(parameters, responseString);
}
public async ValueTask<string> 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<TResponse> GetAsync<TResponse>(string url,
object parameters = null,
Action<HttpRequestMessage> requestOption = null)
{
var responseStr = await GetAsync(url, parameters, requestOption);
var throwException = new ErrorCodeException(ErrorCodes.HttpResponseConvertJsonFailed, attachObj: new {parameters, responseStr});
try
{
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseStr);
if (responseObj != null) return responseObj;
throw throwException;
}
catch (JsonSerializationException)
{
throw throwException;
}
var responseString = await GetAsync(url, parameters, requestOption);
return ConvertHttpResponseToObject<TResponse>(parameters, responseString);
}
protected virtual HttpClient BuildHttpClient()
@ -143,5 +108,47 @@ namespace ZonyLrcTools.Cli.Infrastructure.Network
return JsonConvert.SerializeObject(parameters);
}
/// <summary>
/// 校验 Http 响应 <see cref="HttpResponseMessage"/> 是否合法。
/// </summary>
/// <param name="responseMessage">执行 Http 请求之后的响应实例。</param>
/// <param name="requestParameters">执行 Http 请求时传递的参数。</param>
/// <param name="responseString">执行 Http 请求之后响应内容。</param>
/// <returns>如果响应正常,则返回具体的响应内容。</returns>
/// <exception cref="ErrorCodeException">如果 Http 响应不正常,则可能抛出本异常。</exception>
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})
};
}
/// <summary>
/// 将 Http 响应的字符串反序列化为指定类型 <see cref="TResponse"/> 的对象。
/// </summary>
/// <param name="requestParameters">执行 Http 请求时传递的参数。</param>
/// <param name="responseString">执行 Http 请求之后响应内容。</param>
/// <typeparam name="TResponse">需要将响应结果反序列化的目标类型。</typeparam>
/// <exception cref="ErrorCodeException">如果反序列化失败,则可能抛出本异常。</exception>
private TResponse ConvertHttpResponseToObject<TResponse>(object requestParameters, string responseString)
{
var throwException = new ErrorCodeException(ErrorCodes.HttpResponseConvertJsonFailed, attachObj: new {requestParameters, responseString});
try
{
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseString);
if (responseObj != null) return responseObj;
throw throwException;
}
catch (JsonSerializationException)
{
throw throwException;
}
}
}
}