Files
simapi-net/Helpers/SimApiHttpClient.cs
T

89 lines
2.8 KiB
C#
Raw Normal View History

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;
using SimApi.Communications;
using SimApi.Exceptions;
2025-11-02 22:14:34 +08:00
namespace SimApi.Helpers;
2025-12-18 16:17:18 +08:00
public class SimApiHttpClient(string? appId, string appKey, bool debug = false)
2025-11-02 22:14:34 +08:00
{
2025-12-18 11:28:47 +08:00
public string Server { get; init; } = string.Empty;
2025-11-02 22:14:34 +08:00
public string SignName { get; init; } = "sign";
public string TimestampName { get; init; } = "timestamp";
public string NonceName { get; init; } = "nonce";
public string? AppIdName { get; init; } = "appId";
public string[] SignFields { get; init; } = [];
2025-12-18 13:05:05 +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))
{
queryUrl += $"{AppIdName}={appId}&";
}
queryUrl += $"{TimestampName}={(int)SimApiUtil.TimestampNow}&{NonceName}={Guid.NewGuid()}";
var signStr = $"{queryUrl}&{appKey}";
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}");
}
return Query<T>(path, body);
2025-11-02 22:14:34 +08:00
}
public T? AesQuery<T>(string url, object body)
{
2025-12-18 11:28:47 +08:00
url = Server + url;
2025-11-02 22:14:34 +08:00
if (!string.IsNullOrEmpty(AppIdName))
{
url += $"?{AppIdName}={appId}";
}
var req = new SimApiOneFieldRequest<string>
{
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
};
return Query<T>(url, req);
2025-11-02 22:14:34 +08:00
}
public T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
{
var req = new SimApiOneFieldRequest<string>
{
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
};
return SignQuery<T>(url, req, queries);
}
private T? Query<T>(string url, object? req)
{
var http = new HttpClient();
2025-12-18 16:17:18 +08:00
if (debug)
{
Console.WriteLine($"[HTTPCLIENT请求] {url}\n{SimApiUtil.Json(req)}\n");
}
var resp = http.PostAsJsonAsync(url, req).Result;
2025-12-18 16:17:18 +08:00
if (debug)
{
Console.WriteLine($"[HTTPCLIENT响应] {resp.Content.ReadAsStringAsync().Result}\n");
}
var res = resp.Content.ReadFromJsonAsync<SimApiBaseResponse<T>>().Result;
if (res == null)
{
throw new SimApiException(500, "请求发生错误");
}
return res.Code != 200 ? throw new SimApiException(res.Code, res.Message) : res.Data;
}
2025-11-02 22:14:34 +08:00
}