iOS/Sources/Shared/API/HAAPI+RequestHelpers.swift
Bruno Pantaleão Gonçalves 79d84045d6
Add more logging to connectivity errors (#3196)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2024-11-26 14:06:57 +01:00

151 lines
5.5 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
guard let url = server.info.connection.activeAPIURL()?.appendingPathComponent(path) else {
seal.reject(ServerConnectionError.noActiveURL(server.info.name))
return
}
_ = 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
guard let url = server.info.connection.activeAPIURL()?.appendingPathComponent(path) else {
seal.reject(ServerConnectionError.noActiveURL(server.info.name))
return
}
_ = 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
guard let url = server.info.connection.activeAPIURL()?.appendingPathComponent(path) else {
seal.reject(ServerConnectionError.noActiveURL(server.info.name))
return
}
_ = 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
guard let url = server.info.connection.activeAPIURL()?.appendingPathComponent(path) else {
seal.reject(ServerConnectionError.noActiveURL(server.info.name))
return
}
_ = 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
guard let url = server.info.connection.activeAPIURL()?.appendingPathComponent(path) else {
seal.reject(ServerConnectionError.noActiveURL(server.info.name))
return
}
_ = 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
)
}
}
}
}