Skip to content

Commit

Permalink
Replaced Request with Alamofire
Browse files Browse the repository at this point in the history
  • Loading branch information
zaru committed Feb 20, 2017
1 parent 52583a2 commit 3225355
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions NotifyHub/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,36 +105,33 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele

func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
let url = NSURL(string: event!.paramDescriptorForKeyword(AEKeyword(keyDirectObject))!.stringValue!)
let querys = url!.query!.componentsSeparatedByString("=")
print(querys[1])
self.fetchAccessToken(querys[1])
let querys = url!.query!.componentsSeparatedByString("&")
let code = querys[0].componentsSeparatedByString("=")
self.fetchAccessToken(code[1])

popover.performClose(nil)
}

func fetchAccessToken(code: String) {
let keys = NotifyhubKeys()
print("fetchAccessToken")
print(code)

let url: NSURL = NSURL(string: "https://github.com/login/oauth/access_token")!
let body: NSMutableDictionary = NSMutableDictionary()
body.setValue(keys.gitHubClientId(), forKey: "client_id")
body.setValue(keys.gitHubClientSecret(), forKey: "client_secret")
body.setValue(code, forKey: "code")

let request: Request = Request()
let parameters = [
"client_id": keys.gitHubClientId(),
"client_secret": keys.gitHubClientSecret(),
"code": code
]

request.post(url, body: body, completionHandler: { data, response, error in
do {
let responseJson = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary
let accessToken = responseJson["access_token"]!
GitHubModel().setAccessToken(accessToken as! String)

Alamofire.request(.POST, "https://github.com/login/oauth/access_token", headers:["Accept": "application/json"], parameters: parameters, encoding: .URLEncodedInURL)
.validate()
.responseJSON { response in
guard let object = response.result.value else {
return
}
let json = JSON(object)
let accessToken = json["access_token"].stringValue
GitHubModel().setAccessToken(accessToken)
NSNotificationCenter.defaultCenter().postNotificationName(self.MyNotification, object: nil)
} catch {
}
})
}
}

func updatePopoverView(notification: NSNotification?) {
Expand Down

0 comments on commit 3225355

Please sign in to comment.