Jarvis is a lightweight network abstraction layer, built on top of Alamofire. It can be used to dramatically simplify client-to-server communication.
- Generic, protocol-based implementation
- Support for iOS/macOS/tvOS/watchOS/
- Easy to work with MultipartFormData
- Support Codable protocol in Swift 4
- Ability to log Response
We designed Jarvis to be simple to use and also very easy to customize. After initial setup, using Jarvis is very straightforward:
api.getPosts(for: page) { response in
switch response.result {
case .success(let posts):
print("Received Posts: \(posts)")
case .failure(let error):
print("Posts request failed, parsed error: \(error)")
}
}
- Import
Jarvis
module to yourAPIClient
class
import Jarvis
- Add implementation for
RequestDataProvider
protocol.RequestDataProvider
has default implementation, so you can set just properties you need:
struct PostsListAPIClient {
enum Route: RequestDataProvider {
case get(page: Int)
var method: HTTP.Method {
return .get
}
var baseUrl: URLConvertible {
return URL(string: "http://jsonplaceholder.typicode.com")!
}
var path: String {
switch self {
case .get:
return "posts"
}
}
var parameters: Parameters? {
switch self {
case .get(let page):
return ["page": page]
}
}
}
}
- Make you sure that you model support protocol
Decodable
struct Post: Decodable {
let id: Int
let userId: Int
let title: String
let body: String
}
- Sending requests
func getPosts(for page: Int, completion: @escaping (_ response: Response<[Post], APIError>) -> Void) {
let request = Route.get(page: page)
Jarvis.request(request).responseDecodable(completion: completion)
}
- Handling response
api.getPosts(for: page) { response in
switch response.result {
case .success(let posts):
print("Received Posts: \(posts)")
case .failure(let error):
print("Posts request failed, parsed error: \(error)")
}
}
To integrate Jarvis
into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target 'TargetName' do
pod 'Jarvis'
end
Then, run the following command:
$ pod install
Jarvis is available under the MIT license. See the LICENSE file for more info.