测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2017年6月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Fahid Attique 维护。
依赖关系 | |
Alamofire | >= 0 |
ObjectMapper | >= 0 |
AlamofireObjectMapper | >= 0 |
QorumLogs | >= 0 |
Routable.swift
Routable.swift
中找到其实现ServiceManager.swift
文件enum Endpoint
以添加应用程序特定的端点。使其符合 Directible 以从端点创建可定位的 URL。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
}
}
ServiceManager
,并使其符合协议 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
}
}
在您的控制器中,您可以使用ServiceManager
来管理您的网络调用
以下是一个简单API调用的示例:
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在MIT许可下可用。有关更多信息,请参阅LICENSE文件。
法希德·阿蒂克 - (https://github.com/fahidattique55)