Skip to content

damien-nd/ws

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ws ☁️ - Elegant JSON WebService in Swift

Language: Swift 2 Platform: iOS 8+ Carthage compatible License: MIT Build Status Release version

Reason - Example - Installation

let ws = WS("http://jsonplaceholder.typicode.com")

ws.get("/users").then { json in
    // Get back some json \o/
}

Why

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

How

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

What

  • Simple
  • Lightweight (1 file)
  • Pure Swift
  • No magic involved
  • Strongly Typed
  • Chainable
  • Uses popular Promise/Future concept

Usage

Import ws at the top of your file

import ws

Set webservice base URL

let ws = WS("http://jsonplaceholder.typicode.com")

Get back some json instantly \o/

ws.get("/users").then { json in
    print(json)
}

Design your Api

func latestUsers() -> Promise<[User]> {
    return ws.get("/users")
}

Tell ws how to map your user models

extension User:ArrowParsable {
    init(json: JSON) {
        identifier <-- json["id"]
        username <-- json["username"]
        email <-- json["email"]
    }
}

Get back some sweet swift models ❤️

latestUsers().then { users in
    print(users) // STRONGLY typed [Users] ❤️
}

Settings

Want to log all network calls and responses ?

ws.logLevels = .CallsAndResponses

Want to hide network activity indicator ?

ws.showsNetworkActivityIndicator = false

Api Example

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 {

}

Installation

Carthage

In your Cartfile

github "freshOS/ws"
  • Run carthage update
  • Drag and drop ws.framework from Carthage/Build/iOS to Linked 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.

Manually

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

Cocoapods is not supported at the moment.

And voila !

Other repos ❤️

ws is part of a series of lightweight libraries aiming to make developing iOS Apps a breeze :

About

Elegant JSON WebService for Swift ☁️

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.5%
  • Objective-C 1.5%