Update to use latest webservice network layer in RSWeb

This commit is contained in:
Maurice Parker 2019-05-04 08:54:07 -05:00
parent 1b49788a0a
commit bb42e46887
7 changed files with 46 additions and 61 deletions

View File

@ -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<Bool, Error>) -> 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
}

View File

@ -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<Bool, Error>) -> Void)
func refreshAll(for: Account)

View File

@ -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<Bool, Error>) -> 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)!
}
}

View File

@ -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<Bool, Error>) -> 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)
}
}

View File

@ -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<Bool, Error>) -> Void) {
return handler(.success(false))
}
func refreshAll(for account: Account) {

View File

@ -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")
}

@ -1 +1 @@
Subproject commit c7c235e45bc77930688af875f44ada769c89f1bf
Subproject commit 039427d62d8efdfc43d541518afbd46d0147967d