From bb42e468874db15f2b7a6bc68cffef9650a83a34 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 4 May 2019 08:54:07 -0500 Subject: [PATCH] Update to use latest webservice network layer in RSWeb --- Frameworks/Account/Account.swift | 10 ++-- Frameworks/Account/AccountDelegate.swift | 2 +- .../Account/Feedbin/FeedbinAPICaller.swift | 50 ++++++------------- .../Feedbin/FeedbinAccountDelegate.swift | 18 +++---- .../LocalAccount/LocalAccountDelegate.swift | 4 +- .../AccountsAddFeedbinWindowController.swift | 21 +++++--- submodules/RSWeb | 2 +- 7 files changed, 46 insertions(+), 61 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 03dca1d71..7d51321df 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -177,13 +177,13 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, return delegate.supportsSubFolders } - init?(dataFolder: String, type: AccountType, accountID: String) { + init?(dataFolder: String, type: AccountType, accountID: String, transport: Transport = URLSession.webserviceTransport()) { switch type { case .onMyMac: self.delegate = LocalAccountDelegate() case .feedbin: - self.delegate = FeedbinAccountDelegate() + self.delegate = FeedbinAccountDelegate(transport: transport) default: fatalError("Only Local and Feedbin accounts are supported") } @@ -246,12 +246,12 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, // self.password = password } - public static func validateCredentials(type: AccountType, username: String, password: String, completionHandler handler: @escaping ((Bool) -> ())) { + public static func validateCredentials(transport: Transport = URLSession.webserviceTransport(), type: AccountType, username: String, password: String, completionHandler handler: @escaping (Result) -> Void) { switch type { case .onMyMac: - LocalAccountDelegate.validateCredentials(username: username, password: password, completionHandler: handler) + LocalAccountDelegate.validateCredentials(transport: transport, username: username, password: password, completionHandler: handler) case .feedbin: - FeedbinAccountDelegate.validateCredentials(username: username, password: password, completionHandler: handler) + FeedbinAccountDelegate.validateCredentials(transport: transport, username: username, password: password, completionHandler: handler) default: break } diff --git a/Frameworks/Account/AccountDelegate.swift b/Frameworks/Account/AccountDelegate.swift index 2f03073ea..7f6ca2306 100644 --- a/Frameworks/Account/AccountDelegate.swift +++ b/Frameworks/Account/AccountDelegate.swift @@ -16,7 +16,7 @@ public protocol AccountDelegate { var refreshProgress: DownloadProgress { get } - static func validateCredentials(username: String, password: String, completionHandler handler: @escaping ((Bool) -> ())) + static func validateCredentials(transport: Transport, username: String, password: String, completionHandler handler: @escaping (Result) -> Void) func refreshAll(for: Account) diff --git a/Frameworks/Account/Feedbin/FeedbinAPICaller.swift b/Frameworks/Account/Feedbin/FeedbinAPICaller.swift index bd66d934f..7e3cc7eed 100644 --- a/Frameworks/Account/Feedbin/FeedbinAPICaller.swift +++ b/Frameworks/Account/Feedbin/FeedbinAPICaller.swift @@ -11,45 +11,27 @@ import RSWeb final class FeedbinAPICaller: NSObject { - private static let feedbinBaseURL = "https://api.feedbin.com/v2/" - private var session: URLSession! + private let feedbinBaseURL = URL(string: "https://api.feedbin.com/v2/")! + private var transport: Transport! - override init() { - + init(transport: Transport) { super.init() + self.transport = transport + } + + func validateCredentials(username: String, password: String, completionHandler handler: @escaping (Result) -> Void) { - let sessionConfiguration = URLSessionConfiguration.default - sessionConfiguration.requestCachePolicy = .reloadIgnoringLocalCacheData - sessionConfiguration.timeoutIntervalForRequest = 60.0 - sessionConfiguration.httpShouldSetCookies = false - sessionConfiguration.httpCookieAcceptPolicy = .never - sessionConfiguration.httpMaximumConnectionsPerHost = 2 - sessionConfiguration.httpCookieStorage = nil - sessionConfiguration.urlCache = nil + let callURL = feedbinBaseURL.appendingPathComponent("authentication.json") + let request = URLRequest(url: callURL, username: username, password: password) - if let userAgentHeaders = UserAgent.headers() { - sessionConfiguration.httpAdditionalHeaders = userAgentHeaders + transport.send(request: request) { result in + switch result { + case .success: + handler(.success(true)) + case .failure: + handler(.success(false)) + } } - session = URLSession(configuration: sessionConfiguration) - } - - func validateCredentials(username: String, password: String, completionHandler handler: @escaping APIResultBlock) { - let request = URLRequest(url: urlFromRelativePath("authentication.json"), username: username, password: password) - let call = APICall(session: session, request: request) - call.execute(completionHandler: handler) - } - -} - -// MARK: Private - -private extension FeedbinAPICaller { - - func urlFromRelativePath(_ path: String) -> URL { - let fullPath = "\(FeedbinAPICaller.feedbinBaseURL)\(path)" - return URL(string: fullPath)! - } - } diff --git a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift index ff1fcd0c7..f7d58030b 100644 --- a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift @@ -13,21 +13,19 @@ final class FeedbinAccountDelegate: AccountDelegate { let supportsSubFolders = false - private let caller = FeedbinAPICaller() + private let caller: FeedbinAPICaller - var refreshProgress: DownloadProgress { - return DownloadProgress(numberOfTasks: 0) + init(transport: Transport) { + caller = FeedbinAPICaller(transport: transport) } - static func validateCredentials(username: String, password: String, completionHandler handler: @escaping ((Bool) -> ())) { + var refreshProgress = DownloadProgress(numberOfTasks: 0) + + static func validateCredentials(transport: Transport, username: String, password: String, completionHandler handler: @escaping (Result) -> Void) { - let caller = FeedbinAPICaller() + let caller = FeedbinAPICaller(transport: transport) caller.validateCredentials(username: username, password: password) { result in - if result.statusCode == 200 { - handler(true) - } else { - handler(false) - } + handler(result) } } diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index 37f1c9c95..ffb783670 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -18,8 +18,8 @@ final class LocalAccountDelegate: AccountDelegate { return refresher.progress } - static func validateCredentials(username: String, password: String, completionHandler handler: ((Bool) -> ())) { - return handler(false) + static func validateCredentials(transport: Transport, username: String, password: String, completionHandler handler: (Result) -> Void) { + return handler(.success(false)) } func refreshAll(for account: Account) { diff --git a/Mac/Preferences/Accounts/AccountsAddFeedbinWindowController.swift b/Mac/Preferences/Accounts/AccountsAddFeedbinWindowController.swift index d6966075c..3de63f21d 100644 --- a/Mac/Preferences/Accounts/AccountsAddFeedbinWindowController.swift +++ b/Mac/Preferences/Accounts/AccountsAddFeedbinWindowController.swift @@ -60,16 +60,21 @@ class AccountsAddFeedbinWindowController: NSWindowController, NSTextFieldDelegat self.progressIndicator.isHidden = true self.progressIndicator.stopAnimation(self) - if result { + switch result { + case .success(let authenticated): - let account = AccountManager.shared.createAccount(type: .feedbin) - account.storeCredentials(username: self.usernameTextField.stringValue, password: self.passwordTextField.stringValue) + if authenticated { + let account = AccountManager.shared.createAccount(type: .feedbin) + account.storeCredentials(username: self.usernameTextField.stringValue, password: self.passwordTextField.stringValue) + + self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK) + } else { + self.errorMessageLabel.stringValue = NSLocalizedString("Unable to verify credentials.", comment: "Credentials Error") + } + + case .failure: - self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK) - - } else { - - self.errorMessageLabel.stringValue = NSLocalizedString("Unable to verify credentials", comment: "Credentials Error") + self.errorMessageLabel.stringValue = NSLocalizedString("Unable to verify credentials due to networking error.", comment: "Credentials Error") } diff --git a/submodules/RSWeb b/submodules/RSWeb index c7c235e45..039427d62 160000 --- a/submodules/RSWeb +++ b/submodules/RSWeb @@ -1 +1 @@ -Subproject commit c7c235e45bc77930688af875f44ada769c89f1bf +Subproject commit 039427d62d8efdfc43d541518afbd46d0147967d