It simplifies your RESTful API calls, automatically convert the HttpResponse into specified Model as well as the Error using the new apple ’s Codable feature.
For example in a request for fetching specific user information and you have a User model, all you have to do is make the User model conforms to Codable and specify it when using the RequestCaller.
{
"name":"kel",
"email":"me@iamkel.net"
}
User model that conforms to Codable.
struct User: Codable {
var name:String
var email:String
}
This will automatically convert the response into an instance User model.
RxRetroSwift provided a typealias ErrorCodable which is a combination of HasErrorInfo and Decodable protocol:
public typealias DecodableError = Decodable & HasErrorInfo
For example, the json error response of your login request is
{
"message": "Unable to login."
"details": {
"password": "You changed your password 2 months ago."
}
}
And you have this as Model:
struct ErrorModel {
var errorCode:Int?
var message:String
var details:[String:String]
}
How about dealing to a request that don’t expect to return an object or model?
RxRetroSwift provide a method that will return Observable<Result<RawResponse>, DecodableErrorModel>>.
public func call<DecodableErrorModel:DecodableError>(_ request: URLRequest)
-> Observable<Result<RawResponse, DecodableErrorModel>>
public struct RawResponse {
public var statusCode:Int
public var data:Data?
init(statusCode:Int, data:Data?) {
self.statusCode = statusCode
self.data = data
}
}
Example
To run the example project, clone the repo, and run pod install from the Example directory first.
RxRetroSwift
What does it do?
It simplifies your RESTful API calls, automatically convert the
HttpResponse
into specified Model as well as the Error using the new apple ’s Codable feature.For example in a request for fetching specific user information and you have a
User
model, all you have to do is make the User model conforms to Codable and specify it when using the RequestCaller.User
model that conforms to Codable.This will automatically convert the response into an instance
User
model.Example:
Let say it’s an array of users; since Array conforms to Codable, all you have to do is specify the type to be
[User]
.Example:
About handling ResponseError:
RxRetroSwift provided a typealias ErrorCodable which is a combination of HasErrorInfo and Decodable protocol:
For example, the json error response of your login request is
And you have this as Model:
How about dealing to a request that don’t expect to return an object or model?
RxRetroSwift provide a method that will return Observable<Result<RawResponse>, DecodableErrorModel>>.
Example
To run the example project, clone the repo, and run
pod install
from the Example directory first.Requirements
Features
TODO
URLRequest
.Installation
Cocoapods
RxRetroSwift is now available through CocoaPods. To install it, simply add the following line to your Podfile:
Swift Package Manager
Sample implementation
Using JSONPlaceholder API. You can also check the Sample Project
Testing
Contributions
Just feel free to submit pull request or suggest anything that would be useful.
Author
Michael Henry Pantaleon, me@iamkel.net
License
RxRetroSwift is available under the MIT license. See the LICENSE file for more info.