Reason - Example - Installation
let ws = WS("http://jsonplaceholder.typicode.com")
ws.get("/users").then { json in
// Get back some json \o/
}
Because JSON apis are used in 99% of iOS Apps, this should be simple.
We developers should focus on our app logic rather than boilerplate code .
Less code is better code
By providing a lightweight client that automates boilerplate code everyone has to write.
By exposing a delightfully simple api to get the job done simply, clearly, quickly.
Getting swift models from a JSON api is now a problem of the past
- Simple
- Lightweight (1 file)
- Pure Swift
- No magic involved
- Strongly Typed
- Chainable
- Uses popular Promise/Future concept
import ws
let ws = WS("http://jsonplaceholder.typicode.com")
ws.get("/users").then { json in
print(json)
}
func latestUsers() -> Promise<[User]> {
return ws.get("/users")
}
extension User:ArrowParsable {
init(json: JSON) {
identifier <-- json["id"]
username <-- json["username"]
email <-- json["email"]
}
}
latestUsers().then { users in
print(users) // STRONGLY typed [Users] ❤️
}
Want to log all network calls and responses ?
ws.logLevels = .CallsAndResponses
Want to hide network activity indicator ?
ws.showsNetworkActivityIndicator = false
Here is a Typical CRUD example for Articles :
extension Article {
static func list() -> Promise<[Article]> {
return ws.get("/articles")
}
func save() -> Promise<Article> {
return ws.post("/articles", params: ["name":name])
}
func fetch() -> Promise<Article> {
return ws.get("/articles/\(id)")
}
func update() -> Promise<Void> {
return ws.put("/articles/\(id)", params: ["name":name])
}
func delete() -> Promise<Void> {
return ws.delete("/articles/\(id)")
}
}
Here is how we use it in code :
// List Articles
Article.list().then { articles in
}
// Create Article
var newArticle = Article(name:"Cool story")
newArticle.save().then { createdArticle in
}
// Fetch Article
var existingArticle = Article(id:42)
existingArticle.fetch().then { fetchedArticle in
}
// Edit Article
existingArticle.name = "My new name"
existingArticle.update().then {
}
// Delete Article
existingArticle.delete().then {
}
In your Cartfile
github "freshOS/ws"
- Run
carthage update
- Drag and drop
ws.framework
fromCarthage/Build/iOS
toLinked Frameworks and Libraries
(“General” settings tab) - Go to
Project
>Target
>Build Phases
+New run Script Phase
/usr/local/bin/carthage copy-frameworks
Add input files
$(SRCROOT)/Carthage/Build/iOS/ws.framework
$(SRCROOT)/Carthage/Build/iOS/Alamofire.framework
$(SRCROOT)/Carthage/Build/iOS/Arrow.framework
$(SRCROOT)/Carthage/Build/iOS/then.framework
This links ws and its dependencies.
Carthage is pretty useful since it takes care of pulling dependencies such as Arrow, then and Alamofire. What's cool is that it really is transparent. What I mean is that you could just use carthage on the side to pull and build dependencies and manually link frameworks to your Xcode project.
Without Carthage, I'd see 2 solutions : 1 - Copy paste all the source code : ws / then / Arrow / Alamofire which doesn't sound like a lot of fun ;) 2 - Manually link the frameworks (ws + dependencies) by A grabbing .frameworks them on each repo, or B use Carthage to build them
Cocoapods is not supported at the moment.
And voila !
ws is part of a series of lightweight libraries aiming to make developing iOS Apps a breeze :