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 |
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 [email protected]: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)
})
model.save(data: ["foo": "bar"])
// POST "/posts" {foo: bar}
Default parameters:
model.fetch(data: ["foo": "bar"])
// GET "/posts?foo=bar"
Default parameters:
model.data["id"] = 1
model.save(data: ["foo": "bar"])
// PUT "/posts/1" {foo: bar}
Default parameters:
model.destroy()
// DELETE "/posts/1"
Default parameters:
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:
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"
Ready to submit a fix or a feature? Submit a pull request! And please:
See LICENSE.