43 lines
1.0 KiB
C#
43 lines
1.0 KiB
C#
using System.Net;
|
|
using Kurisu.RemoteCall.Abstractions;
|
|
using Mapster;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Dlhis.Dh.Api.ResultHandlers;
|
|
|
|
public class DhResultHandler : IRemoteCallResultHandler
|
|
{
|
|
/// <inheritdoc />
|
|
public TResult Handle<TResult>(HttpStatusCode statusCode, string responseBody)
|
|
{
|
|
var apiResult = JsonConvert.DeserializeObject<DhResponse>(responseBody)!;
|
|
|
|
var isSuccessStatusCode = ((int)statusCode >= 200) && ((int)statusCode <= 299);
|
|
if (!isSuccessStatusCode || apiResult.Success == false)
|
|
{
|
|
if (!string.IsNullOrEmpty(apiResult.ErrMsg))
|
|
{
|
|
throw new Exception(apiResult.ErrMsg);
|
|
}
|
|
|
|
throw new HttpRequestException(statusCode + " " + (int)statusCode);
|
|
}
|
|
|
|
return apiResult.Adapt<TResult>();
|
|
}
|
|
}
|
|
|
|
public class DhResponse
|
|
{
|
|
public bool Success { get; set; }
|
|
|
|
public string Code { get; set; }
|
|
|
|
public string ErrMsg { get; set; }
|
|
}
|
|
|
|
// {
|
|
// "success": true,
|
|
// "code": "0",
|
|
// "errMsg": "请求成功"
|
|
// } |