-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JagCesar
committed
Jul 15, 2018
1 parent
c6b73ad
commit 5cee5f2
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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())) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import XCTest | ||
@testable import TeslaAPI | ||
|
||
extension TeslaAPITests { | ||
func testEnableHvac() { | ||
let waitExpectation = expectation(description: "Enable HVAC") | ||
|
||
HVACRequest( | ||
accessToken: accessToken(), | ||
vehicleIdentifier: vehicleIdentifier(), | ||
state: .on).execute { result in | ||
XCTAssert(Thread.isMainThread) | ||
switch result { | ||
case .success(let result): | ||
if result { | ||
waitExpectation.fulfill() | ||
} else { | ||
XCTFail() | ||
} | ||
case .failure(_): | ||
XCTFail() | ||
} | ||
} | ||
waitForExpectations(timeout: 30, handler: nil) | ||
} | ||
|
||
func testDisableHvac() { | ||
let waitExpectation = expectation(description: "Disable HVAC") | ||
|
||
HVACRequest( | ||
accessToken: accessToken(), | ||
vehicleIdentifier: vehicleIdentifier(), | ||
state: .off).execute { result in | ||
XCTAssert(Thread.isMainThread) | ||
switch result { | ||
case .success(let result): | ||
if result { | ||
waitExpectation.fulfill() | ||
} else { | ||
XCTFail() | ||
} | ||
case .failure(_): | ||
XCTFail() | ||
} | ||
} | ||
waitForExpectations(timeout: 30, handler: nil) | ||
} | ||
} |