A lightweight URLSession wrapper for basic network requirements.
Download and drag(or add using Xcode) below Files into Target
- RequestBuilder.swift
- RestClient.swift
- Change baseURL in RequestBuilder.swift file
fileprivate let baseURL : String = "http://api.com/"
Create an API file with all endpoints. refer DemoAPI.swift
class API {
//GET Query parameters
func getQuery(skip:Int,take:Int) -> Request {
return RequestBuilder(method: .GET, url: "api/get")
.setHeaders(["Content-Type":"application/json", "Accept":"application/json"])
.addHeader(name: "Authorization", value: "access_token")
.addQuery(name: "skip", value: skip)
.addQuery(name: "take", value: take)
.build()
}
//GET path parameters
func getPath(id:Int) -> Request {
return RequestBuilder(method: .GET, url: "api/issue/{id}")
.setHeaders(["Content-Type":"application/json", "Accept":"application/json"])
.addHeader(name: "Authorization", value: "access_token")
.addPath(name: "id", value: id)
.build()
}
//POST fields
func post1(username:String,password:String) -> Request {
return RequestBuilder(method: .POST, url: "token")
.addField(name: "username", value: username)
.addField(name: "password", value: password)
.addField(name: "grant_type", value: "password")
.build()
}
//POST Model as request body, Model must be a type of Codable
func post2(model: [String : String]) -> Request {
return RequestBuilder(method: .POST, url: "api/user")
.setHeaders(["Content-Type":"application/json", "Accept":"application/json"])
.addHeader(name: "Authorization", value: "access_token")
.setBody(body: model)
.build()
}
//multipart file upload
func uploadFile(withParams: [String : String]?, media: [Media]?) -> Request {
return RequestBuilder(method: .POST, url: "tempfiles")
.setMultipartData(withParameters: withParams, media:media)
.build()
}
}
With response body
API().getPath(id: 1)
.handleResponse(.OnMainThread)
.execute(onSuccess: { (response:MyCodableClass) in
//handle response
}) { (error) in
//handle error
}
Without response body
API().getQuery(skip: 0, take: 10)
.handleResponse(.OnBackground)
.execute(onSuccess: {
//handle response
}) { (error) in
//handle error
}