-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChargeStateRequest.swift
More file actions
34 lines (31 loc) · 1.21 KB
/
ChargeStateRequest.swift
File metadata and controls
34 lines (31 loc) · 1.21 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
import Foundation
public struct ChargeStateRequest: RequestProtocol {
typealias CompletionType = ChargeState
var path: String {
return "/api/1/vehicles/\(vehicleIdentifier)/data_request/charge_state"
}
let method = WebRequest.RequestMethod.get
let accessToken: String
let vehicleIdentifier: String
public init(accessToken: String, vehicleIdentifier: String) {
self.accessToken = accessToken
self.vehicleIdentifier = vehicleIdentifier
}
public func execute(completion: @escaping (Result<ChargeState>) -> 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 dictionary = response["response"] {
completion(Result.success(ChargeState(dictionary: dictionary)))
} else {
completion(Result.failure(APIError()))
}
}
}
}
}