Rage for iOS
iOS 应用程序的实际网络抽象层。
⚠️ 警告
该库尚未准备好生产使用。请在它变得更加稳定之前不要使用它。库的较高和较低版本之间可能存在兼容性问题,直到版本 1.0.0。
当前文档可能与真实 API 不同,因为它不稳定且正在快速改进。
功能
- 单行请求。
🎯 - 以可读性极高的方式描述 API 规范。
📚 - 使用 Codable 的 JSON 支持开箱即用。
📒 - 开箱即用的 RxSwift 支持。
🚀 - 操纵严格类型的对象而不是 String 字典的地狱。
📦
使用
您可以在 RageExample 项目中查看示例实现。
首先您需要使用构建者模式创建 RageClient。
let client = Rage.builderWithBaseUrl("https://api.github.com")
.withContentType(.json)
.withHeaderDictionary([
"Api-Version": "1.1",
"Platform": "iOS"
])
.withPlugin(LoggingPlugin(logLevel: .full))
.withAuthenticator(MyAuthenticator())
.build()
然后像这样描述您的 API 请求。这是一种声明请求及其后台执行的方式。
func getUser(username: String, completion: @escaping Result<RageResponse, RageError> -> ()) {
client.get("/users/{user}")
.request()
.path("user", username)
.enqueue(completion)
}
// Async
func getRepositoriesForOrganization(organizationTitle: String, completion: @escaping Result<RageResponse, RageError> -> ()) {
client.get("/orgs/{org}/repos")
.request()
.path("org", organizationTitle)
.enqueue(completion)
}
// Sync
func getRepositoriesForOrganizationSync(organizationTitle: String) -> Result<RageResponse, RageError> {
return client.get("/orgs/{org}/repos")
.request()
.path("org", organizationTitle)
.execute()
}
使用 RxSwift ,您可以用这种方式声明 API。
func getUser(username: String) -> Observable<GithubUser> {
return client.get("/users/{user}")
.request()
.path("user", username)
.executeObjectObservable()
}
func getRepositoriesForOrganization(organizationTitle: String) -> Observable<[GithubRepository]> {
return client.get("/orgs/{org}/repos")
.request()
.path("org", organizationTitle)
.executeArrayObservable()
}
就是这样。紧凑但强大。
安装(CocoaPods)
在Podfile中添加此依赖项并执行pod install
# Core subspec of Rage
pod 'Rage', '~> 0.17.1'
如果您想使用RxSwift功能,则应使用这些Rage子规范
# RxSwift only
pod "Rage/RxSwift", "~> 0.17.1"
许可
The MIT License (MIT)
Copyright (c) 2016-2018 gspd.mobi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.