An HTTP networking library for iOS in Swift 4
Drag Rest.swift file to your XCode project and set default setting as per your requirement.
- [x] Support all basic service type (GET, POST, PUT, PATCH, DELETE)
- [x] Multipart file Upload support
- [x] Request Cancel support
- [x] Inbuilt network logs support
- [x] Ios Network Activity Indicator support
- [x] Set parameter to request, url params and header to your request
- [x] You can set ypu time out for webservice
- [x] Status Codes acceptance for single or all request.
- [x] Network Activity Indicator support
- [ ] File Download support
RestError
is a custom error while creating request, or handle the response, you can print it using enabling logs in rest.
- showLogs(Bool): Rest will print logs for you or not, the default is true
- timeout(Double): Webservice timeout in second.
- index(Int): Webservice index use for debugging.
- activityIndicatorDisplay(Bool): Network Activity Indicator display default is true.
- statusCodes([Int]): The expected status call for the call, Default is from any[].
- cachePolicy: The NSURLRequest CachePolicy for Rest request
If you want to user Rest then must extend RestRequired protocol
in your service wrapper file.
Two things you need to set...
- Domain url as origin(
String
) - End points enums
Then just use Your class name and call()... your services
- route: String -> The route for the request
- requestType: HTTPMethod -> The requestType for the request
- expectedStatusCodes: [Int] -> The requestType for the request
- requestTimeoutSeconds -> The amount of time in
seconds
until the request times out, Default is from Rest default setting. - parameter : [String: Any] -> An optional set of params to send, What params you want to add in the request. Rest will do things right whether a method is GET or POST.
- URLParams : [Any] -> An optional set of URLParams to send: like user/4/post/10, What URLParams you want to add in the request. Rest will do things right whether a method is GET or POST.
- files: [File] -> Add files to Rest, POST only
- httpHeaders: [String: String] -> An optional set of HTTP Headers to send with the call.
- HTTPBodyRaw: (body: String, isJSON: Bool) -> An optional set of HTTP Raw body to send with the call, is JSON or not: will set "Content-Type" of HTTP request to "application/json" or "text/plain;charset=UTF-8"
- flow: Flow -> The request flow will be sync or aysnc, default to aysnc
- Drag
Rest.swift
file in your project - Create One service class with your requirement and follow the given Example pattern.
- Done....
class TestAPI: RestRequired { }
// Default settings
Rest.default.showLogs = true
Rest.default.activityIndicatorDisplay = true
// GET request
var option = RestOptions(route: End.users.route, method: .GET)
option.parameter = ["page": id]
option.expectedStatusCodes = [200, 404]
TestAPI.call(with: option) { (result) in
switch(result) {
case .success(_):
print("Success")
case .failure(_):
print("error")
}
}
// POST request
var option = RestOptions(route: End.login.route, method: .POST)
option.parameter = [ "email": email, "password": password]
option.expectedStatusCodes = [400, 200]
TestAPI.call(with: option) { (result) in
switch(result) {
case .success(_):
print("Success")
case .failure(_):
print("error")
}
}
// PUT request
var option = RestOptions(route: End.users.route, method: .PUT)
// user with id
option.URLParams = [id]
// update data
option.parameter = ["name": info.name, "job": info.job]
option.expectedStatusCodes = [200, 404]
TestAPI.call(with: option) { (result) in
switch(result) {
case .success(_):
print("Success")
case .failure(_):
print("error")
}
}
// DELETE request
var option = RestOptions(route: End.users.route, method: .DELETE)
// user with id
option.URLParams = [id]
option.expectedStatusCodes = [204]
TestAPI.call(with: option) { (result) in
switch(result) {
case .success(_):
print("Success")
case .failure(_):
print("error")
}
}
// Cancel request Test
// Create `CancellationSource` for handle when request is Cancel
var cs: CancellationSource = delayCall { (success, errorMsg) in
if !success {
print("error")
}
}
// This call when request cancelld
cs?.token.register {
print("request stoped")
}
// For Cancel web request
cs?.cancel()
// Web function
func delayCall(callback: @escaping (_ success: Bool, _ error: String?) -> ()) -> CancellationSource {
let cancellationSource = CancellationSource()
var option = RestOptions(route: End.users.route, method: .GET)
// user with id
option.URLParams = [2]
option.parameter = ["delay": 10]
option.expectedStatusCodes = [200, 404]
TestAPI.call(with: option, andCancelToken: cancellationSource.token) { (result) in
switch(result) {
case .success(_):
print("user successfully deleted")
callback(true, nil)
case .failure(let error):
callback(false, error.localizedDescription)
}
}
return cancellationSource
}
Rest welcomes contributions to our open source projects on Github.
Feel free to submit issues and enhancement requests.
Please refer to each project's style guidelines and guidelines for submitting patches and additions. In general, we follow the "fork-and-pull" Git workflow.
- Fork the repo on GitHub
- Clone the project to your own machine
- Commit changes to your own branch
- Push your work back up to your fork
- Submit a Pull request so that I can review your changes
NOTE: Be sure to merge the latest from "upstream" before making a pull request!