Skip to content

Rentlio/SwiftRestModel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

db610f2 · Oct 21, 2016
Oct 8, 2016
Oct 8, 2016
Oct 11, 2016
Oct 21, 2016
Oct 21, 2016
Feb 4, 2016
Oct 21, 2016
Oct 21, 2016
Feb 1, 2016
Oct 21, 2016
Oct 21, 2016
Oct 21, 2016

Repository files navigation

SwiftRestModel

codebeat badge 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:

platform :ios, '9.0'
use_frameworks!

target 'MyApp' do
    pod 'SwiftRestModel'
end

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 [email protected]: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: JSONEncoding.default
  • 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: JSONEncoding.default
  • 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: URLEncoding.default,
    success : {
        response in
        print(response)
    },
    error   : {
        response in
        print(response)
    }
)
// GET "/posts?foo=bar"

Default parameters:

  • method: "get"
  • url: ""
  • data: [:]
  • headers: [:]
  • encoding: URLEncoding.default
  • 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.