SwiftRestModel is a small helper class for communicating with RESTful APIs using Alamofire and SwiftyJSON.
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>
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
let model = SwiftRestModel(rootUrl: "http://jsonplaceholder.typicode.com/posts")
model.fetch()
// GET "/posts"
model.fetch(success: {
response in
print(response)
// or print(model.data)
})
model.fetch(error: {
response in
print(response)
})
- fetch()
- save()
- destory()
- request()
model.save(data: ["foo": "bar"])
// POST "/posts" {foo: bar}
Default parameters:
- data: [:]
- encoding: JSONEncoding.default
- success: nil
- error: nil
model.fetch(data: ["foo": "bar"])
// GET "/posts?foo=bar"
Default parameters:
- data: [:]
- success: nil
- error: nil
model.data["id"] = 1
model.save(data: ["foo": "bar"])
// PUT "/posts/1" {foo: bar}
Default parameters:
- data: [:]
- encoding: JSONEncoding.default
- success: nil
- error: nil
model.destroy()
// DELETE "/posts/1"
Default parameters:
- success: nil
- error: nil
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
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"
- 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.
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.
See LICENSE.