Add HTTP Caching for Feedbin (conditional get)

This commit is contained in:
Maurice Parker 2019-05-05 08:44:57 -05:00
parent 35160aaf75
commit 29f9cf83b1
3 changed files with 36 additions and 4 deletions

View File

@ -7,6 +7,7 @@
//
import Foundation
import RSWeb
protocol AccountMetadataDelegate: class {
func valueDidChange(_ accountMetadata: AccountMetadata, key: AccountMetadata.CodingKeys)
@ -14,10 +15,15 @@ protocol AccountMetadataDelegate: class {
final class AccountMetadata: Codable {
struct ConditionalGetKeys {
static let subscriptions = "subscriptions"
}
enum CodingKeys: String, CodingKey {
case name
case isActive
case username
case conditionalGetInfo
}
var name: String? {
@ -43,6 +49,14 @@ final class AccountMetadata: Codable {
}
}
}
var conditionalGetInfo = [String: HTTPConditionalGetInfo]() {
didSet {
if conditionalGetInfo != oldValue {
valueDidChange(.conditionalGetInfo)
}
}
}
weak var delegate: AccountMetadataDelegate?

View File

@ -50,12 +50,17 @@ final class FeedbinAPICaller: NSObject {
func retrieveSubscriptions(completionHandler completion: @escaping (Result<[FeedbinFeed], Error>) -> Void) {
let callURL = feedbinBaseURL.appendingPathComponent("subscriptions.json")
let request = URLRequest(url: callURL, credentials: credentials)
let conditionalGet = accountMetadata?.conditionalGetInfo[AccountMetadata.ConditionalGetKeys.subscriptions]
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
transport.send(request: request, resultType: [FeedbinFeed].self) { result in
transport.send(request: request, resultType: [FeedbinFeed].self) { [weak self] result in
switch result {
case .success(let (headers, feeds)):
break // TODO: put pageing implementation here
self?.storeConditionalGet(metadata: self?.accountMetadata, key: AccountMetadata.ConditionalGetKeys.subscriptions, headers: headers)
// TODO: Add paging code
case .failure(let error):
completion(.failure(error))
}
@ -65,3 +70,16 @@ final class FeedbinAPICaller: NSObject {
}
}
// MARK: Private
extension FeedbinAPICaller {
func storeConditionalGet(metadata: AccountMetadata?, key: String, headers: HTTPHeaders) {
if var conditionalGet = accountMetadata?.conditionalGetInfo {
conditionalGet[key] = HTTPConditionalGetInfo(headers: headers)
accountMetadata?.conditionalGetInfo = conditionalGet
}
}
}

@ -1 +1 @@
Subproject commit 6631ace43c69cdf77cb46414345923093d6467e8
Subproject commit b82bbf7731c93d7599e3806e142c5d0747b895c3