chore: core 多模块骨架
This commit is contained in:
37
IosClientKit/Sources/CoreNetwork/APIClient.swift
Normal file
37
IosClientKit/Sources/CoreNetwork/APIClient.swift
Normal file
@@ -0,0 +1,37 @@
|
||||
import Foundation
|
||||
|
||||
/// 网络错误。
|
||||
public enum NetworkError: Error, Sendable {
|
||||
case invalidResponse
|
||||
case http(statusCode: Int)
|
||||
case decoding(String)
|
||||
}
|
||||
|
||||
/// 通用 HTTP 客户端(对应 Android Retrofit 的最小抽象)。
|
||||
public protocol APIClient: Sendable {
|
||||
func get<T: Decodable & Sendable>(_ url: URL) async throws -> T
|
||||
}
|
||||
|
||||
/// 基于 URLSession 的 APIClient 实现。
|
||||
public struct URLSessionAPIClient: APIClient {
|
||||
private let session: URLSession
|
||||
|
||||
public init(session: URLSession = .shared) {
|
||||
self.session = session
|
||||
}
|
||||
|
||||
public func get<T: Decodable & Sendable>(_ url: URL) async throws -> T {
|
||||
let (data, response) = try await session.data(from: url)
|
||||
guard let http = response as? HTTPURLResponse else {
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
guard (200..<300).contains(http.statusCode) else {
|
||||
throw NetworkError.http(statusCode: http.statusCode)
|
||||
}
|
||||
do {
|
||||
return try JSONDecoder().decode(T.self, from: data)
|
||||
} catch {
|
||||
throw NetworkError.decoding(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
15
IosClientKit/Sources/CoreNetwork/HomeDTO.swift
Normal file
15
IosClientKit/Sources/CoreNetwork/HomeDTO.swift
Normal file
@@ -0,0 +1,15 @@
|
||||
import Foundation
|
||||
|
||||
/// 示例网络 DTO(与领域 Model 分离,对应 Kotlin @Serializable 的 data class)。
|
||||
/// 注意:DTO 只承载接口字段,不直接进入 UI;领域层用 CoreModel.HomeData。
|
||||
public struct HomeDTO: Codable, Sendable {
|
||||
public let id: String
|
||||
public let greeting: String
|
||||
public let habits: [HabitDTO]
|
||||
}
|
||||
|
||||
public struct HabitDTO: Codable, Sendable {
|
||||
public let id: String
|
||||
public let title: String
|
||||
public let checkedIn: Bool
|
||||
}
|
||||
Reference in New Issue
Block a user