Last active
July 11, 2017 12:48
-
-
Save yabenatti/28ec34a0ac3a35c336cbb79120886034 to your computer and use it in GitHub Desktop.
Alamofire Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Alamofire | |
class AccessTokenAdapter: RequestAdapter { | |
private let accessToken: String | |
init(accessToken: String) { | |
self.accessToken = accessToken | |
} | |
func adapt(_ urlRequest: URLRequest) throws -> URLRequest { | |
var urlRequest = urlRequest | |
if (urlRequest.url?.absoluteString.hasPrefix(Urls.URL_BASE))! { | |
urlRequest.setValue("Token " + accessToken, forHTTPHeaderField: "Authorization") | |
} | |
return urlRequest | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Alamofire | |
class NetworkManager { | |
static let shared = NetworkManager() | |
private let manager: Alamofire.SessionManager = { | |
let configuration = URLSessionConfiguration.default | |
configuration.timeoutIntervalForRequest = 10 | |
configuration.timeoutIntervalForResource = 10 | |
return Alamofire.SessionManager(configuration: configuration) | |
}() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Alamofire | |
class RaffleManager { | |
static let shared = RaffleManager() | |
private let manager: Alamofire.SessionManager = { | |
let configuration = URLSessionConfiguration.default | |
configuration.timeoutIntervalForRequest = 10 | |
configuration.timeoutIntervalForResource = 10 | |
return Alamofire.SessionManager(configuration: configuration) | |
}() | |
func getRaffles(completHandler: @escaping (Bool) -> ()) { | |
manager.adapter = AccessTokenAdapter(accessToken: AppUtils.retrieveFromUserDefaultWithKey(key: Constants.kApiToken) as! String) | |
manager.request(Urls.URL_RAFFLE).validate().responseJSON { (response) in | |
debugPrint(response) | |
debugPrint(response.result) | |
//to get status code | |
if let status = response.response?.statusCode { | |
switch(status){ | |
case 200: | |
print("example success") | |
default: | |
print("error with response status: \(status)") | |
//completion([false]) | |
} | |
} | |
guard response.result.isSuccess else { | |
print(response.result.isFailure.description) | |
return | |
} | |
//to get JSON return value | |
debugPrint(response.result.value as? [String: Any] ?? ["nothing" : false]) | |
guard let responseJSON = response.result.value as? [String: Any], | |
let dataJSON = responseJSON["data"] as? [String: Any] else { | |
print("Invalid raffle information received from the service") | |
return | |
} | |
debugPrint(dataJSON) | |
completHandler(true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment