This repository was archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathTokenRequestHandler.swift
58 lines (49 loc) · 2.24 KB
/
TokenRequestHandler.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
import UIKit
class TokenRequestHandler {
class func postDataFrom(params:[String:String]) -> String {
var data = ""
for (key, value) in params {
if let encodedKey = key.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
let encodedValue = value.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) {
if !data.isEmpty {
data = data + "&"
}
data = data + encodedKey + "=" + encodedValue;
}
}
return data
}
class func fetchToken(params:[String:String], completion:@escaping (NSDictionary, NSError?) -> Void) {
if let filePath = Bundle.main.path(forResource: "Keys", ofType:"plist"),
let dictionary = NSDictionary(contentsOfFile:filePath) as? [String: AnyObject],
let tokenRequestUrl = dictionary["TokenRequestUrl"] as? String {
var request = URLRequest(url: URL(string: tokenRequestUrl)!)
request.httpMethod = "POST"
let postString = self.postDataFrom(params: params)
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
completion(NSDictionary(), NSError(domain: "TWILIO", code: 1000, userInfo: [NSLocalizedDescriptionKey: "Incorrect return code for token request."]))
return
}
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
print("json = \(json)")
completion(json as NSDictionary, error as NSError?)
} catch let error as NSError {
completion(NSDictionary(), error)
}
}
task.resume()
}
else {
let userInfo = [NSLocalizedDescriptionKey : "TokenRequestUrl Key is missing"]
let error = NSError(domain: "app", code: 404, userInfo: userInfo)
completion(NSDictionary(), error)
}
}
}