Files
iOS/Sources/Shared/API/HAAPI+RequestHelpers.swift
Zac West 640cca884b Add SwiftFormat to project (#1463)
Adds new fastlane lanes:
- `fastlane lint` which checks the linters
- `fastlane autocorrect` which applies the linters which can autocorrect (Rubocop, SwiftFormat)

Adds a build step to the Codegen abstract target which runs SwiftFormat in lint mode, pointing out what it's going to change when run.

Applies SwiftFormat to nearly all code -- exempts a few externally-sourced files and generated code.
2021-02-05 22:06:25 -08:00

158 lines
5.7 KiB
Swift

import Alamofire
import Foundation
import ObjectMapper
import PromiseKit
extension HomeAssistantAPI {
// MARK: - Helper methods for reducing boilerplate.
func handleResponse<T>(response: AFDataResponse<T>, seal: Resolver<T>, callingFunctionName: String) {
// Current.Log.verbose("\(callingFunctionName) response timeline: \(response.timeline)")
switch response.result {
case let .success(value):
seal.fulfill(value)
case let .failure(error):
Current.Log.error("Error on \(callingFunctionName) request: \(error)")
seal.reject(error)
}
}
func request(
path: String,
callingFunctionName: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil
) -> Promise<String> {
Promise { seal in
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
_ = manager.request(
url,
method: method,
parameters: parameters,
encoding: encoding,
headers: headers
)
.validate()
.responseString { (response: AFDataResponse<String>) in
self.handleResponse(
response: response,
seal: seal,
callingFunctionName: callingFunctionName
)
}
}
}
func request<T: BaseMappable>(
path: String,
callingFunctionName: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil
) -> Promise<T> {
Promise { seal in
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
.validate()
.responseObject { (response: AFDataResponse<T>) in
self.handleResponse(
response: response,
seal: seal,
callingFunctionName: callingFunctionName
)
}
}
}
func request<T: BaseMappable>(
path: String,
callingFunctionName: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil
) -> Promise<[T]> {
Promise { seal in
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
.validate()
.responseArray { (response: AFDataResponse<[T]>) in
self.handleResponse(
response: response,
seal: seal,
callingFunctionName: callingFunctionName
)
}
}
}
func request<T: ImmutableMappable>(
path: String,
callingFunctionName: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil
) -> Promise<[T]> {
Promise { seal in
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
.validate()
.responseArray { (response: AFDataResponse<[T]>) in
self.handleResponse(
response: response,
seal: seal,
callingFunctionName: callingFunctionName
)
}
}
}
func request<T: ImmutableMappable>(
path: String,
callingFunctionName: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil
) -> Promise<T> {
Promise { seal in
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
.validate()
.responseObject { (response: AFDataResponse<T>) in
self.handleResponse(
response: response,
seal: seal,
callingFunctionName: callingFunctionName
)
}
}
}
func requestImmutable<T: ImmutableMappable>(
path: String,
callingFunctionName: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil
) -> Promise<T> {
Promise { seal in
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
.validate()
.responseObject { (response: AFDataResponse<T>) in
self.handleResponse(
response: response,
seal: seal,
callingFunctionName: callingFunctionName
)
}
}
}
}