iOS 的 Rage 可编码扩展
GithubUser
假设我们有一个可编码对象 let user = GithubUser(name: "ArtemCherkasov")
请求体
request.withBody().bodyJson(user) // Adds body data to request as JSON string
存根
request.stub(user) // Adds stub data as JSON string
执行
同步
let userResult: Result<GithubUser, RageError> = request.executeObject() // Parse response from JSON string to GithubUser model
let usersResult: Result<[GithubUser], RageError> = request.executeArray() // Works for arrays too
异步操作
相同的,在异步请求中使用enqueue(_:)
函数
request.enqueueObject { (userResult: Result<GithubUser, RageError>) in
// Handle result in main thread
}
request.enqueueArray { (usersResult: Result<[GithubUser], RageError>) in
// Handle result in main thread
}
RxSwift
let userObjectObservable: Observable<GithubUser, RageError> = request.executeObjectObservable() // Where GithubUser is Codable
let usersObjectObservable: Observable<[GithubUser], RageError> = request.executeArrayObservable() // Works for arrays too
创建Multipart TypedObjects JSON数据
可以使用Encodable.makeTypedObject()
函数从任何Codable对象创建TypedObject
let typedObject = user.makeTypedObject() // Creates typed object with mimeType application/json and Data of JSON string
JSONDecoder和JSONEncoder
JSONDecoder
可以使用JSONDecoder对象在execute()
和enqueue()
函数中自定义Codable序列化
let jsonDecoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.YYYY"
jsonDecoder.dateDecodingStrategy = .formatted(formatter)
jsonDecoder.dataDecodingStrategy = .base64
let userResult: Result<GithubUser, RageError> = request.executeObject(decoder: jsonDecoder)
let usersResult: Result<[GithubUser], RageError> = request.executeArray(decoder: jsonDecoder)
request.enqueueObject(decoder: jsonDecoder) { (userResult: Result<GithubUser, RageError>) in
// Handle result in main thread
}
request.enqueueArray(decoder: jsonDecoder) { (usersResult: Result<[GithubUser], RageError>) in
// Handle result in main thread
}
JSONEncoder
可以使用JSONEncoder对象在stub()
和bodyJson()
函数中自定义Codable反序列化
let jsonEncoder = JSONEncoder()
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.YYYY"
jsonEncoder.dateEncodingStrategy = .formatted(formatter)
jsonEncoder.dataEncodingStrategy = .base64
request.withBody().bodyJson(user, encoder: jsonEncoder)
request.stub(user, encoder: jsonEncoder)
安装(CocoaPods)
将此依赖项添加到 Podfile,然后执行 pod install
# Core subspec of RageCodableExtensions
pod 'RageCodableExtensions', '~> 0.3.0'
或者如果您想使用 RxSwift 功能,应使用这些 RageCodableExtensions 子规格
pod 'RageCodableExtensions/RxSwift', '~> 0.3.0'
许可证
The MIT License (MIT)
Copyright (c) 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.