iOS/Sources/Shared/API/HAAPI+RequestHelpers.swift
Bruno Pantaleão Gonçalves e823624661
Do not default to internal URL when external URL is not available (#2767)
<!-- 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 -->

To avoid using internal URL out of local network, this PR makes
"activeURL" optional, and it will not use internalURL when out of local
network.

Tested the internal/external logic on:
- iPhone
- iPad
- Mac
- Watch
- Shortcuts
- Notifications
- Widgets

For VPN users, they have to set external URL the same as internal URL to
have their setup working.

Pending:
- [x] Verify all edge cases where "guard let" were added
- [ ] Enforce SSID to use internal URL in the App

Next possible iteration:
- Add noise protocol between app and server to make http connections
secure

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->
<img width="1154" alt="Screenshot 2024-05-13 at 11 46 47"
src="https://github.com/home-assistant/iOS/assets/5808343/7ea634cc-382b-49c2-ab64-a218f996452e">

## 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-08 17:07:02 +01:00

151 lines
5.4 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)
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)
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)
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)
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)
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
)
}
}
}
}