TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jun 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Fahid Attique.
Depends on: | |
Alamofire | >= 0 |
ObjectMapper | >= 0 |
AlamofireObjectMapper | >= 0 |
QorumLogs | >= 0 |
Routable.swift
Routable.swift
ServiceManager.swift
file in your Xcode projectenum Endpoint
to add app specific end points. Conform it with Directable to make directable urls from your end points.
enum Endpoint: Directable {
// Setup the base Url of your application according to the application mode
static var baseUrl: String {
var baseUrl = ""
switch appMode {
case .test:
baseUrl = "http://apidev.accuweather.com/currentconditions"
case .production:
baseUrl = "http://apidev.accuweather.com/currentconditions"
}
return baseUrl
}
// Define some endpoints like this,
case weatherConditions,
countryList
// Implement the protocol method to make your app specific end points fully directable as Url
func directableURLString() -> String {
var servicePath = ""
switch (self) {
case .weatherConditions:
servicePath = "get-weather-conditions"
case .countryList:
servicePath = "get-countries-data"
}
let tail = "api"
return Endpoint.baseUrl + "/" + tail + "/" + servicePath
}
}
class ServiceManager
and conform it with protocol Routable
class ServiceManager: Routable {
// You need to implement this method to send App specific headers with your API calls
func authorizationHeadersIf(_ authorized: Bool) -> [String : String]? {
// You can send Open Auth Token, Appversion, API version and many more as per your need
return ["app-version":"1.0"]
}
// You need to implement this method to validate the error of your server and you can take many decisions here with server's error status code
func handleServerError(_ result: DataResponse<JSONTopModal>, failure: FailureErrorBlock!) -> Bool {
let resultValue = result.value!
if resultValue.isError {
if resultValue.statusCode == -1 {
// handleTokenError(resultValue.message)
}
else {
failure(NSError(errorMessage: resultValue.message, code: resultValue.statusCode))
}
return true
}
return false
}
}
In your controllers, you can use the ServiceManager
to manage your network calls
Example for a simple API call is given below,
fileprivate let manager:ServiceManager = ServiceManager()
manager.request(.get, service: Endpoint.countryList, success: { (response, jsonTopModelResult) in
print("proceed with your jsonTopModelResult")
}, failure: { (error) in
print("Handle error")
})
manager.request(.get, service: Endpoint.countryList, authorized: true, success: { (response, jsonTopModelResult) in
print("proceed with your jsonTopModelResult")
}, failure: { (error) in
print("Handle error")
})
manager.requestForArray(.get, service: Endpoint.countryList, mapperClass: Country.self, success: { (response, countries) in
print("proceed with your countries list")
}, failure: { (error) in
print("Handle error")
})
manager.requestForObject(.get, service: Endpoint.weatherConditions, mapperClass: WeatherCondition.self, success: { (response, weatherConditions) in
print("proceed with your weather Conditions")
}, failure: { (error) in
print("Handle error")
})
NBUtility is available under the MIT license. See the LICENSE file for more info.
Fahid Attique - (https://github.com/fahidattique55)