-
Notifications
You must be signed in to change notification settings - Fork 0
/
Request.swift
214 lines (159 loc) · 6.24 KB
/
Request.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// Request.swift
//
// Created by Aurélien Aubert on 26/06/16.
//
import Foundation
enum RequestMethod: String {
case GET = "GET"
case POST = "POST"
case PUT = "PUT"
case DELETE = "DELETE"
}
enum RequestAuth {
case anonymous, user, none
}
class Request {
let clientID: String = "CLIENT_ID"
let clientSecret: String = "CLIENT_SECRET"
var url: String = "https://helloworld.com"
var authExt: String = "oauth/auth"
var tokenExt: String = "oauth/token"
var token: RequestToken?
init() {
if let token = self.getToken() {
self.token = token
}
}
//METHODS
func GET(url: String, params: NSDictionary, callback: ((response: NSDictionary) -> Void)) {
self.NEW(url: self.url+url, method: .GET, params: params, callback: callback)
}
func POST(url: String, params: NSDictionary, callback: ((response: NSDictionary) -> Void)) {
self.NEW(url: self.url+url, method: .POST, params: params, callback: callback)
}
func PUT(url: String, params: NSDictionary, callback: ((response: NSDictionary) -> Void)) {
self.NEW(url: self.url+url, method: .PUT, params: params, callback: callback)
}
func DELETE(url: String, params: NSDictionary, callback: ((response: NSDictionary) -> Void)) {
self.NEW(url: self.url+url, method: .DELETE, params: params, callback: callback)
}
//NEW REQUEST
func NEW(url: String, method: RequestMethod, params: NSDictionary, callback: ((response: NSDictionary) -> Void)) {
if self.isValidToken() {
self.request(url: url, method: method, params: params, callback: callback)
}
else {
self.requestToken(callback: {
error in
if error == false {
self.NEW(url: url, method: method, params: params, callback: callback)
}
})
}
}
func request(url: String, method: RequestMethod, params: NSDictionary, callback: ((response: NSDictionary) -> Void)) {
let urlString = "\(url)?\(self.httpParams(params: params))"
print(urlString)
let urlRequest = NSMutableURLRequest(url: NSURL(string: urlString)! as URL)
urlRequest.httpMethod = method.rawValue
urlRequest.addValue("application/www-form-urlencoded", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared().dataTask(with: urlRequest as URLRequest) {
(data, response, error) in
if error == nil {
var jsonResult: NSDictionary?
do {
jsonResult = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
}
catch { return }
if jsonResult != nil {
callback(response: jsonResult!)
}
}
}
task.resume()
}
func httpParams(params: NSDictionary) -> String {
var httpParams = ""
for (key, value) in params {
httpParams += "\(key)=\(value)&"
}
if self.token != nil {
httpParams += "access_token=\(self.token!.accessToken!)"
}
return httpParams
}
//AUTH
func anonymousAuth(callback: ((error: Bool) -> Void)) {
let url = self.url + self.tokenExt
let params: NSDictionary = ["client_id": self.clientID, "client_secret": self.clientSecret, "grant_type": "client_credentials"]
self.request(url: url, method: .POST, params: params, callback: {
response in
callback(error: !(self.updateToken(response: response)))
})
}
func userAuth(username: String, password: String, callback: ((error: Bool) -> Void)) {
let url = self.url + self.tokenExt
let params: NSDictionary = ["client_id": self.clientID, "client_secret": self.clientSecret, "grant_type": "password", "username": username, "password": password]
self.request(
url: url, method: .POST, params: params, callback: {
response in
callback(error: !(self.updateToken(response: response)))
})
}
func userAuth(callback: ((error: Bool) -> Void)) {
if self.token?.isUserAuth == true {
let url = self.url + self.tokenExt
let params: NSDictionary = ["client_id": self.clientID, "client_secret": self.clientSecret, "grant_type": "refresh_token", "refresh_token": (self.token?.refreshToken)!]
self.request(url: url, method: .POST, params: params, callback: {
response in
callback(error: !(self.updateToken(response: response)))
})
}
}
func authType() -> RequestAuth {
if(self.token != nil) {
return (self.token?.isUserAuth == true) ? .user : .anonymous
}
return .none
}
//TOKEN
func isValidToken() -> Bool {
if self.token != nil && Date().timeIntervalSince1970 < (self.token!.dateToken.timeIntervalSince1970 + 3600) {
return true
}
else {
return false
}
}
func requestToken(callback: ((error: Bool) -> Void)) {
if self.token?.isUserAuth == true {
self.userAuth(callback: callback)
}
else {
self.anonymousAuth(callback: callback)
}
}
func updateToken(response: NSDictionary) -> Bool {
if response["access_token"] != nil {
self.token = RequestToken(data: response)
self.saveToken(token: self.token!)
return true
}
else {
return false
}
}
func saveToken(token: RequestToken) {
KeychainWrapper.setObject(value: token, forKey: "token")
}
func getToken() -> RequestToken? {
if let token = KeychainWrapper.objectForKey(keyName: "token") as? RequestToken {
return token
}
else {
return nil
}
}
}