-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHVACRequests.swift
More file actions
49 lines (46 loc) · 1.61 KB
/
HVACRequests.swift
File metadata and controls
49 lines (46 loc) · 1.61 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
46
47
48
49
import Foundation
public struct HVACRequest: RequestProtocol {
public enum HVACState {
case on
case off
}
typealias CompletionType = Bool
var path: String {
switch state {
case .on:
return "/api/1/vehicles/\(vehicleIdentifier)/command/auto_conditioning_start"
case .off:
return "/api/1/vehicles/\(vehicleIdentifier)/command/auto_conditioning_stop"
}
}
let method = WebRequest.RequestMethod.post
let accessToken: String
let vehicleIdentifier: String
let state: HVACState
public init(accessToken: String, vehicleIdentifier: String, state: HVACState) {
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
if let error = error {
DispatchQueue.main.async {
completion(Result.failure(error))
}
} else if let response = response as? [String: [String: Any]],
let resultBool = response["response"]?["result"] as? Bool {
DispatchQueue.main.async {
completion(Result.success(resultBool))
}
} else {
DispatchQueue.main.async {
completion(Result.failure(APIError()))
}
}
}
}
}