SwiftRestModel 2.0.4

SwiftRestModel 2.0.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2016
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Juraj Hilje.



 
Depends on:
Alamofire~> 4.0.1
SwiftyJSON~> 3.1.0
HTTPStatusCodes~> 3.1.0
 

  • By
  • Juraj Hilje

codebeat badge

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.