2026-04-21 22:07:29 +08:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
public struct ApiResponse<T: Decodable>: Decodable {
|
|
|
|
|
public let code: Int
|
|
|
|
|
public let status: String
|
|
|
|
|
public let data: T?
|
|
|
|
|
public let message: String
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 10:27:23 +08:00
|
|
|
public struct EmptyResponse: Decodable, Sendable {
|
|
|
|
|
public init() {}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 22:07:29 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (data, response) = try await session.data(for: req)
|
|
|
|
|
guard let http = response as? HTTPURLResponse, (200..<300).contains(http.statusCode) else {
|
|
|
|
|
throw URLError(.badServerResponse)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let wrapper = try JSONDecoder().decode(ApiResponse<T>.self, from: data)
|
2026-04-28 10:27:23 +08:00
|
|
|
if let result = wrapper.data {
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
if T.self == EmptyResponse.self {
|
|
|
|
|
return EmptyResponse() as! T
|
|
|
|
|
}
|
|
|
|
|
throw URLError(.cannotDecodeContentData)
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
}
|