SwiftQuests
一个面向对象的,基于 URLSession 的网络库。
安装
Swift 包管理器
使用 URL 将 Swift 包依赖添加到项目
https://github.com/Ast3r10n/swiftquests
CocoaPods
pod 'SwiftQuests'
使用方法
Request
是一个基本的、独立的对象,它与一个关联的任务相关联,通过其初始化器进行配置。一旦初始化,Request
(大部分情况下)就是不可变的。其任务将通过 perform
方法调用启动。
基础请求
要执行一个基本的 请求
,需要初始化一个
do {
let request = try Request(.get,
atPath: "/user")
} catch {
// Error handling
}
然后调用 perform
方法来启动其关联的任务。
do {
try request.perform { data, response, error in
// Response implementation
}
} catch {
// Error handling
}
可解码对象请求
请求
支持使用 Decodable
对象进行自动 JSON 解码
以下是一个 请求
的示例,用于从 /user
端点获取 Decodable
的 User
对象。
do {
try Request(.get,
atPath: "/user")
.perform(decoding: User.self) { user, response, error in
// Your completion handler here
}
} catch {
// Error handling
}