2025-11-02 22:14:34 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Json;
|
2026-05-10 20:24:08 +08:00
|
|
|
using Microsoft.Extensions.Logging;
|
2025-11-02 22:14:34 +08:00
|
|
|
using SimApi.Communications;
|
2026-05-10 20:24:08 +08:00
|
|
|
using SimApi.Configurations;
|
2025-12-18 14:06:11 +08:00
|
|
|
using SimApi.Exceptions;
|
2025-11-02 22:14:34 +08:00
|
|
|
|
|
|
|
|
namespace SimApi.Helpers;
|
|
|
|
|
|
2026-05-10 20:24:08 +08:00
|
|
|
public class SimApiHttpClient(SimApiOptions? apiOptions = null, ILogger<SimApiHttpClient>? logger = null)
|
2025-11-02 22:14:34 +08:00
|
|
|
{
|
2026-05-22 12:37:00 +08:00
|
|
|
public virtual string Server { get; init; } = apiOptions?.SimApiHttpClientOptions.Server ?? string.Empty;
|
|
|
|
|
public virtual string AppId { get; init; } = apiOptions?.SimApiHttpClientOptions.AppId ?? string.Empty;
|
|
|
|
|
public virtual string AppKey { get; init; } = apiOptions?.SimApiHttpClientOptions.AppKey ?? string.Empty;
|
2026-05-05 03:53:02 +08:00
|
|
|
|
2026-05-22 12:37:00 +08:00
|
|
|
public virtual string SignName { get; init; } = "sign";
|
|
|
|
|
public virtual string TimestampName { get; init; } = "timestamp";
|
|
|
|
|
public virtual string NonceName { get; init; } = "nonce";
|
|
|
|
|
public virtual string? AppIdName { get; init; } = "appId";
|
|
|
|
|
public virtual string[] SignFields { get; init; } = [];
|
2025-11-02 22:14:34 +08:00
|
|
|
|
2026-04-26 10:03:38 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 发起签名请求
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <param name="body"></param>
|
|
|
|
|
/// <param name="queries"></param>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
2026-06-23 15:25:35 +08:00
|
|
|
public T SignQuery<T>(string url, object? body = null, Dictionary<string, string>? queries = null)
|
2025-11-02 22:14:34 +08:00
|
|
|
{
|
2025-12-18 11:28:47 +08:00
|
|
|
url = Server + url;
|
2025-11-02 22:14:34 +08:00
|
|
|
var queryUrl = SignFields.Aggregate(string.Empty,
|
|
|
|
|
(current, signField) => current + $"{signField}={queries?[signField]}&");
|
|
|
|
|
if (!string.IsNullOrEmpty(AppIdName))
|
|
|
|
|
{
|
2026-05-05 03:53:02 +08:00
|
|
|
queryUrl += $"{AppIdName}={AppId}&";
|
2025-11-02 22:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryUrl += $"{TimestampName}={(int)SimApiUtil.TimestampNow}&{NonceName}={Guid.NewGuid()}";
|
2026-05-05 03:53:02 +08:00
|
|
|
var signStr = $"{queryUrl}&{AppKey}";
|
2025-11-02 22:14:34 +08:00
|
|
|
var path = $"{url}?{queryUrl}&{SignName}={SimApiUtil.Md5(signStr)}";
|
|
|
|
|
|
|
|
|
|
if (queries != null)
|
|
|
|
|
{
|
|
|
|
|
path = queries.Where(q => !SignFields.Contains(q.Key))
|
|
|
|
|
.Aggregate(path, (current, q) => current + $"&{q.Key}={q.Value}");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 14:06:11 +08:00
|
|
|
return Query<T>(path, body);
|
2025-11-02 22:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-26 10:03:38 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 发起AES加密请求
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <param name="body"></param>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
2026-06-23 15:25:35 +08:00
|
|
|
public T AesQuery<T>(string url, object body)
|
2025-11-02 22:14:34 +08:00
|
|
|
{
|
2025-12-18 11:28:47 +08:00
|
|
|
url = Server + url;
|
2025-11-02 22:14:34 +08:00
|
|
|
if (!string.IsNullOrEmpty(AppIdName))
|
|
|
|
|
{
|
2026-05-05 03:53:02 +08:00
|
|
|
url += $"?{AppIdName}={AppId}";
|
2025-11-02 22:14:34 +08:00
|
|
|
}
|
2026-06-23 16:13:04 +08:00
|
|
|
logger?.LogDebug($"[HTTPCLIENT请求][加密前BODY] {url}\n{SimApiUtil.Json(body)}\n");
|
2025-11-02 22:14:34 +08:00
|
|
|
var req = new SimApiOneFieldRequest<string>
|
|
|
|
|
{
|
2026-05-05 03:53:02 +08:00
|
|
|
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), AppKey)
|
2025-11-02 22:14:34 +08:00
|
|
|
};
|
2025-12-18 14:06:11 +08:00
|
|
|
return Query<T>(url, req);
|
2025-11-02 22:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-26 10:03:38 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 发起AES加密以及签名请求
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <param name="body"></param>
|
|
|
|
|
/// <param name="queries"></param>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
2026-06-23 15:25:35 +08:00
|
|
|
public T AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
|
2025-11-02 22:14:34 +08:00
|
|
|
{
|
2026-06-23 16:13:04 +08:00
|
|
|
logger?.LogDebug($"[HTTPCLIENT请求][加密前BODY] {url}\n{SimApiUtil.Json(body)}\n");
|
2025-11-02 22:14:34 +08:00
|
|
|
var req = new SimApiOneFieldRequest<string>
|
|
|
|
|
{
|
2026-05-05 03:53:02 +08:00
|
|
|
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), AppKey)
|
2025-11-02 22:14:34 +08:00
|
|
|
};
|
|
|
|
|
return SignQuery<T>(url, req, queries);
|
|
|
|
|
}
|
2025-12-18 14:06:11 +08:00
|
|
|
|
2026-04-26 10:03:38 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 发起请求
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <param name="req"></param>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="SimApiException"></exception>
|
2026-06-23 15:25:35 +08:00
|
|
|
private T Query<T>(string url, object? req)
|
2025-12-18 14:06:11 +08:00
|
|
|
{
|
|
|
|
|
var http = new HttpClient();
|
2026-05-10 20:24:08 +08:00
|
|
|
logger?.LogDebug($"[HTTPCLIENT请求] {url}\n{SimApiUtil.Json(req)}\n");
|
2025-12-18 14:06:11 +08:00
|
|
|
var resp = http.PostAsJsonAsync(url, req).Result;
|
2026-05-10 20:24:08 +08:00
|
|
|
logger?.LogDebug($"[HTTPCLIENT响应] {resp.Content.ReadAsStringAsync().Result}\n");
|
2026-05-22 12:37:00 +08:00
|
|
|
SimApiError.ErrorWhenFalse(resp.IsSuccessStatusCode, (int)resp.StatusCode, $"HTTP ERROR: {resp.StatusCode}");
|
2025-12-18 14:06:11 +08:00
|
|
|
var res = resp.Content.ReadFromJsonAsync<SimApiBaseResponse<T>>().Result;
|
2026-05-10 20:24:08 +08:00
|
|
|
SimApiError.ErrorWhenNull(res, 500, "请求发生错误");
|
|
|
|
|
SimApiError.ErrorWhen(res.Code != 200, res.Code, res.Message);
|
2026-06-23 15:25:35 +08:00
|
|
|
return res.Data!;
|
2025-12-18 14:06:11 +08:00
|
|
|
}
|
2025-11-02 22:14:34 +08:00
|
|
|
}
|