-
Notifications
You must be signed in to change notification settings - Fork 0
/
UdacityConvenience.swift
139 lines (113 loc) · 6.78 KB
/
UdacityConvenience.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// UdacityConvenience.swift
// On the Map
//
// Created by Mario Preishuber on 29/03/16.
// Copyright © 2016 Preishuber. All rights reserved.
//
import UIKit
import Foundation
extension UdacityClient {
func authenticateWithViewController(username: String, password: String, completionHandlerForAuth: (success: Bool, errorString: String?) -> Void) {
getSessionID(username, password: password) { (success, sessionID, uniqueKey, errorString) in
if success {
// success! we have the sessionID!
print("SUCCESS: Login (Session-Id)")
AppVariables.sessionID = sessionID
ParseClient.sharedInstance().getUserData(uniqueKey!) { (success, userData, errorString) in
if(success) {
print("SUCCESS: Get User Data (via Parse)")
// user already created a pin
AppVariables.userData = userData
completionHandlerForAuth(success: true, errorString: nil)
} else {
// user did not create a pin yet, get main information via Udacity
self.getUserData(uniqueKey!) { (success, userData, errorString) in
if(success) {
print("SUCCESS: Get User Data (via Udacity)")
AppVariables.userData = userData
completionHandlerForAuth(success: true, errorString: nil)
} else {
print("FAILED: \(errorString)")
completionHandlerForAuth(success: false, errorString: errorString)
}
}
}
}
} else {
print("FAILED: Login")
completionHandlerForAuth(success: false, errorString: errorString)
}
}
}
func logout(completionHandlerForLogout: (success: Bool, errorString: String?) -> Void) {
taskForDELETEMethod(Methods.Session) { (results, error) in
if let error = error {
print("FAILED: Logout")
completionHandlerForLogout(success: false, errorString: error.description)
} else {
print("SUCCESS: Logout")
AppVariables.sessionID = nil
completionHandlerForLogout(success: true, errorString: nil)
}
}
}
private func getSessionID(username: String, password: String, completionHandlerForSession: (success: Bool, sessionID: String?, uniqueKey: String?, errorString: String?) -> Void) {
let jsonUsr = "\"\(UdacityClient.JSONBodyKeys.Username)\":\"\(username)\""
let jsonPwd = "\"\(UdacityClient.JSONBodyKeys.Password)\":\"\(password)\""
let jsonBody = "{\"\(UdacityClient.JSONBodyKeys.Udacity)\":{\(jsonUsr),\(jsonPwd)}}"
taskForPOSTMethod(Methods.Session, jsonBody: jsonBody) { (results, error) in
if let error = error {
print(error)
completionHandlerForSession(success: false, sessionID: nil, uniqueKey: nil, errorString: "Login Failed (SessionID).")
} else {
guard let session = results[UdacityClient.JSONResponseKeys.Session] as? [String:AnyObject] else {
print("Could not find \(UdacityClient.JSONResponseKeys.Session) in \(results)")
completionHandlerForSession(success: false, sessionID: nil, uniqueKey: nil, errorString: "Login Failed (SessionID).")
return
}
guard let sessionID = session[UdacityClient.JSONResponseKeys.Id] as? String else {
print("Could not find \(UdacityClient.JSONResponseKeys.Id) in \(session)")
completionHandlerForSession(success: false, sessionID: nil, uniqueKey: nil, errorString: "Login Failed (SessionID).")
return
}
guard let account = results[UdacityClient.JSONResponseKeys.Account] as? [String:AnyObject] else {
print("Could not find \(UdacityClient.JSONResponseKeys.Account) in \(session)")
completionHandlerForSession(success: false, sessionID: nil, uniqueKey: nil, errorString: "Login Failed (SessionID).")
return
}
guard let uniqueKey = account[UdacityClient.JSONResponseKeys.Key] as? String else {
print("Could not find \(UdacityClient.JSONResponseKeys.Key) in \(session)")
completionHandlerForSession(success: false, sessionID: nil, uniqueKey: nil, errorString: "Login Failed (SessionID).")
return
}
completionHandlerForSession(success: true, sessionID: sessionID, uniqueKey: uniqueKey, errorString: nil)
}
}
}
private func getUserData(uniqueKey: String, completionHandlerForUsrData: (success: Bool, userData: UserData?, errorString: String?) -> Void) {
taskForGETMethod(Methods.UserData, pathExtension: uniqueKey) { (results, error) in
if let error = error {
print(error)
completionHandlerForUsrData(success: false, userData: nil, errorString: "Get Public User Data Failed.")
} else {
guard let user = results[JSONResponseKeys.User] as? [String:AnyObject] else {
print("Could not find \(JSONResponseKeys.User) in \(results)")
completionHandlerForUsrData(success: false, userData: nil, errorString: "Get Public User Data Failed.")
return
}
guard let firstName = user[JSONResponseKeys.FirstName] as? String else {
print("Could not find \(JSONResponseKeys.FirstName) in \(results)")
completionHandlerForUsrData(success: false, userData: nil, errorString: "Get Public User Data Failed.")
return
}
guard let lastName = user[JSONResponseKeys.LastName] as? String else {
print("Could not find \(JSONResponseKeys.FirstName) in \(results)")
completionHandlerForUsrData(success: false, userData: nil, errorString: "Get Public User Data Failed.")
return
}
completionHandlerForUsrData(success: true, userData: UserData(uniqueKey: uniqueKey, firstName: firstName, lastName: lastName), errorString: nil)
}
}
}
}