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.
|
|
|
|
//
|
|
|
|
|
2019-05-12 14:22:33 +02:00
|
|
|
// Feedbin currently has a maximum of 250 requests per second. If you begin to receive
|
|
|
|
// HTTP Response Codes of 403, you have exceeded this limit. Wait 5 minutes and your
|
|
|
|
// IP address will become unblocked and you can use the service again.
|
|
|
|
|
2019-05-03 01:17:52 +02:00
|
|
|
import Foundation
|
|
|
|
import RSWeb
|
|
|
|
|
2019-05-08 13:56:15 +02:00
|
|
|
enum CreateSubscriptionResult {
|
2019-05-09 00:41:19 +02:00
|
|
|
case created(FeedbinSubscription)
|
2019-05-10 17:14:24 +02:00
|
|
|
case multipleChoice([FeedbinSubscriptionChoice])
|
2019-05-08 13:56:15 +02:00
|
|
|
case alreadySubscribed
|
|
|
|
case notFound
|
|
|
|
}
|
|
|
|
|
2019-05-03 01:17:52 +02:00
|
|
|
final class FeedbinAPICaller: NSObject {
|
|
|
|
|
2019-05-12 15:47:27 +02:00
|
|
|
struct ConditionalGetKeys {
|
|
|
|
static let subscriptions = "subscriptions"
|
|
|
|
static let tags = "tags"
|
|
|
|
static let taggings = "taggings"
|
|
|
|
static let icons = "icons"
|
2019-05-13 10:13:06 +02:00
|
|
|
static let unreadEntries = "unreadEntries"
|
|
|
|
static let starredEntries = "starredEntries"
|
2019-05-12 15:47:27 +02:00
|
|
|
}
|
|
|
|
|
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-21 17:26:11 +02:00
|
|
|
weak 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-15 18:52:56 +02:00
|
|
|
func validateCredentials(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-17 17:04:13 +02:00
|
|
|
func importOPML(opmlData: Data, completion: @escaping (Result<FeedbinImportResult, Error>) -> Void) {
|
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("imports.json")
|
2019-05-20 01:19:08 +02:00
|
|
|
var request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
|
2019-05-17 17:04:13 +02:00
|
|
|
transport.send(request: request, method: HTTPMethod.post, payload: opmlData) { result in
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, data)):
|
|
|
|
|
|
|
|
guard let resultData = data else {
|
|
|
|
completion(.failure(TransportError.noData))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
let result = try JSONDecoder().decode(FeedbinImportResult.self, from: resultData)
|
|
|
|
completion(.success(result))
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func retrieveOPMLImportResult(importID: Int, completion: @escaping (Result<FeedbinImportResult?, Error>) -> Void) {
|
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("imports/\(importID).json")
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: FeedbinImportResult.self) { result in
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, importResult)):
|
|
|
|
completion(.success(importResult))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func retrieveTags(completion: @escaping (Result<[FeedbinTag]?, Error>) -> Void) {
|
2019-05-05 22:41:20 +02:00
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("tags.json")
|
2019-05-12 15:47:27 +02:00
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.tags]
|
2019-05-05 22:41:20 +02:00
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
2019-05-07 00:34:41 +02:00
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
transport.send(request: request, resultType: [FeedbinTag].self) { result in
|
2019-05-07 00:34:41 +02:00
|
|
|
|
2019-05-05 22:41:20 +02:00
|
|
|
switch result {
|
2019-05-08 13:56:15 +02:00
|
|
|
case .success(let (response, tags)):
|
2019-05-22 22:40:34 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.tags, headers: response.allHeaderFields)
|
2019-05-05 22:41:20 +02:00
|
|
|
completion(.success(tags))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-05 15:44:57 +02:00
|
|
|
|
2019-05-06 17:53:20 +02:00
|
|
|
func renameTag(oldName: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("tags.json")
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
let payload = FeedbinRenameTag(oldName: oldName, newName: newName)
|
2019-05-07 00:34:41 +02:00
|
|
|
transport.send(request: request, method: HTTPMethod.post, payload: payload, completion: completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteTag(name: String, completion: @escaping (Result<[FeedbinTagging]?, Error>) -> Void) {
|
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("tags.json")
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
let payload = FeedbinDeleteTag(name: name)
|
|
|
|
|
|
|
|
transport.send(request: request, method: HTTPMethod.delete, payload: payload, resultType: [FeedbinTagging].self) { result in
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, taggings)):
|
|
|
|
completion(.success(taggings))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-05 10:25:21 +02:00
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func retrieveSubscriptions(completion: @escaping (Result<[FeedbinSubscription]?, Error>) -> Void) {
|
2019-05-07 17:51:41 +02:00
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("subscriptions.json")
|
2019-05-12 15:47:27 +02:00
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.subscriptions]
|
2019-05-07 17:51:41 +02:00
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
transport.send(request: request, resultType: [FeedbinSubscription].self) { result in
|
2019-05-07 17:51:41 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-05-08 13:56:15 +02:00
|
|
|
case .success(let (response, subscriptions)):
|
2019-05-22 22:40:34 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.subscriptions, headers: response.allHeaderFields)
|
2019-05-07 17:51:41 +02:00
|
|
|
completion(.success(subscriptions))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func createSubscription(url: String, completion: @escaping (Result<CreateSubscriptionResult, Error>) -> Void) {
|
2019-05-08 13:56:15 +02:00
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("subscriptions.json")
|
2019-05-09 00:41:19 +02:00
|
|
|
var request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
|
|
|
|
let payload: Data
|
|
|
|
do {
|
|
|
|
payload = try JSONEncoder().encode(FeedbinCreateSubscription(feedURL: url))
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
return
|
|
|
|
}
|
2019-05-08 13:56:15 +02:00
|
|
|
|
2019-05-09 14:25:45 +02:00
|
|
|
transport.send(request: request, method: HTTPMethod.post, payload: payload) { result in
|
2019-05-08 13:56:15 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-05-09 00:41:19 +02:00
|
|
|
case .success(let (response, data)):
|
2019-05-08 13:56:15 +02:00
|
|
|
|
|
|
|
switch response.forcedStatusCode {
|
|
|
|
case 201:
|
2019-05-09 00:41:19 +02:00
|
|
|
guard let subData = data else {
|
|
|
|
completion(.failure(TransportError.noData))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
let subscription = try JSONDecoder().decode(FeedbinSubscription.self, from: subData)
|
|
|
|
completion(.success(.created(subscription)))
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
2019-05-08 13:56:15 +02:00
|
|
|
case 300:
|
2019-05-09 00:41:19 +02:00
|
|
|
guard let subData = data else {
|
|
|
|
completion(.failure(TransportError.noData))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
do {
|
2019-05-10 17:14:24 +02:00
|
|
|
let subscriptions = try JSONDecoder().decode([FeedbinSubscriptionChoice].self, from: subData)
|
2019-05-09 00:41:19 +02:00
|
|
|
completion(.success(.multipleChoice(subscriptions)))
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
2019-05-08 13:56:15 +02:00
|
|
|
case 302:
|
|
|
|
completion(.success(.alreadySubscribed))
|
|
|
|
default:
|
|
|
|
completion(.failure(TransportError.httpError(status: response.forcedStatusCode)))
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
|
|
|
switch error {
|
|
|
|
case TransportError.httpError(let status):
|
2019-05-10 17:40:02 +02:00
|
|
|
switch status {
|
|
|
|
case 401:
|
|
|
|
// I don't know why we get 401's here. This looks like a Feedbin bug, but it only happens
|
|
|
|
// when you are already subscribed to the feed.
|
|
|
|
completion(.success(.alreadySubscribed))
|
|
|
|
case 404:
|
2019-05-08 13:56:15 +02:00
|
|
|
completion(.success(.notFound))
|
2019-05-10 17:40:02 +02:00
|
|
|
default:
|
2019-05-08 13:56:15 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:25:45 +02:00
|
|
|
func renameSubscription(subscriptionID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-09 01:13:54 +02:00
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("subscriptions/\(subscriptionID)/update.json")
|
2019-05-09 00:41:19 +02:00
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
let payload = FeedbinUpdateSubscription(title: newName)
|
2019-05-09 01:13:54 +02:00
|
|
|
transport.send(request: request, method: HTTPMethod.post, payload: payload, completion: completion)
|
2019-05-09 00:41:19 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:25:45 +02:00
|
|
|
func deleteSubscription(subscriptionID: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("subscriptions/\(subscriptionID).json")
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
transport.send(request: request, method: HTTPMethod.delete, completion: completion)
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func retrieveTaggings(completion: @escaping (Result<[FeedbinTagging]?, Error>) -> Void) {
|
2019-05-08 00:41:32 +02:00
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("taggings.json")
|
2019-05-12 15:47:27 +02:00
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.taggings]
|
2019-05-08 00:41:32 +02:00
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
transport.send(request: request, resultType: [FeedbinTagging].self) { result in
|
2019-05-08 00:41:32 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-05-08 13:56:15 +02:00
|
|
|
case .success(let (response, taggings)):
|
2019-05-22 22:40:34 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.taggings, headers: response.allHeaderFields)
|
2019-05-08 00:41:32 +02:00
|
|
|
completion(.success(taggings))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-09 20:31:18 +02:00
|
|
|
func createTagging(feedID: Int, name: String, completion: @escaping (Result<Int, Error>) -> Void) {
|
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("taggings.json")
|
|
|
|
var request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
|
|
|
|
let payload: Data
|
|
|
|
do {
|
|
|
|
payload = try JSONEncoder().encode(FeedbinCreateTagging(feedID: feedID, name: name))
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
transport.send(request: request, method: HTTPMethod.post, payload:payload) { result in
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (response, _)):
|
|
|
|
if let taggingLocation = response.valueForHTTPHeaderField(HTTPResponseHeader.location),
|
|
|
|
let lowerBound = taggingLocation.range(of: "v2/taggings/")?.upperBound,
|
|
|
|
let upperBound = taggingLocation.range(of: ".json")?.lowerBound,
|
|
|
|
let taggingID = Int(taggingLocation[lowerBound..<upperBound]) {
|
|
|
|
completion(.success(taggingID))
|
|
|
|
} else {
|
|
|
|
completion(.failure(TransportError.noData))
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteTagging(taggingID: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("taggings/\(taggingID).json")
|
|
|
|
var request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
transport.send(request: request, method: HTTPMethod.delete, completion: completion)
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func retrieveIcons(completion: @escaping (Result<[FeedbinIcon]?, Error>) -> Void) {
|
2019-05-07 17:51:41 +02:00
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("icons.json")
|
2019-05-12 15:47:27 +02:00
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.icons]
|
2019-05-07 17:51:41 +02:00
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
transport.send(request: request, resultType: [FeedbinIcon].self) { result in
|
2019-05-07 17:51:41 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-05-08 13:56:15 +02:00
|
|
|
case .success(let (response, icons)):
|
2019-05-22 22:40:34 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.icons, headers: response.allHeaderFields)
|
2019-05-07 17:51:41 +02:00
|
|
|
completion(.success(icons))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-17 21:56:27 +02:00
|
|
|
func retrieveEntries(articleIDs: [String], completion: @escaping (Result<([FeedbinEntry]?), Error>) -> Void) {
|
|
|
|
|
|
|
|
guard !articleIDs.isEmpty else {
|
|
|
|
completion(.success(([FeedbinEntry]())))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let concatIDs = articleIDs.reduce("") { param, articleID in return param + ",\(articleID)" }
|
|
|
|
let paramIDs = String(concatIDs.dropFirst())
|
|
|
|
|
2019-05-27 01:35:54 +02:00
|
|
|
var callComponents = URLComponents(url: feedbinBaseURL.appendingPathComponent("entries.json"), resolvingAgainstBaseURL: false)!
|
|
|
|
callComponents.queryItems = [URLQueryItem(name: "ids", value: paramIDs), URLQueryItem(name: "mode", value: "extended")]
|
|
|
|
let request = URLRequest(url: callComponents.url!, credentials: credentials)
|
2019-05-17 21:56:27 +02:00
|
|
|
|
2019-05-19 20:33:48 +02:00
|
|
|
transport.send(request: request, resultType: [FeedbinEntry].self) { result in
|
2019-05-17 21:56:27 +02:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, entries)):
|
|
|
|
completion(.success((entries)))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-13 18:18:54 +02:00
|
|
|
func retrieveEntries(feedID: String, completion: @escaping (Result<([FeedbinEntry]?, String?), Error>) -> Void) {
|
|
|
|
|
|
|
|
let since = Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()
|
|
|
|
let sinceString = FeedbinDate.formatter.string(from: since)
|
|
|
|
|
2019-05-27 01:35:54 +02:00
|
|
|
var callComponents = URLComponents(url: feedbinBaseURL.appendingPathComponent("feeds/\(feedID)/entries.json"), resolvingAgainstBaseURL: false)!
|
|
|
|
callComponents.queryItems = [URLQueryItem(name: "since", value: sinceString), URLQueryItem(name: "per_page", value: "100"), URLQueryItem(name: "mode", value: "extended")]
|
|
|
|
let request = URLRequest(url: callComponents.url!, credentials: credentials)
|
2019-05-13 18:18:54 +02:00
|
|
|
|
|
|
|
transport.send(request: request, resultType: [FeedbinEntry].self) { result in
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (response, entries)):
|
|
|
|
|
|
|
|
let pagingInfo = HTTPLinkPagingInfo(urlResponse: response)
|
|
|
|
completion(.success((entries, pagingInfo.nextPage)))
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:10:07 +02:00
|
|
|
func retrieveEntries(completion: @escaping (Result<([FeedbinEntry]?, String?, Int?), Error>) -> Void) {
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
|
|
let since: Date = {
|
|
|
|
if let lastArticleFetch = accountMetadata?.lastArticleFetch {
|
|
|
|
return lastArticleFetch
|
|
|
|
} else {
|
|
|
|
return Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
let sinceString = FeedbinDate.formatter.string(from: since)
|
2019-05-27 01:35:54 +02:00
|
|
|
var callComponents = URLComponents(url: feedbinBaseURL.appendingPathComponent("entries.json"), resolvingAgainstBaseURL: false)!
|
|
|
|
callComponents.queryItems = [URLQueryItem(name: "since", value: sinceString), URLQueryItem(name: "per_page", value: "100"), URLQueryItem(name: "mode", value: "extended")]
|
|
|
|
let request = URLRequest(url: callComponents.url!, credentials: credentials)
|
2019-05-13 01:32:32 +02:00
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
transport.send(request: request, resultType: [FeedbinEntry].self) { result in
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (response, entries)):
|
|
|
|
|
2019-05-13 02:17:16 +02:00
|
|
|
let dateInfo = HTTPDateInfo(urlResponse: response)
|
2019-05-22 22:40:34 +02:00
|
|
|
self.accountMetadata?.lastArticleFetch = dateInfo?.date
|
2019-05-13 02:17:16 +02:00
|
|
|
|
2019-05-13 01:32:32 +02:00
|
|
|
let pagingInfo = HTTPLinkPagingInfo(urlResponse: response)
|
2019-05-22 22:40:34 +02:00
|
|
|
let lastPageNumber = self.extractPageNumber(link: pagingInfo.lastPage)
|
2019-05-14 20:10:07 +02:00
|
|
|
completion(.success((entries, pagingInfo.nextPage, lastPageNumber)))
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
|
|
case .failure(let error):
|
2019-05-22 22:40:34 +02:00
|
|
|
self.accountMetadata?.lastArticleFetch = nil
|
2019-05-13 01:32:32 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func retrieveEntries(page: String, completion: @escaping (Result<([FeedbinEntry]?, String?), Error>) -> Void) {
|
|
|
|
|
2019-05-27 01:35:54 +02:00
|
|
|
guard let url = URL(string: page), var callComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
2019-05-13 01:32:32 +02:00
|
|
|
completion(.success((nil, nil)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-27 01:35:54 +02:00
|
|
|
callComponents.queryItems?.append(URLQueryItem(name: "mode", value: "extended"))
|
|
|
|
let request = URLRequest(url: callComponents.url!, credentials: credentials)
|
2019-05-13 01:32:32 +02:00
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
transport.send(request: request, resultType: [FeedbinEntry].self) { result in
|
2019-05-13 01:32:32 +02:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (response, entries)):
|
|
|
|
|
|
|
|
let pagingInfo = HTTPLinkPagingInfo(urlResponse: response)
|
|
|
|
completion(.success((entries, pagingInfo.nextPage)))
|
|
|
|
|
|
|
|
case .failure(let error):
|
2019-05-22 22:40:34 +02:00
|
|
|
self.accountMetadata?.lastArticleFetch = nil
|
2019-05-13 01:32:32 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func retrieveUnreadEntries(completion: @escaping (Result<[Int]?, Error>) -> Void) {
|
2019-05-13 10:13:06 +02:00
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("unread_entries.json")
|
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.unreadEntries]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
transport.send(request: request, resultType: [Int].self) { result in
|
2019-05-13 10:13:06 +02:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (response, unreadEntries)):
|
2019-05-22 22:40:34 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.unreadEntries, headers: response.allHeaderFields)
|
2019-05-13 10:13:06 +02:00
|
|
|
completion(.success(unreadEntries))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func createUnreadEntries(entries: [Int], completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("unread_entries.json")
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
let payload = FeedbinUnreadEntry(unreadEntries: entries)
|
|
|
|
transport.send(request: request, method: HTTPMethod.post, payload: payload, completion: completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteUnreadEntries(entries: [Int], completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("unread_entries.json")
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
let payload = FeedbinUnreadEntry(unreadEntries: entries)
|
|
|
|
transport.send(request: request, method: HTTPMethod.delete, payload: payload, completion: completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func retrieveStarredEntries(completion: @escaping (Result<[Int]?, Error>) -> Void) {
|
2019-05-13 10:13:06 +02:00
|
|
|
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("starred_entries.json")
|
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.starredEntries]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-05-22 22:40:34 +02:00
|
|
|
transport.send(request: request, resultType: [Int].self) { result in
|
2019-05-13 10:13:06 +02:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (response, starredEntries)):
|
2019-05-22 22:40:34 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.starredEntries, headers: response.allHeaderFields)
|
2019-05-13 10:13:06 +02:00
|
|
|
completion(.success(starredEntries))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:52:56 +02:00
|
|
|
func createStarredEntries(entries: [Int], completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("starred_entries.json")
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
let payload = FeedbinStarredEntry(starredEntries: entries)
|
|
|
|
transport.send(request: request, method: HTTPMethod.post, payload: payload, completion: completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteStarredEntries(entries: [Int], completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
let callURL = feedbinBaseURL.appendingPathComponent("starred_entries.json")
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials)
|
|
|
|
let payload = FeedbinStarredEntry(starredEntries: entries)
|
|
|
|
transport.send(request: request, method: HTTPMethod.delete, payload: payload, completion: completion)
|
|
|
|
}
|
|
|
|
|
2019-05-03 01:17:52 +02:00
|
|
|
}
|
2019-05-05 15:44:57 +02:00
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
extension FeedbinAPICaller {
|
|
|
|
|
2019-05-13 10:13:06 +02:00
|
|
|
func storeConditionalGet(key: String, headers: [AnyHashable : Any]) {
|
2019-05-05 15:44:57 +02:00
|
|
|
if var conditionalGet = accountMetadata?.conditionalGetInfo {
|
|
|
|
conditionalGet[key] = HTTPConditionalGetInfo(headers: headers)
|
|
|
|
accountMetadata?.conditionalGetInfo = conditionalGet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 21:56:27 +02:00
|
|
|
func extractPageNumber(link: String?) -> Int? {
|
|
|
|
|
|
|
|
guard let link = link else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if let lowerBound = link.range(of: "page=")?.upperBound {
|
|
|
|
if let upperBound = link.range(of: "&")?.lowerBound {
|
|
|
|
return Int(link[lowerBound..<upperBound])
|
|
|
|
}
|
|
|
|
if let upperBound = link.range(of: ">")?.lowerBound {
|
|
|
|
return Int(link[lowerBound..<upperBound])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-05 15:44:57 +02:00
|
|
|
}
|