Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: Vahana Playground 2 - figuring out class structure
- import Foundation
- import Alamofire
- import XCPlayground
- XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
- class OSVBase: NSObject {
- func log(level: String, message: String) {
- print("\(level.uppercaseString): \(message)")
- }
- func error(message: String) {
- self.log("error", message: message)
- }
- func warn(message: String) {
- self.log("warn", message: message)
- }
- func debug(message: String) {
- self.log("debug", message: message)
- }
- }
- class OSVRest: OSVBase {
- func GET(url: String, completionHandler: (Int, AnyObject) -> Void) -> Void {
- Alamofire.request(.GET, url).responseJSON { response in
- let statusCode = response.response!.statusCode
- var json:AnyObject = []
- switch response.result {
- case .Failure(let error):
- self.error("GET Request failed: \(error)")
- case .Success:
- self.debug("GET Request completed, calling handler")
- json = response.result.value!
- }
- completionHandler(statusCode, json)
- }
- }
- func POST(url: String, parameters: [String: AnyObject]?, completionHandler: (Int, AnyObject) -> Void) -> Void {
- Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON).responseJSON { response in
- let statusCode = response.response!.statusCode
- var json:AnyObject = []
- switch response.result {
- case .Failure(let error):
- self.error("POST Request failed: \(error)")
- case .Success:
- self.debug("POST Request completed, calling handler")
- json = response.result.value!
- }
- completionHandler(statusCode, json)
- }
- }
- }
- class OSVIdentity: OSVBase {
- let rest = OSVRest()
- var authURL: String
- var username: String
- var password: String
- var token: AnyObject?
- init(url: String, username: String, password: String) {
- self.authURL = url + "/auth/tokens"
- self.username = username
- self.password = password
- self.token = nil
- }
- func getToken() -> Void {
- let parameters = [
- "auth": [
- "identity": [
- "methods": ["password"],
- "password": [
- "user": [
- "name": self.username,
- "domain": [ "id": "default" ],
- "password": self.password,
- ]
- ]
- ]
- ]
- ]
- self.rest.POST(self.authURL, parameters: parameters, completionHandler: {statusCode, response -> Void in
- self.debug("In getToken handler")
- self.debug("Got result from code: \(statusCode)")
- debugPrint(response)
- if (statusCode == 201) {
- self.debug("Storing token")
- self.token = response
- }
- })
- }
- }
- let identity = OSVIdentity(url: "http://10.0.88.166:5000/v3", username: "admin", password: "lol")
- identity.getToken()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement