From dfa4768917facf4b80da35efa0d808bbc067e24b Mon Sep 17 00:00:00 2001 From: Stephan Vanterpool Date: Wed, 29 Aug 2018 23:53:56 -0700 Subject: [PATCH] Add network logging and some optimizations --- .../ClientEventTableViewController.swift | 2 ++ HomeAssistant/Resources/SwiftGen/Strings.swift | 2 ++ HomeAssistant/Resources/en.lproj/Localizable.strings | 1 + HomeAssistant/Views/SettingsViewController.swift | 2 +- Shared/API/HAAPI.swift | 2 +- Shared/Authentication/AuthenticationAPI.swift | 10 +++++----- Shared/Authentication/TokenManager.swift | 12 +++++++----- Shared/ClientEvents/Model/ClientEvent.swift | 1 + 8 files changed, 20 insertions(+), 12 deletions(-) diff --git a/HomeAssistant/ClientEvents/ViewControllers/ClientEventTableViewController.swift b/HomeAssistant/ClientEvents/ViewControllers/ClientEventTableViewController.swift index 1cea9cc34..e6397d9c5 100644 --- a/HomeAssistant/ClientEvents/ViewControllers/ClientEventTableViewController.swift +++ b/HomeAssistant/ClientEvents/ViewControllers/ClientEventTableViewController.swift @@ -92,6 +92,8 @@ extension ClientEvent.EventType { return L10n.ClientEvents.EventType.locationUpdate case .serviceCall: return L10n.ClientEvents.EventType.serviceCall + case .networkRequest: + return L10n.ClientEvents.EventType.networkRequest case .unknown: return L10n.ClientEvents.EventType.unknown } diff --git a/HomeAssistant/Resources/SwiftGen/Strings.swift b/HomeAssistant/Resources/SwiftGen/Strings.swift index a68221f0a..5257499a7 100644 --- a/HomeAssistant/Resources/SwiftGen/Strings.swift +++ b/HomeAssistant/Resources/SwiftGen/Strings.swift @@ -97,6 +97,8 @@ internal enum L10n { internal enum EventType { /// Location Update internal static let locationUpdate = L10n.tr("Localizable", "client_events.event_type.location_update") + /// Network Request + internal static let networkRequest = L10n.tr("Localizable", "client_events.event_type.networkRequest") /// Notification internal static let notification = L10n.tr("Localizable", "client_events.event_type.notification") /// Service Call diff --git a/HomeAssistant/Resources/en.lproj/Localizable.strings b/HomeAssistant/Resources/en.lproj/Localizable.strings index aa8ec9016..7649bf428 100644 --- a/HomeAssistant/Resources/en.lproj/Localizable.strings +++ b/HomeAssistant/Resources/en.lproj/Localizable.strings @@ -149,6 +149,7 @@ send notifications to your device."; "client_events.event_type.service_call" = "Service Call"; "client_events.event_type.notification" = "Notification"; "client_events.event_type.location_update" = "Location Update"; +"client_events.event_type.networkRequest" = "Network Request"; "client_events.event_type.unknown" = "Unknown"; "client_events.view.clear" = "Clear"; "settings.connection_section.network_name.title" = "Current Network Name"; diff --git a/HomeAssistant/Views/SettingsViewController.swift b/HomeAssistant/Views/SettingsViewController.swift index 229141ec2..46f5fa5c5 100644 --- a/HomeAssistant/Views/SettingsViewController.swift +++ b/HomeAssistant/Views/SettingsViewController.swift @@ -673,7 +673,7 @@ class SettingsViewController: FormViewController, CLLocationManagerDelegate, SFS self.authenticationController.authenticateWithBrowser(at: connectionInfo.activeURL) }.then { (code: String) -> Promise in print("Browser auth succeeded, getting token") - let tokenManager = TokenManager(baseURL: connectionInfo.activeURL, tokenInfo: nil) + let tokenManager = TokenManager(connectionInfo: connectionInfo, tokenInfo: nil) return tokenManager.initialTokenWithCode(code) }.then { _ -> Promise in print("Token acquired") diff --git a/Shared/API/HAAPI.swift b/Shared/API/HAAPI.swift index dad81d752..e59f4f16e 100644 --- a/Shared/API/HAAPI.swift +++ b/Shared/API/HAAPI.swift @@ -96,7 +96,7 @@ public class HomeAssistantAPI { case .legacy(let apiPassword): self.manager = self.configureSessionManager(withPassword: apiPassword) case .modern(let tokenInfo): - self.tokenManager = TokenManager(baseURL: connectionInfo.baseURL, tokenInfo: tokenInfo) + self.tokenManager = TokenManager(connectionInfo: connectionInfo, tokenInfo: tokenInfo) tokenManager?.authenticationRequiredCallback = { return self.authenticationController.authenticateWithBrowser(at: connectionInfo.baseURL) } diff --git a/Shared/Authentication/AuthenticationAPI.swift b/Shared/Authentication/AuthenticationAPI.swift index f50d4723c..a3f105b7f 100644 --- a/Shared/Authentication/AuthenticationAPI.swift +++ b/Shared/Authentication/AuthenticationAPI.swift @@ -15,22 +15,22 @@ import ObjectMapper typealias URLRequestConvertible = Alamofire.URLRequestConvertible public class AuthenticationAPI { - let baseURL: URL + let connectionInfo: ConnectionInfo public enum AuthenticationError: Error { case unexepectedType case unexpectedResponse case invalidCode } - init(baseURL: URL) { - self.baseURL = baseURL + init(connectionInfo: ConnectionInfo) { + self.connectionInfo = connectionInfo } public func refreshTokenWith(tokenInfo: TokenInfo) -> Promise { return Promise { seal in let token = tokenInfo.refreshToken let routeInfo = RouteInfo(route: AuthenticationRoute.refreshToken(token: token), - baseURL: self.baseURL) + baseURL: self.connectionInfo.activeURL) let request = Alamofire.request(routeInfo) let context = TokenInfo.TokenInfoContext(oldTokenInfo: tokenInfo) request.validate().responseObject(context: context) { (dataresponse: DataResponse) in @@ -48,7 +48,7 @@ public class AuthenticationAPI { public func fetchTokenWithCode(_ authorizationCode: String) -> Promise { return Promise { seal in let routeInfo = RouteInfo(route: AuthenticationRoute.token(authorizationCode: authorizationCode), - baseURL: self.baseURL) + baseURL: self.connectionInfo.activeURL) let request = Alamofire.request(routeInfo) request.validate().responseObject { (dataresponse: DataResponse) in switch dataresponse.result { diff --git a/Shared/Authentication/TokenManager.swift b/Shared/Authentication/TokenManager.swift index adc0ccd37..b401773a2 100644 --- a/Shared/Authentication/TokenManager.swift +++ b/Shared/Authentication/TokenManager.swift @@ -20,7 +20,7 @@ public class TokenManager: RequestAdapter, RequestRetrier { private var tokenInfo: TokenInfo? private var authenticationAPI: AuthenticationAPI private var refreshPromiseCache: Promise? - private let baseURL: URL + private let connectionInfo: ConnectionInfo /// This should be set to enable the token manager to trigger re-authentication when needed. public var authenticationRequiredCallback: (() -> Promise)? @@ -29,9 +29,9 @@ public class TokenManager: RequestAdapter, RequestRetrier { return self.tokenInfo != nil } - public init(baseURL: URL, tokenInfo: TokenInfo? = nil) { - self.baseURL = baseURL - self.authenticationAPI = AuthenticationAPI(baseURL: baseURL) + public init(connectionInfo: ConnectionInfo, tokenInfo: TokenInfo? = nil) { + self.connectionInfo = connectionInfo + self.authenticationAPI = AuthenticationAPI(connectionInfo: self.connectionInfo) self.tokenInfo = tokenInfo } @@ -42,7 +42,6 @@ public class TokenManager: RequestAdapter, RequestRetrier { return self.authenticationAPI.fetchTokenWithCode(code).then { tokenInfo -> Promise in self.tokenInfo = tokenInfo Current.settingsStore.tokenInfo = tokenInfo - Current.settingsStore.baseURL = self.baseURL return self.bearerToken } } @@ -103,6 +102,9 @@ public class TokenManager: RequestAdapter, RequestRetrier { return urlRequest } + let text = "Request(SSID: \(ConnectionInfo.currentSSID() ?? "Unavailable") - \(urlRequest.url?.absoluteString ?? "URL Unavailable")" + let networkEvent = ClientEvent(text: text, type: .networkRequest) + Current.clientEventStore.addEvent(networkEvent) var newRequest = urlRequest newRequest.setValue("Bearer \(tokenInfo.accessToken)", forHTTPHeaderField: "Authorization") return newRequest diff --git a/Shared/ClientEvents/Model/ClientEvent.swift b/Shared/ClientEvents/Model/ClientEvent.swift index e05608ea5..c36e8ef1a 100644 --- a/Shared/ClientEvents/Model/ClientEvent.swift +++ b/Shared/ClientEvents/Model/ClientEvent.swift @@ -16,6 +16,7 @@ public class ClientEvent: Object { case notification case serviceCall case locationUpdate + case networkRequest case unknown }