Lightweight REST library for iOS and watchOS
RMHttp
Lightweight REST library for iOS and watchOS. Available on cococapods
RMHttp is a Lightweight REST library for iOS and watchOS.
Features
- [x] Chainable Request
- [x] URL / JSON Parameter Encoding
- [x] HTTP Methods GET/POST/DELETE/PATCH/PUT based in RFC2616
- [x] Custom Request Builder / HEADERS / PARAMETERS
- [x] Form-Data Support
- [x] Dynamic Response Handler (JSONObject, JSONArray, String)
- [x] Codable Support
TODO:
- [-] Support Upload/Download resource
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Installation
RMHttp is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'RMHttp'
Installation
pod "RMHttp"
Run the following command in your Terminal
$ pod install
Usage
HTPP Methods
HTTP Methods are declared in public enum RMHttpMethod
.
GET
, POST
, DELETE
, PUT
, PATCH
Parameter Encoding
Encoding are declared in public enum Encoding
URLEncoding
JSONEncoding
Serialization
JSONObject
- a representation of Dictionary<String, Any>
{
"data" : "value",
"isBoolean" : true,
"list": [
"object1",
"object2"
]
}
JSONArray
- a representation of [Dictionary<String, Any>]
[
{ "data1" : "value1"},
{ "data2" : "value2"},
{ "data3" : "value3"},
]
String
Any String respresentation (e.g HTML String, XML String, Plain Text)
Building Request
Building request with parameters from Dictionary type
let params = [
"string":"Ipsum", // String
"number": 100, // Number
"boolean":true // Boolean
] as [String : Any]
let urlString = "https://httpbin.org/get"
let request = RMRequest(urlString, method: .GET(.URLEncoding), parameters: params, hearders: nil)
Building request with parameters from RMParams
container
URL query representation names[]=lorem&names[]=ipsum&names[]=dolor&
let lorem = RMParams(key: "names[]", value: "lorem")
let ipsum = RMParams(key: "names[]", value: "ipsum")
let dolor = RMParams(key: "names[]", value: "dolor")
let params = [lorem, ipsum, dolor]
let urlString = "https://httpbin.org/post"
request = RMRequest(urlString, .GET(.URLEncoding), params, nil)
return request
Chained Response Handlers
Expecting Array object Response
RMHttp.JSON(request: request) { (response:JSONArray?, error) in
guard error == nil else {
self.title = "Response Error"
self.activity.stopAnimating()
self.textView.text = "\(err)"
return
}
self.activity.stopAnimating()
if let data = response {
self.title = "Response Sucess"
self.textView.text = "\(data)"
}
}
Expecting JSON object Response
RMHttp.JSON(request: request) { (response:JSONObject?, error) in
guard error == nil else {
self.title = "Response Error"
self.activity.stopAnimating()
self.textView.text = "\(err)"
return
}
self.activity.stopAnimating()
if let data = response {
self.title = "Response Sucess"
self.textView.text = "\(data)"
}
}
Generic method that return HTTP response has parameter data
that comply to RMHttpProtocol
(e.g JSONObject, JSONArray, String, )
public class func JSON<T:RMHttpProtocol>(request: RMRequest, completionHandler: @escaping Handler<T>)
FORM-DATA
Add fields from dictionary type
let params = [
"name":"lorem",
"lastName":"ipsum"
]
let urlString = "https://httpbin.org/post"
let request = RMRequest(urlString, .POST(.FomDataEncoding), params, nil)
return request
Add file
See Media Types
let request = RMRequest(url: URL(string: urlString)!)
request.addForm(field: "file", file: imgData, fileName: "image.jpeg", mimeType: "image/jpeg")
request.setHttp(method: .POST(.FomDataEncoding))
return request
Or manually add field
let request = RMRequest(url: URL(string: urlString)!)
request.addForm(fieldName: "field1", value: "lorem ipsum")
request.addForm(fieldName: "field2", value: "sit dolor amet")
Author
rogermolas, contact@rogermolas.com