refactor: 全框架 JSON 操作迁移到 simapi_serialization
- 依赖:新增 simapi_serialization path 依赖(保留 soulsoft_serialization 供 soulsoft web 栈传递使用) - SimApiJson.json / SimApiUtil.json → JsonSerializer.Serialize - SimApiUtil.fromJson / base64DecodeTo → JsonSerializer.Deserialize(去 ISerialization 约束) - SimApiHttpClient signQuery/aesQuery/aesSignQuery/query → 去约束,反序列化换新库 - SimApiBaseResponse/SimApiDataResponse/SimApiLoginItem/AesBodyRequest/AuthSDK DTO → 去 @Serialization 宏/去 soulsoft 接口,改纯 POCO(simapi_serialization 反射) - SimApiBaseResponse 标 @SerializerParent:子类响应序列化包含 _code/_message - SimApiResultWriter / SimApiExceptionMiddleware → 响应输出走 simapi_serialization(统一 _code/_message 格式) - SimApiRequestLogMiddleware 截断 → Deserialize<HashMap<String,Any>> + 重序列化 - SimApiAuthDto JsonValue 字段 → HashMap<String,Any>;SimApiCache.get<T> 去约束 - webdemo:AuthController readFromJson 换 Deserialize;cjpm.toml 补依赖 - 验证:webdemo 启动正常,GET/POST 接口响应格式统一,S3 存储全链路 OK
This commit is contained in:
@@ -11,7 +11,7 @@ import stdx.net.tls.*
|
||||
import stdx.net.tls.common.*
|
||||
import soulsoft_net_http.{HttpClient, HttpRequestMessage, JsonContent}
|
||||
import soulsoft_net_http.{HttpMethod as NetHttpMethod}
|
||||
import soulsoft_serialization.*
|
||||
import simapi_serialization.*
|
||||
import simapi.communications.*
|
||||
import simapi.configurations.*
|
||||
import simapi.exceptions.*
|
||||
@@ -21,7 +21,7 @@ import simapi.exceptions.*
|
||||
* 对齐 C# 的 SimApi.Helpers.SimApiHttpClient:
|
||||
* - 内部使用 soulsoft_net_http 的 HttpClient(等价 .NET 的 System.Net.Http.HttpClient)
|
||||
* - 返回泛型 T(反序列化响应 body 的 data 字段),不再返回 String
|
||||
* @param T 响应 data 的数据类型(需实现 ISerialization<T>,如 SimApiLoginItem、String、Int64 等)。
|
||||
* @param T 响应 data 的数据类型(任意类,simapi_serialization 反射反序列化)。
|
||||
*/
|
||||
public open class SimApiHttpClient {
|
||||
public var server: String
|
||||
@@ -48,7 +48,7 @@ public open class SimApiHttpClient {
|
||||
* @param queries 额外查询参数(可选)。
|
||||
* @return 响应 data 字段反序列化后的 T。
|
||||
*/
|
||||
public func signQuery<T>(url: String, body!: String = "", queries!: HashMap<String, String> = HashMap<String, String>()): T where T <: ISerialization<T> {
|
||||
public func signQuery<T>(url: String, body!: String = "", queries!: HashMap<String, String> = HashMap<String, String>()): T {
|
||||
var queryUrl = StringBuilder()
|
||||
for (field in signFields) {
|
||||
queryUrl.append("${field}=")
|
||||
@@ -80,7 +80,7 @@ public open class SimApiHttpClient {
|
||||
* @param body 请求体 JSON 字符串。
|
||||
* @return 响应 data 字段反序列化后的 T。
|
||||
*/
|
||||
public func aesQuery<T>(url: String, body: String): T where T <: ISerialization<T> {
|
||||
public func aesQuery<T>(url: String, body: String): T {
|
||||
var target = "${server}${url}"
|
||||
if (let Some(name) <- appIdName) {
|
||||
target = "${target}?${name}=${appId}"
|
||||
@@ -98,19 +98,20 @@ public open class SimApiHttpClient {
|
||||
* @param queries 额外查询参数(可选)。
|
||||
* @return 响应 data 字段反序列化后的 T。
|
||||
*/
|
||||
public func aesSignQuery<T>(url: String, body: String, queries!: HashMap<String, String> = HashMap<String, String>()): T where T <: ISerialization<T> {
|
||||
public func aesSignQuery<T>(url: String, body: String, queries!: HashMap<String, String> = HashMap<String, String>()): T {
|
||||
let encrypted = aesEncrypt(body)
|
||||
let req = "{\"data\":\"${encrypted}\"}"
|
||||
return signQuery<T>(url, body: req, queries: queries)
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起 POST 请求并反序列化 SimApiBaseResponse<T>,返回 data 字段。
|
||||
* 发起 POST 请求并反序列化 SimApiResponse<T>,返回 data 字段。
|
||||
* 对齐 C# Query<T>:
|
||||
* ErrorWhenFalse(IsSuccessStatusCode) → ReadFromJsonAsync<SimApiBaseResponse<T>> → ErrorWhen(Code != 200) → return Data。
|
||||
* ErrorWhenFalse(IsSuccessStatusCode) → ReadFromJsonAsync<SimApiResponse<T>> → ErrorWhen(Code != 200) → return Data。
|
||||
* 注意:必须 noProxy(),否则会走系统代理(192.168.0.250:8118)导致连接被拒。
|
||||
* 反序列化使用 simapi_serialization(Deserialize<T> 免约束)。
|
||||
*/
|
||||
private func query<T>(url: String, body: String): T where T <: ISerialization<T> {
|
||||
private func query<T>(url: String, body: String): T {
|
||||
let client = HttpClient.create { builder =>
|
||||
builder.noProxy()
|
||||
// 支持 https:配置 TLS(信任所有证书 + SNI 域名)
|
||||
@@ -128,7 +129,8 @@ public open class SimApiHttpClient {
|
||||
let response = client.send(request)
|
||||
try {
|
||||
SimApiError.errorWhenFalse(response.isSuccessStatusCode, code: response.statusCode, message: "HTTP ERROR: ${response.statusCode}")
|
||||
let result = response.content.readFromJson<SimApiResponse<T>>()
|
||||
let json = response.content.readAsString()
|
||||
let result = JsonSerializer.Deserialize<SimApiResponse<T>>(json)
|
||||
SimApiError.errorWhen(result._code != 200, code: result._code, message: result._message)
|
||||
return result._data.getOrThrow()
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user