-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApi.swift
108 lines (90 loc) · 3.78 KB
/
Api.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
//
// Api.swift
// ECS189E
//
// Created by Zhiyi Xu on 9/23/18.
// Copyright © 2018 Sam King. All rights reserved.
//
import Foundation
struct Api {
struct ApiError: Error {
var message: String
var code: String
init(response: [String: Any]) {
self.message = (response["error_message"] as? String) ?? "Network error"
self.code = (response["error_code"] as? String) ?? "network_error"
}
}
typealias ApiCompletion = ((_ response: [String: Any]?, _ error: ApiError?) -> Void)
static var baseUrl = "https://ecs189e-fall2018.appspot.com/api"
static let defaultError = ApiError(response: [:])
static func configuration() -> URLSessionConfiguration {
let config = URLSessionConfiguration.ephemeral
config.timeoutIntervalForRequest = 60
config.timeoutIntervalForResource = 60
if let authToken = Storage.authToken {
config.httpAdditionalHeaders = ["x-authtoken": authToken]
print("auth in config:\(authToken)")
} else {
config.httpAdditionalHeaders = [:]
}
return config
}
static func ApiCall(endpoint: String, parameters: [String: Any], completion: @escaping ApiCompletion) {
guard let url = URL(string: baseUrl + endpoint) else {
print("Wrong url")
return
}
let session = URLSession(configuration: configuration())
var request = URLRequest(url: url)
request.httpMethod = "POST"
guard let requestData = try? JSONSerialization.data(withJSONObject: parameters) else {
DispatchQueue.main.async { completion(nil, defaultError) }
return
}
session.uploadTask(with: request, from: requestData) { data, response, error in
guard let rawData = data else {
DispatchQueue.main.async { completion(nil, defaultError) }
return
}
let jsonData = try? JSONSerialization.jsonObject(with: rawData)
guard let responseData = jsonData as? [String: Any] else {
DispatchQueue.main.async { completion(nil, defaultError) }
return
}
DispatchQueue.main.async {
if "ok" == responseData["status"] as? String {
completion(responseData, nil)
} else {
completion(nil, ApiError(response: responseData))
}
}
}.resume()
}
static func sendVerificationCode(phoneNumber: String, completion: @escaping ApiCompletion) {
ApiCall(endpoint: "/send_verification_code",
parameters: ["phone_number": phoneNumber],
completion: completion)
}
static func verifyCode(phoneNumber: String, code: String, completion: @escaping ApiCompletion) {
ApiCall(endpoint: "/verify_code",
parameters: ["e164_phone_number": phoneNumber, "code": code],
completion: completion)
}
static func setName(name: String, completion: @escaping ApiCompletion) {
ApiCall(endpoint: "/user",
parameters: ["name": name],
completion: completion)
}
static func setAccounts(accounts: [Account], completion: @escaping ApiCompletion) {
let serverAccounts = accounts.map { ["name": $0.name, "ID": $0.ID, "amount": String($0.amount)] }
ApiCall(endpoint: "/user",
parameters: ["accounts": serverAccounts],
completion: completion)
}
static func user(completion: @escaping ApiCompletion) {
ApiCall(endpoint: "/user",
parameters: [:],
completion: completion)
}
}