2019-05-03 01:17:52 +02:00
|
|
|
//
|
|
|
|
// FeedbinAPICaller.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 5/2/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSWeb
|
|
|
|
|
|
|
|
final class FeedbinAPICaller: NSObject {
|
|
|
|
|
2019-05-04 15:54:07 +02:00
|
|
|
private let feedbinBaseURL = URL(string: "https://api.feedbin.com/v2/")!
|
|
|
|
private var transport: Transport!
|
2019-05-05 14:49:59 +02:00
|
|
|
|
2019-05-05 10:25:21 +02:00
|
|
|
var credentials: Credentials?
|
2019-05-05 14:49:59 +02:00
|
|
|
var accountMetadata: AccountMetadata?
|
2019-05-03 01:17:52 +02:00
|
|
|
|
2019-05-04 15:54:07 +02:00
|
|
|
init(transport: Transport) {
|
2019-05-03 01:17:52 +02:00
|
|
|
super.init()
|
2019-05-04 15:54:07 +02:00
|
|
|
self.transport = transport
|
|
|
|
}
|
|
|
|
|
2019-05-05 10:25:21 +02:00
|
|
|
func validateCredentials(completionHandler completion: @escaping (Result<Bool, Error>) -> Void) {
|
2019-05-03 01:17:52 +02:00
|
|
|
|
2019-05-04 15:54:07 +02:00
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("authentication.json")
|
2019-05-04 18:48:48 +02:00
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
2019-05-03 01:17:52 +02:00
|
|
|
|
2019-05-04 15:54:07 +02:00
|
|
|
transport.send(request: request) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
2019-05-05 10:25:21 +02:00
|
|
|
completion(.success(true))
|
2019-05-04 16:18:14 +02:00
|
|
|
case .failure(let error):
|
|
|
|
switch error {
|
|
|
|
case TransportError.httpError(let status):
|
|
|
|
if status == 401 {
|
2019-05-05 10:25:21 +02:00
|
|
|
completion(.success(false))
|
2019-05-04 16:18:14 +02:00
|
|
|
} else {
|
2019-05-05 10:25:21 +02:00
|
|
|
completion(.failure(error))
|
2019-05-04 16:18:14 +02:00
|
|
|
}
|
|
|
|
default:
|
2019-05-05 10:25:21 +02:00
|
|
|
completion(.failure(error))
|
2019-05-04 16:18:14 +02:00
|
|
|
}
|
2019-05-04 15:54:07 +02:00
|
|
|
}
|
2019-05-03 01:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-05 10:25:21 +02:00
|
|
|
|
2019-05-06 12:28:02 +02:00
|
|
|
func retrieveTags(completionHandler completion: @escaping (Result<[FeedbinTag]?, Error>) -> Void) {
|
2019-05-05 22:41:20 +02:00
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("tags.json")
|
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[AccountMetadata.ConditionalGetKeys.tags]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: [FeedbinTag].self) { [weak self] result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (headers, tags)):
|
|
|
|
self?.storeConditionalGet(metadata: self?.accountMetadata, key: AccountMetadata.ConditionalGetKeys.tags, headers: headers)
|
|
|
|
completion(.success(tags))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func retrieveTaggings(completionHandler completion: @escaping (Result<[FeedbinTagging], Error>) -> Void) {
|
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("taggings.json")
|
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[AccountMetadata.ConditionalGetKeys.taggings]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: [FeedbinTagging].self) { [weak self] result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (headers, taggings)):
|
|
|
|
|
|
|
|
self?.storeConditionalGet(metadata: self?.accountMetadata, key: AccountMetadata.ConditionalGetKeys.taggings, headers: headers)
|
|
|
|
|
|
|
|
// TODO: Add paging code
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-05 10:25:21 +02:00
|
|
|
func retrieveSubscriptions(completionHandler completion: @escaping (Result<[FeedbinFeed], Error>) -> Void) {
|
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("subscriptions.json")
|
2019-05-05 15:44:57 +02:00
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[AccountMetadata.ConditionalGetKeys.subscriptions]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
2019-05-05 10:25:21 +02:00
|
|
|
|
2019-05-05 15:44:57 +02:00
|
|
|
transport.send(request: request, resultType: [FeedbinFeed].self) { [weak self] result in
|
2019-05-05 10:25:21 +02:00
|
|
|
switch result {
|
|
|
|
case .success(let (headers, feeds)):
|
2019-05-05 15:44:57 +02:00
|
|
|
|
|
|
|
self?.storeConditionalGet(metadata: self?.accountMetadata, key: AccountMetadata.ConditionalGetKeys.subscriptions, headers: headers)
|
|
|
|
|
|
|
|
// TODO: Add paging code
|
|
|
|
|
2019-05-05 10:25:21 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-03 01:17:52 +02:00
|
|
|
}
|
2019-05-05 15:44:57 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|