Skip to content

linebreak-co/SwiftRestModel

 
 

Repository files navigation

SwiftRestModel

Build Status Language

SwiftRestModel is a small helper class for communicating with RESTful APIs using Alamofire and SwiftyJSON.

Dependencies

Integration

You can use CocoaPods to install SwiftRestModel by adding it to your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'SwiftRestModel'

App Transport Security is blocking a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Example Project

You'll need to install CocoaPods first.

Grab the source code, and then install dependencies.

$ git clone git@github.com:Rentlio/SwiftRestModel.git
$ cd SwiftRestModel
$ pod install
$ open SwiftRestModel.xcworkspace

Basic Usage

Making a Request

let model = SwiftRestModel(rootUrl: "http://jsonplaceholder.typicode.com/posts")

model.fetch()
// GET "/posts"

Success Handling

model.fetch(success: {
    response in
    print(response)
    // or print(model.data)
})

Error Handling

model.fetch(error: {
    response in
    print(response)
})

Methods

  • fetch()
  • save()
  • destory()
  • request()

Create

model.save(data: ["foo": "bar"])
// POST "/posts" {foo: bar}

Default parameters:

  • data: [:]
  • encoding: .JSON
  • success: nil
  • error: nil

Read

model.fetch(data: ["foo": "bar"])
// GET "/posts?foo=bar"

Default parameters:

  • data: [:]
  • success: nil
  • error: nil

Update

model.data["id"] = 1

model.save(data: ["foo": "bar"])
// PUT "/posts/1" {foo: bar}

Default parameters:

  • data: [:]
  • encoding: .JSON
  • success: nil
  • error: nil

Delete

model.destroy()
// DELETE "/posts/1"

Default parameters:

  • success: nil
  • error: nil

Request

model.request(
    method  : "get",
    url     : "http://jsonplaceholder.typicode.com/posts",
    data    : ["foo": "bar"],
    headers : ["Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="],
    encoding: .URL,
    success : {
        response in
        print(response)
    },
    error   : {
        response in
        print(response)
    }
)
// GET "/posts?foo=bar"

Default parameters:

  • method: "get"
  • url: ""
  • data: [:]
  • headers: [:]
  • encoding: .URL
  • success: nil
  • error: nil

Models

Subclass SwiftRestModel to organize your API models

class Posts: SwiftRestModel {
    
    let url = "http://jsonplaceholder.typicode.com/posts"
    
    init() {
        super.init(rootUrl: self.url)
    }
    
    // Custom Endpoint
    func fetchFirst(data data: Dictionary<String, AnyObject> = [:], success: ((response: JSON) -> ())? = nil, error: ((response: JSON) -> ())? = nil) {
        self.request(method: "get", url: self.rootUrl + "/first", data: data, success: success, error: error)
    }
    
}
let posts = Posts()

posts.fetch()
// GET "/posts"

posts.fetchFirst()
// GET "/posts/first"

posts.fetchFirst(
    data   : ["foo": "bar"],
    success: {
        response in
        print(response)
    },
    error  : {
        response in
        print(response)
    }
)
// GET "/posts/first?foo=bar"

Branches

  • master - The production branch. Clone or fork this repository for the latest copy.
  • develop - The active development branch. Pull requests should be directed to this branch.

Contribution

Ready to submit a fix or a feature? Submit a pull request! And please:

  • If code changes, run the tests and make sure everything still works.
  • Write new tests for new functionality.
  • Update documentation comments where applicable.
  • Maintain the existing style.

Contact

License

See LICENSE.

About

Swift helper for REST models

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 93.2%
  • Ruby 6.8%