-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLockRequest.swift
More file actions
45 lines (42 loc) · 1.47 KB
/
LockRequest.swift
File metadata and controls
45 lines (42 loc) · 1.47 KB
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
import Foundation
public struct LockRequest: RequestProtocol {
public enum LockState {
case lock
case unlock
}
typealias CompletionType = Bool
var path: String {
switch state {
case .lock:
return "/api/1/vehicles/\(vehicleIdentifier)/command/door_lock"
case .unlock:
return "/api/1/vehicles/\(vehicleIdentifier)/command/door_unlock"
}
}
let method = WebRequest.RequestMethod.post
let accessToken: String
let vehicleIdentifier: String
let state: LockState
public init(accessToken: String, vehicleIdentifier: String, state: LockState) {
self.accessToken = accessToken
self.vehicleIdentifier = vehicleIdentifier
self.state = state
}
public func execute(completion: @escaping (Result<Bool>) -> Void) {
WebRequest.request(
path: path,
method: method,
accessToken: accessToken) { response, error in
DispatchQueue.main.async {
if let error = error {
completion(Result.failure(error))
} else if let response = response as? [String: [String: Any]],
let resultBool = response["response"]?["result"] as? Bool {
completion(Result.success(resultBool))
} else {
completion(Result.failure(APIError()))
}
}
}
}
}