一个使用 Apple NIO 编写的超级易用的 Github API 客户端库
以下列表可能不包含规范中的所有方法,但这是一个好的开始...
.package(url: "https://github.com/Einstore/GitHubKit.git", from: "1.0.0")
不要忘记您的目标
.target(
name: "App",
x: [
"GitHubKit"
]
)
let config = Github.Config(
username: "orafaj",
token: token,
server: "https://github.ford.com/api/v3/"
)
let github = try Github(config)
// or
let github = try Github(config, eventLoop: eventLoop)
// or
let github = try Github(config, eventLoopGroupProvider: .createNew)
虽然此库仅通过 Vapor 4 测试,但使用 Apple NIO HHTPClient 应与任何 iOS 或服务器端项目兼容。
请在我们的 slack 上告诉我们您的使用情况,或者您需要任何支持!
import GitHubKit
// In configure.swift
services.register(Github.self) { container in
let config = Github.Config(
username: "orafaj",
token: token,
server: "https://github.ford.com/api/v3/"
)
return try Github(config, eventLoop: container.eventLoop)
}
// In routes (or a controller)
r.get("github", "organizations") { req -> EventLoopFuture<[Organization]> in
let github = try c.make(Github.self)
return try Organization.query(on: github).getAll().map() { orgs in
print(orgs)
return orgs
}
}
添加新的 API 调用也非常简单,令人惊讶
假设您需要添加用户的详细信息
https://developer.github.com/v3/users/
复制示例 JSON,例如
{
"login": "octocat",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false,
"name": "monalisa octocat",
"company": "GitHub",
"blog": "https://github.com/blog",
"location": "San Francisco",
"email": "octocat@github.com",
"hireable": false,
"bio": "There once was...",
"public_repos": 2,
"public_gists": 1,
"followers": 20,
"following": 0,
"created_at": "2008-01-14T04:33:35Z",
"updated_at": "2008-01-14T04:33:35Z"
}
转到 https://app.quicktype.io 并将 JSON 转换为模型... 您可能需要稍微调整设置,以使模型与项目中已有的模型保持一致。此外,任何子结构(除非可以在其他地方使用)都应移到父模型内部。
哦,对了……将主类命名为 User
!:)……
导入 Vapor
并使主模型符合 Content
而不是 Codable
。
首先,您需要使 User
模型符合 Queryable
。 这将启用 User.query(on: container)
方法。
extension User: Queryable { }
接下来,我们在 QueryableProperty
上创建一个扩展,每次您从上一步请求容器上的查询时都会生成该扩展。 确保您指定 QueryableType == User
extension QueryableProperty where QueryableType == User {
/// Get all organizations for authenticated user
public func get() throws -> EventLoopFuture<User> {
return try github.get(path: "user")
}
}
您应该能够调用 try User.query(on: c).get()
并获得包含已验证用户详细信息的 EventLoopFuture<User>
。
Ondrej Rafaj @rafiki270
(毕竟不是我的 token,是吗?!)
MIT;有关详细信息,请参见 LICENSE 文件。