XuqmGroup-iOSSDK/Sources/XuqmSDK/Core/ApiClient.swift
XuqmGroup 2352b46c6b docs(sdk): 添加 Android SDK 文档和 API 设计规范
- 新增 Android SDK 使用文档,包含模块结构、集成方式和快速开始指南
- 添加 SDK API 重设计规范,统一初始化和登录接口设计
- 补充安全设计规范,完善 UserSig 鉴权和敏感数据处理方案
- 创建平台 REST API 规范,定义服务端到服务端的调用接口
- 添加离线推送架构设计,集成各大厂商推送服务与 IM 联动方案
2026-04-29 15:46:39 +08:00

74 行
2.4 KiB
Swift

import Foundation
public struct ApiResponse<T: Decodable>: Decodable {
public let code: Int
public let status: String
public let data: T?
public let message: String
}
public struct EmptyResponse: Decodable, Sendable {
public init() {}
}
public final class ApiClient: @unchecked Sendable {
public static let shared = ApiClient()
private var config: SDKConfig?
private var session: URLSession = .shared
private init() {}
func configure(with config: SDKConfig) {
self.config = config
}
public func request<T: Decodable>(
path: String,
method: String = "GET",
queryItems: [URLQueryItem]? = nil,
body: (some Encodable)? = nil as String?
) async throws -> T {
guard let config else { throw URLError(.badURL) }
let tokenStore = await XuqmSDK.shared.tokenStore
var components = URLComponents(url: SDKEndpoints.apiBaseURL.appendingPathComponent(path), resolvingAgainstBaseURL: false)!
if let qi = queryItems { components.queryItems = qi }
var req = URLRequest(url: components.url!)
req.httpMethod = method
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
if let token = tokenStore?.get() {
req.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
if let b = body {
req.httpBody = try JSONEncoder().encode(b)
}
if config.debug {
print("[XuqmSDK][HTTP] request", method, req.url?.absoluteString ?? path)
}
let (data, response) = try await session.data(for: req)
guard let http = response as? HTTPURLResponse, (200..<300).contains(http.statusCode) else {
if config.debug, let http = response as? HTTPURLResponse {
print("[XuqmSDK][HTTP] response", http.statusCode, req.url?.absoluteString ?? path)
}
throw URLError(.badServerResponse)
}
if config.debug {
print("[XuqmSDK][HTTP] response", http.statusCode, req.url?.absoluteString ?? path)
}
let wrapper = try JSONDecoder().decode(ApiResponse<T>.self, from: data)
if let result = wrapper.data {
return result
}
if T.self == EmptyResponse.self {
return EmptyResponse() as! T
}
throw URLError(.cannotDecodeContentData)
}
}