Springbok is a light and fast HTTP Networking in Swift working with Codable.
- Chainable Request / Response Methods
- Request handle is background and Response in the main thread
- Works with Codable !
- UIImageView extension to set image
- UIImage set in cache (only memory for now)
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Springbok into your Xcode project using Carthage, specify it in your Cartfile
:
github "nodes-ios/Springbok"
Run carthage update
to build the framework and drag the built Springbok.framework
into your Xcode project.
struct User: Codable {
let id: Int
let name: String
...
}
import Springbok
Springbok
.request("<your_url>")
.responseCodable { (result: Result<User>) in
switch result {
case .success(let user):
// request successful, your model is decoded !
case .failure(let error):
// request failed ...
}
}
import Springbok
Springbok
.request(
"<your_url>",
method: .post, // .get, .post, .patch, .put, .delete
parameters: ["id": 1], // [String: Any]
headers: ["Content-Type": "application/json"] // [String: String]
)
.responseCodable { (result: Result<User>) in
switch result {
case .success(let playlists):
// request successful, your model is decoded !
case .failure(let error):
// request failed ...
}
}
If your server returns data like this :
{
"<root_object_name>": [
{
"id": 1,
"name": "John Doe"
},
...
]
}
You need to unwrap the response in order to decode it.
import Springbok
Springbok
.request("<your_url>")
.unwrap("root_object_name") // the name of your root object
.responseCodable { (result: Result<User>) in
switch result {
case .success(let user):
// request successful, your model is decoded !
case .failure(let error):
// request failed ...
}
}
You can download images and set into imageView like :
import Springbok
// without option (by default fading animation is true, duration is 0.3 and placeholder image is nil)
imageView.setImage(url: "<your_url>")
// With options
imageView.setImage(url: "", fading: true, duration: 0.5, placeholderImage: nil)
You can also cancel download image task by doing :
import Springbok
imageView.cancelDownloadTask()
You can cancel request with the given url :
import Springbok
Springbok.cancelRequest(url: "<your_url>")