- 实现环境配置管理,支持外部和本地主机模式切换 - 集成Demo API接口,包含登录、注册、文件上传等功能 - 构建附件处理仓库,支持图片、视频、音频和文件发送 - 开发认证仓库,管理用户会话和IM令牌刷新机制 - 添加语音录制功能,支持实时音频消息录制 - 创建依赖注入容器,统一管理应用组件实例 - 实现登录界面,提供用户认证交互功能 - 开发聊天界面,集成消息收发和媒体处理功能
74 行
2.4 KiB
Swift
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: config.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)
|
|
}
|
|
}
|