Advertisement
Guest User

Untitled

a guest
Dec 9th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.29 KB | None | 0 0
  1. //: Vahana Playground 2 - figuring out class structure
  2.  
  3. import Foundation
  4. import Alamofire
  5.  
  6. import XCPlayground
  7. XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
  8.  
  9. class OSVBase: NSObject {
  10.     func log(level: String, message: String) {
  11.         print("\(level.uppercaseString): \(message)")
  12.     }
  13.  
  14.     func error(message: String) {
  15.         self.log("error", message: message)
  16.     }
  17.  
  18.     func warn(message: String) {
  19.         self.log("warn", message: message)
  20.     }
  21.  
  22.     func debug(message: String) {
  23.         self.log("debug", message: message)
  24.     }
  25. }
  26.  
  27. class OSVRest: OSVBase {
  28.     func GET(url: String, completionHandler: (Int, AnyObject) -> Void) -> Void {
  29.         Alamofire.request(.GET, url).responseJSON { response in
  30.             let statusCode = response.response!.statusCode
  31.             var json:AnyObject = []
  32.  
  33.             switch response.result {
  34.                 case .Failure(let error):
  35.                     self.error("GET Request failed: \(error)")
  36.                 case .Success:
  37.                     self.debug("GET Request completed, calling handler")
  38.                     json = response.result.value!
  39.             }
  40.  
  41.             completionHandler(statusCode, json)
  42.         }
  43.     }
  44.  
  45.     func POST(url: String, parameters: [String: AnyObject]?, completionHandler: (Int, AnyObject) -> Void) -> Void {
  46.         Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON).responseJSON { response in
  47.             let statusCode = response.response!.statusCode
  48.             var json:AnyObject = []
  49.  
  50.             switch response.result {
  51.                 case .Failure(let error):
  52.                     self.error("POST Request failed: \(error)")
  53.                 case .Success:
  54.                     self.debug("POST Request completed, calling handler")
  55.                     json = response.result.value!
  56.             }
  57.  
  58.             completionHandler(statusCode, json)
  59.         }
  60.     }
  61. }
  62.  
  63. class OSVIdentity: OSVBase {
  64.     let rest = OSVRest()
  65.     var authURL: String
  66.     var username: String
  67.     var password: String
  68.     var token: AnyObject?
  69.  
  70.     init(url: String, username: String, password: String) {
  71.         self.authURL = url + "/auth/tokens"
  72.         self.username = username
  73.         self.password = password
  74.         self.token = nil
  75.     }
  76.  
  77.     func getToken() -> Void {
  78.         let parameters = [
  79.             "auth": [
  80.                 "identity": [
  81.                     "methods": ["password"],
  82.                     "password": [
  83.                         "user": [
  84.                             "name": self.username,
  85.                             "domain": [ "id": "default" ],
  86.                             "password": self.password,
  87.                         ]
  88.                     ]
  89.                 ]
  90.             ]
  91.         ]
  92.  
  93.         self.rest.POST(self.authURL, parameters: parameters, completionHandler: {statusCode, response -> Void in
  94.             self.debug("In getToken handler")
  95.             self.debug("Got result from code: \(statusCode)")
  96.             debugPrint(response)
  97.             if (statusCode == 201) {
  98.                 self.debug("Storing token")
  99.                 self.token = response
  100.             }
  101.         })
  102.     }
  103. }
  104.  
  105. let identity = OSVIdentity(url: "http://10.0.88.166:5000/v3", username: "admin", password: "lol")
  106. identity.getToken()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement