SwiftQuests

一个基于 URLSession 的面向对象的网络库。

Swift GitHub tag (latest by date) Codecov License Cocoapods LastCommit

安装

Swift Package Manager

通过 URL 将 Swift Package Dependency 添加到您的项目

https://github.com/Ast3r10n/swiftquests

Cocoapods

SwiftQuests 添加到您的 Podfile

pod 'SwiftQuests'

用法

一个 Request 是一个基本的、独立的、与单个关联任务相关的对象,通过其初始化程序进行配置。一旦初始化,Request(在很大程度上)是不可变的。 它的任务仅通过 perform 方法调用来启动。

基本请求

要执行一个基本的 Request,先初始化它

do {
  let request = try Request(.get,
                            atPath: "/user")
} catch {
  // Error handling
}

然后调用 perform 方法来启动其关联的任务。

do {
  try request.perform { result in
    // Response implementation
  }
} catch {
  // Error handling
}

可解码对象请求

Request 支持使用 Decodable 对象自动进行 JSON 解码

这是一个从 /user 端点获取 Decodable User 对象的 Request 示例。

do {
  try Request(.get,
              atPath: "/user")
    .perform(decoding: User.self) { result in
    
    // Your completion handler here
  }
} catch {
  // Error handling
}