mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-23 01:44:06 -06:00
Fixes #1300. ## Summary Updates Alamofire to 5.x and updates how we do authentication adapting and failure retrying. ## Screenshots n/a ## Link to pull request in Documentation repository n/a ## Any other notes - Switches to using `AuthenticationInterceptor` to handle authenticated requests. This has a lot of good things: it's heavily tested, it only executes the refresh one at a time, and it has explicit handling of "this request failed with an _old_ access token" which is the root cause of some logouts in #1300. - Switches from manually doing retries and backoffs to using `RetryPolicy` which also adds in retries for 5xx errors in addition to all of the URLError-specific logic that it has. - Updates the apparently-copied-from-`AlamofireObjectMapper` repository for 5.x and removes some old Sodium-based handling that is now part of the Webhook flow, which is separate from Alamofire.
124 lines
5.8 KiB
Swift
124 lines
5.8 KiB
Swift
//
|
|
// HAAPI+RequestHelpers.swift
|
|
// HomeAssistant
|
|
//
|
|
// Created by Stephan Vanterpool on 8/12/18.
|
|
// Copyright © 2018 Robbie Trencheny. All rights reserved.
|
|
//
|
|
|
|
import Alamofire
|
|
import Foundation
|
|
import PromiseKit
|
|
import ObjectMapper
|
|
|
|
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 .success(let value):
|
|
seal.fulfill(value)
|
|
case .failure(let 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> {
|
|
return 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> {
|
|
return 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]> {
|
|
return 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]> {
|
|
return 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> {
|
|
return 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> {
|
|
return 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)
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|