TravisClient
Swift 4.x 的 Travis v3 API 客户端
| 阅读文档
安装
- 使用 CocoaPods 安装:
pod 'TravisClient'
- 使用 SPM 安装:
.package(url: "https://github.com/IainSmith/TravisClient", from: "0.2.0"),
快速入门
import TravisClient
let key: String = "YOUR_TRAVIS_API_KEY"
let client = TravisClient(token: key, host: .org)
client.activeBuilds { (result: Result<Meta<[Build]>, TravisError>) in
/// In swift 4.1 you can subscript directly into the Result
let activeBuildCount: Int? = result[\[Build].count]
let firstBuildPRTitle: String? = result[\.first?.pullRequestTitle]
}
Travis API 概念。
请阅读 Travis API 文档以获取更多信息。TravisClient 遵循官方 API 文档中的命名约定和概念。
最小化表示与标准表示。
每个模型对象有两种表示形式。包括所有属性的“标准表示”和包含一些属性的“最小表示”。
public struct MinimalJob: Codable, Minimal {
public let id: Int
}
public struct Job: Codable {
public let id: Int
public let number: String
public let state: String
// 10 other properties
}
如果您需要更多信息,可以使用client.follow(embed:completion:)
方法加载数据的“标准表示”。
let build: Meta<Build>
let minimalJob: Embed<MinimalJob> = build.jobs.first! // don't do this in production code
client.follow(embed: minimalJob) { fullJob in
print(fullJob[\.state])
}
用法
import TravisClient
let key: String = "YOUR_TRAVIS_API_KEY"
let client = TravisClient(token: key, host: .org)
client.activeBuilds { (result: Result<Meta<[Build]>, TravisError>) in
#if swift(>=4.1)
/// In swift 4.1 you can subscript directly into the Result
let activeBuildCount: Int? = result[\Build.id]
#else
/// In swift 4.0 you need to subscript into the optional value of a result.
let resultBuildNumber: Int? = result.value?[\.id]
#endif
/// You can also switch over the result
switch result {
case success(let builds: Meta<[Build]>):
// Find the number of active builds
builds[\.count])
// Find the jobs associated with this build
guard let job: Embed<MinimalJob> = jobs[\.first] else { return }
// Each API call returns one resource that has a 'standard representation' full object in this case supports hyper media so you can easily load the full object in a second request.
client.follow(job) { (jobResult: Meta<Job>) in
print(jobResult)
}
// Or follow a paginated request
client.follow(builds.pagination?.next) { nextPage in
print(nextPage)
}
case error(let error):
// handle error
print(error)
}
}
运行测试
# JSON parsing tests
> swift test --filter TravisClientTests.JSONTests
# Hit the travis.org API
> TRAVIS_TOKEN=YOUR_TOKEN_HERE swift test
待办事项
- 支持分页请求
- 添加用户模型
- 添加简单查询参数
- 添加阶段模型
- 添加更多类型化的排序参数
- 支持类型安全的预加载。