2019-05-28 19:08:15 +02:00
|
|
|
//
|
2019-06-19 18:25:37 +02:00
|
|
|
// ReaderAPICaller.swift
|
2019-05-28 19:08:15 +02:00
|
|
|
// Account
|
|
|
|
//
|
2019-06-19 13:56:25 +02:00
|
|
|
// Created by Jeremy Beker on 5/28/19.
|
2019-05-28 19:08:15 +02:00
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSWeb
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
enum CreateReaderAPISubscriptionResult {
|
|
|
|
case created(ReaderAPISubscription)
|
2019-05-28 19:08:15 +02:00
|
|
|
case alreadySubscribed
|
|
|
|
case notFound
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
final class ReaderAPICaller: NSObject {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
struct ConditionalGetKeys {
|
|
|
|
static let subscriptions = "subscriptions"
|
|
|
|
static let tags = "tags"
|
|
|
|
static let taggings = "taggings"
|
|
|
|
static let icons = "icons"
|
|
|
|
static let unreadEntries = "unreadEntries"
|
|
|
|
static let starredEntries = "starredEntries"
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
enum ReaderState: String {
|
2019-06-13 15:35:01 +02:00
|
|
|
case read = "user/-/state/com.google/read"
|
|
|
|
case starred = "user/-/state/com.google/starred"
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
enum ReaderStreams: String {
|
2019-06-19 13:56:25 +02:00
|
|
|
case readingList = "user/-/state/com.google/reading-list"
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
enum ReaderAPIEndpoints: String {
|
2019-06-13 18:44:20 +02:00
|
|
|
case login = "/accounts/ClientLogin"
|
|
|
|
case token = "/reader/api/0/token"
|
2019-06-16 19:14:23 +02:00
|
|
|
case disableTag = "/reader/api/0/disable-tag"
|
|
|
|
case renameTag = "/reader/api/0/rename-tag"
|
2019-06-13 18:44:20 +02:00
|
|
|
case tagList = "/reader/api/0/tag/list"
|
|
|
|
case subscriptionList = "/reader/api/0/subscription/list"
|
2019-06-15 18:27:21 +02:00
|
|
|
case subscriptionEdit = "/reader/api/0/subscription/edit"
|
|
|
|
case subscriptionAdd = "/reader/api/0/subscription/quickadd"
|
2019-06-13 18:44:20 +02:00
|
|
|
case contents = "/reader/api/0/stream/items/contents"
|
|
|
|
case itemIds = "/reader/api/0/stream/items/ids"
|
|
|
|
case editTag = "/reader/api/0/edit-tag"
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
private var transport: Transport!
|
|
|
|
|
|
|
|
var credentials: Credentials?
|
2019-06-16 21:14:20 +02:00
|
|
|
private var accessToken: String?
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
weak var accountMetadata: AccountMetadata?
|
|
|
|
|
2019-05-29 16:54:52 +02:00
|
|
|
var server: String? {
|
|
|
|
get {
|
2019-05-29 21:16:09 +02:00
|
|
|
return APIBaseURL?.host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var APIBaseURL: URL? {
|
|
|
|
get {
|
|
|
|
guard let accountMetadata = accountMetadata else {
|
2019-05-29 16:54:52 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-29 21:16:09 +02:00
|
|
|
|
|
|
|
return accountMetadata.endpointURL
|
2019-05-29 16:54:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
init(transport: Transport) {
|
|
|
|
super.init()
|
|
|
|
self.transport = transport
|
|
|
|
}
|
|
|
|
|
2019-05-29 21:16:09 +02:00
|
|
|
func validateCredentials(endpoint: URL, completion: @escaping (Result<Credentials?, Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
guard let credentials = credentials else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
let request = URLRequest(url: endpoint.appendingPathComponent(ReaderAPIEndpoints.login.rawValue), credentials: credentials)
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
transport.send(request: request) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, data)):
|
|
|
|
guard let resultData = data else {
|
|
|
|
completion(.failure(TransportError.noData))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the return data to UTF8 and then parse out the Auth token
|
|
|
|
guard let rawData = String(data: resultData, encoding: .utf8) else {
|
|
|
|
completion(.failure(TransportError.noData))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var authData: [String: String] = [:]
|
|
|
|
rawData.split(separator: "\n").forEach({ (line: Substring) in
|
|
|
|
let items = line.split(separator: "=").map{String($0)}
|
|
|
|
authData[items[0]] = items[1]
|
|
|
|
})
|
|
|
|
|
|
|
|
guard let authString = authData["Auth"] else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save Auth Token for later use
|
2019-09-15 17:03:47 +02:00
|
|
|
self.credentials = Credentials(type: .readerAPIKey, username: credentials.username, secret: authString)
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-05-29 16:54:52 +02:00
|
|
|
completion(.success(self.credentials))
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
2019-05-29 16:54:52 +02:00
|
|
|
completion(.failure(error))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-10 22:53:35 +02:00
|
|
|
func requestAuthorizationToken(endpoint: URL, completion: @escaping (Result<String, Error>) -> Void) {
|
2019-06-16 21:14:20 +02:00
|
|
|
// If we have a token already, use it
|
|
|
|
if let accessToken = accessToken {
|
|
|
|
completion(.success(accessToken))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise request one.
|
2019-06-10 22:53:35 +02:00
|
|
|
guard let credentials = credentials else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
let request = URLRequest(url: endpoint.appendingPathComponent(ReaderAPIEndpoints.token.rawValue), credentials: credentials)
|
2019-06-10 22:53:35 +02:00
|
|
|
|
|
|
|
transport.send(request: request) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, data)):
|
|
|
|
guard let resultData = data else {
|
|
|
|
completion(.failure(TransportError.noData))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the return data to UTF8 and then parse out the Auth token
|
2019-06-16 21:14:20 +02:00
|
|
|
guard let accessToken = String(data: resultData, encoding: .utf8) else {
|
2019-06-10 22:53:35 +02:00
|
|
|
completion(.failure(TransportError.noData))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:14:20 +02:00
|
|
|
self.accessToken = accessToken
|
|
|
|
completion(.success(accessToken))
|
2019-06-10 22:53:35 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func retrieveTags(completion: @escaping (Result<[ReaderAPITag]?, Error>) -> Void) {
|
2019-05-29 21:16:09 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-05-30 13:48:34 +02:00
|
|
|
// Add query string for getting JSON (probably should break this out as I will be doing it a lot)
|
2019-06-19 18:25:37 +02:00
|
|
|
guard var components = URLComponents(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.tagList.rawValue), resolvingAgainstBaseURL: false) else {
|
2019-05-30 13:48:34 +02:00
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
components.queryItems = [
|
|
|
|
URLQueryItem(name: "output", value: "json")
|
|
|
|
]
|
|
|
|
|
|
|
|
guard let callURL = components.url else {
|
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-01 14:08:19 +02:00
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.tags]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
transport.send(request: request, resultType: ReaderAPITagContainer.self) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-05-30 22:01:56 +02:00
|
|
|
case .success(let (response, wrapper)):
|
2019-05-28 19:08:15 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.tags, headers: response.allHeaderFields)
|
2019-05-30 22:01:56 +02:00
|
|
|
completion(.success(wrapper?.tags))
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func renameTag(oldName: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-16 19:14:23 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success(let token):
|
2019-06-19 18:25:37 +02:00
|
|
|
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.renameTag.rawValue), credentials: self.credentials)
|
2019-06-16 19:14:23 +02:00
|
|
|
|
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
let oldTagName = "user/-/label/\(oldName)"
|
|
|
|
let newTagName = "user/-/label/\(newName)"
|
|
|
|
let postData = "T=\(token)&s=\(oldTagName)&dest=\(newTagName)".data(using: String.Encoding.utf8)
|
|
|
|
|
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 19:14:23 +02:00
|
|
|
func deleteTag(name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-16 19:14:23 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success(let token):
|
2019-06-19 18:25:37 +02:00
|
|
|
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.disableTag.rawValue), credentials: self.credentials)
|
2019-06-16 19:14:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
let tagName = "user/-/label/\(name)"
|
|
|
|
let postData = "T=\(token)&s=\(tagName)".data(using: String.Encoding.utf8)
|
|
|
|
|
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func retrieveSubscriptions(completion: @escaping (Result<[ReaderAPISubscription]?, Error>) -> Void) {
|
2019-06-01 14:08:19 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add query string for getting JSON (probably should break this out as I will be doing it a lot)
|
2019-06-19 18:25:37 +02:00
|
|
|
guard var components = URLComponents(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionList.rawValue), resolvingAgainstBaseURL: false) else {
|
2019-06-01 14:08:19 +02:00
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
components.queryItems = [
|
|
|
|
URLQueryItem(name: "output", value: "json")
|
|
|
|
]
|
|
|
|
|
|
|
|
guard let callURL = components.url else {
|
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.subscriptions]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
transport.send(request: request, resultType: ReaderAPISubscriptionContainer.self) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-06-01 14:08:19 +02:00
|
|
|
case .success(let (response, container)):
|
2019-05-28 19:08:15 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.subscriptions, headers: response.allHeaderFields)
|
2019-06-01 14:08:19 +02:00
|
|
|
completion(.success(container?.subscriptions))
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func createSubscription(url: String, completion: @escaping (Result<CreateReaderAPISubscriptionResult, Error>) -> Void) {
|
2019-06-15 18:27:21 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-05-28 19:08:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-13 14:37:08 +02:00
|
|
|
guard let url = URL(string: url) else {
|
|
|
|
completion(.failure(LocalAccountDelegateError.invalidParameter))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
FeedFinder.find(url: url) { result in
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
switch result {
|
2019-07-13 14:37:08 +02:00
|
|
|
case .success(let feedSpecifiers):
|
|
|
|
guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers),
|
|
|
|
let url = URL(string: bestFeedSpecifier.urlString) else {
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
return
|
2019-06-15 18:27:21 +02:00
|
|
|
}
|
2019-07-13 14:37:08 +02:00
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
2019-06-15 18:27:21 +02:00
|
|
|
switch result {
|
2019-07-13 14:37:08 +02:00
|
|
|
case .success(let token):
|
|
|
|
guard var components = URLComponents(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionAdd.rawValue), resolvingAgainstBaseURL: false) else {
|
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
components.queryItems = [
|
|
|
|
URLQueryItem(name: "quickadd", value: url.absoluteString)
|
|
|
|
]
|
|
|
|
|
|
|
|
guard let callURL = components.url else {
|
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: callURL, credentials: self.credentials)
|
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
let postData = "T=\(token)".data(using: String.Encoding.utf8)
|
|
|
|
|
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: ReaderAPIQuickAddResult.self, completion: { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, subResult)):
|
|
|
|
|
|
|
|
switch subResult?.numResults {
|
|
|
|
case 0:
|
|
|
|
completion(.success(.alreadySubscribed))
|
|
|
|
default:
|
|
|
|
// We have a feed ID but need to get feed information
|
|
|
|
guard let streamId = subResult?.streamId else {
|
2019-06-15 18:27:21 +02:00
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
return
|
|
|
|
}
|
2019-07-13 14:37:08 +02:00
|
|
|
|
|
|
|
// There is no call to get a single subscription entry, so we get them all,
|
|
|
|
// look up the one we just subscribed to and return that
|
|
|
|
self.retrieveSubscriptions(completion: { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success(let subscriptions):
|
|
|
|
guard let subscriptions = subscriptions else {
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let newStreamId = "feed/\(streamId)"
|
|
|
|
|
|
|
|
guard let subscription = subscriptions.first(where: { (sub) -> Bool in
|
|
|
|
sub.feedID == newStreamId
|
|
|
|
}) else {
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
completion(.success(.created(subscription)))
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
})
|
2019-06-15 18:27:21 +02:00
|
|
|
}
|
2019-07-13 14:37:08 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
2019-06-15 18:27:21 +02:00
|
|
|
}
|
2019-07-13 14:37:08 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2019-06-15 18:27:21 +02:00
|
|
|
case .failure(let error):
|
2019-05-28 19:08:15 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
2019-07-13 14:37:08 +02:00
|
|
|
}
|
2019-06-15 18:27:21 +02:00
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-07-13 14:37:08 +02:00
|
|
|
|
|
|
|
case .failure:
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
2019-07-13 14:37:08 +02:00
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
2019-07-13 14:37:08 +02:00
|
|
|
//
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func renameSubscription(subscriptionID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-15 22:13:08 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success(let token):
|
2019-06-19 18:25:37 +02:00
|
|
|
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
|
2019-06-15 22:13:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
let postData = "T=\(token)&s=\(subscriptionID)&ac=edit&t=\(newName)".data(using: String.Encoding.utf8)
|
|
|
|
|
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSubscription(subscriptionID: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-15 22:13:08 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-05-28 19:08:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 22:13:08 +02:00
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
2019-05-28 19:08:15 +02:00
|
|
|
switch result {
|
2019-06-15 22:13:08 +02:00
|
|
|
case .success(let token):
|
2019-06-19 18:25:37 +02:00
|
|
|
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
|
2019-06-15 22:13:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
let postData = "T=\(token)&s=\(subscriptionID)&ac=unsubscribe".data(using: String.Encoding.utf8)
|
|
|
|
|
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 22:13:08 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 19:14:23 +02:00
|
|
|
func createTagging(subscriptionID: String, tagName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-15 22:13:08 +02:00
|
|
|
|
2019-06-16 19:14:23 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success(let token):
|
2019-06-19 18:25:37 +02:00
|
|
|
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
|
2019-06-16 19:14:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
let tagName = "user/-/label/\(tagName)"
|
|
|
|
let postData = "T=\(token)&s=\(subscriptionID)&ac=edit&a=\(tagName)".data(using: String.Encoding.utf8)
|
|
|
|
|
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-16 19:14:23 +02:00
|
|
|
func deleteTagging(subscriptionID: String, tagName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-16 19:14:23 +02:00
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success(let token):
|
2019-06-19 18:25:37 +02:00
|
|
|
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
|
2019-06-16 19:14:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
let tagName = "user/-/label/\(tagName)"
|
|
|
|
let postData = "T=\(token)&s=\(subscriptionID)&ac=edit&r=\(tagName)".data(using: String.Encoding.utf8)
|
|
|
|
|
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func retrieveEntries(articleIDs: [String], completion: @escaping (Result<([ReaderAPIEntry]?), Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
guard !articleIDs.isEmpty else {
|
2019-06-19 18:25:37 +02:00
|
|
|
completion(.success(([ReaderAPIEntry]())))
|
2019-05-28 19:08:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-10 22:53:35 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-10 22:53:35 +02:00
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
2019-05-28 19:08:15 +02:00
|
|
|
switch result {
|
2019-06-10 22:53:35 +02:00
|
|
|
case .success(let token):
|
|
|
|
// Do POST asking for data about all the new articles
|
2019-06-19 18:25:37 +02:00
|
|
|
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.contents.rawValue), credentials: self.credentials)
|
2019-06-10 22:53:35 +02:00
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
// Get ids from above into hex representation of value
|
|
|
|
let idsToFetch = articleIDs.map({ (reference) -> String in
|
|
|
|
return "i=\(reference)"
|
|
|
|
}).joined(separator:"&")
|
|
|
|
|
|
|
|
let postData = "T=\(token)&output=json&\(idsToFetch)".data(using: String.Encoding.utf8)
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: ReaderAPIEntryWrapper.self, completion: { (result) in
|
2019-06-10 22:53:35 +02:00
|
|
|
switch result {
|
2019-06-15 21:46:13 +02:00
|
|
|
case .success(let (_, entryWrapper)):
|
2019-06-10 22:53:35 +02:00
|
|
|
guard let entryWrapper = entryWrapper else {
|
2019-06-19 18:25:37 +02:00
|
|
|
completion(.failure(ReaderAPIAccountDelegateError.invalidResponse))
|
2019-06-10 22:53:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
completion(.success((entryWrapper.entries)))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 22:53:35 +02:00
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func retrieveEntries(feedID: String, completion: @escaping (Result<([ReaderAPIEntry]?, String?), Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
let since = Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()
|
|
|
|
|
2019-06-15 21:46:13 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add query string for getting JSON (probably should break this out as I will be doing it a lot)
|
2019-06-19 18:25:37 +02:00
|
|
|
guard var components = URLComponents(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.itemIds.rawValue), resolvingAgainstBaseURL: false) else {
|
2019-06-15 21:46:13 +02:00
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
components.queryItems = [
|
|
|
|
URLQueryItem(name: "s", value: feedID),
|
|
|
|
URLQueryItem(name: "ot", value: String(since.timeIntervalSince1970)),
|
|
|
|
URLQueryItem(name: "output", value: "json")
|
|
|
|
]
|
|
|
|
|
|
|
|
guard let callURL = components.url else {
|
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: nil)
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-06-15 21:46:13 +02:00
|
|
|
case .success(let (_, unreadEntries)):
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-15 21:46:13 +02:00
|
|
|
guard let itemRefs = unreadEntries?.itemRefs else {
|
|
|
|
completion(.success(([], nil)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let itemIds = itemRefs.map { (reference) -> String in
|
|
|
|
// Convert the IDs to the (stupid) Google Hex Format
|
|
|
|
let idValue = Int(reference.itemId)!
|
|
|
|
return String(idValue, radix: 16, uppercase: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.retrieveEntries(articleIDs: itemIds) { (results) in
|
|
|
|
switch results {
|
|
|
|
case .success(let entries):
|
|
|
|
completion(.success((entries,nil)))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func retrieveEntries(completion: @escaping (Result<([ReaderAPIEntry]?, String?, Int?), Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-10 22:53:35 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
let since: Date = {
|
2019-06-10 22:53:35 +02:00
|
|
|
if let lastArticleFetch = self.accountMetadata?.lastArticleFetch {
|
2019-05-28 19:08:15 +02:00
|
|
|
return lastArticleFetch
|
|
|
|
} else {
|
|
|
|
return Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-06-10 22:53:35 +02:00
|
|
|
let sinceString = since.timeIntervalSince1970
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-10 22:53:35 +02:00
|
|
|
// Add query string for getting JSON (probably should break this out as I will be doing it a lot)
|
2019-06-19 18:25:37 +02:00
|
|
|
guard var components = URLComponents(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.itemIds.rawValue), resolvingAgainstBaseURL: false) else {
|
2019-06-10 22:53:35 +02:00
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
components.queryItems = [
|
|
|
|
URLQueryItem(name: "o", value: String(sinceString)),
|
|
|
|
URLQueryItem(name: "n", value: "10000"),
|
|
|
|
URLQueryItem(name: "output", value: "json"),
|
2019-06-19 18:25:37 +02:00
|
|
|
URLQueryItem(name: "xt", value: ReaderState.read.rawValue),
|
|
|
|
URLQueryItem(name: "s", value: ReaderStreams.readingList.rawValue)
|
2019-06-10 22:53:35 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
guard let callURL = components.url else {
|
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.unreadEntries]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
self.transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-06-10 22:53:35 +02:00
|
|
|
case .success(let (_, entries)):
|
|
|
|
|
|
|
|
guard let entries = entries else {
|
2019-06-19 18:25:37 +02:00
|
|
|
completion(.failure(ReaderAPIAccountDelegateError.invalidResponse))
|
2019-06-10 22:53:35 +02:00
|
|
|
return
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-10 22:53:35 +02:00
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success(let token):
|
|
|
|
// Do POST asking for data about all the new articles
|
2019-06-19 18:25:37 +02:00
|
|
|
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.contents.rawValue), credentials: self.credentials)
|
2019-06-10 22:53:35 +02:00
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
// Get ids from above into hex representation of value
|
|
|
|
let idsToFetch = entries.itemRefs.map({ (reference) -> String in
|
|
|
|
let idValue = Int(reference.itemId)!
|
|
|
|
let idHexString = String(idValue, radix: 16, uppercase: false)
|
|
|
|
return "i=\(idHexString)"
|
|
|
|
}).joined(separator:"&")
|
|
|
|
|
|
|
|
let postData = "T=\(token)&output=json&\(idsToFetch)".data(using: String.Encoding.utf8)
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: ReaderAPIEntryWrapper.self, completion: { (result) in
|
2019-06-10 22:53:35 +02:00
|
|
|
switch result {
|
|
|
|
case .success(let (response, entryWrapper)):
|
|
|
|
guard let entryWrapper = entryWrapper else {
|
2019-06-19 18:25:37 +02:00
|
|
|
completion(.failure(ReaderAPIAccountDelegateError.invalidResponse))
|
2019-06-10 22:53:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let dateInfo = HTTPDateInfo(urlResponse: response)
|
|
|
|
self.accountMetadata?.lastArticleFetch = dateInfo?.date
|
|
|
|
|
|
|
|
|
|
|
|
completion(.success((entryWrapper.entries, nil, nil)))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
|
|
|
self.accountMetadata?.lastArticleFetch = nil
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func retrieveEntries(page: String, completion: @escaping (Result<([ReaderAPIEntry]?, String?), Error>) -> Void) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
guard let url = URL(string: page), var callComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
|
|
|
completion(.success((nil, nil)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
callComponents.queryItems?.append(URLQueryItem(name: "mode", value: "extended"))
|
|
|
|
let request = URLRequest(url: callComponents.url!, credentials: credentials)
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
transport.send(request: request, resultType: [ReaderAPIEntry].self) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (response, entries)):
|
|
|
|
|
|
|
|
let pagingInfo = HTTPLinkPagingInfo(urlResponse: response)
|
|
|
|
completion(.success((entries, pagingInfo.nextPage)))
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
self.accountMetadata?.lastArticleFetch = nil
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func retrieveUnreadEntries(completion: @escaping (Result<[Int]?, Error>) -> Void) {
|
|
|
|
|
2019-06-10 22:53:35 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add query string for getting JSON (probably should break this out as I will be doing it a lot)
|
2019-06-19 18:25:37 +02:00
|
|
|
guard var components = URLComponents(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.itemIds.rawValue), resolvingAgainstBaseURL: false) else {
|
2019-06-10 22:53:35 +02:00
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
components.queryItems = [
|
2019-06-19 18:25:37 +02:00
|
|
|
URLQueryItem(name: "s", value: ReaderStreams.readingList.rawValue),
|
2019-06-10 22:53:35 +02:00
|
|
|
URLQueryItem(name: "n", value: "10000"),
|
2019-06-19 18:25:37 +02:00
|
|
|
URLQueryItem(name: "xt", value: ReaderState.read.rawValue),
|
2019-06-10 22:53:35 +02:00
|
|
|
URLQueryItem(name: "output", value: "json")
|
|
|
|
]
|
|
|
|
|
|
|
|
guard let callURL = components.url else {
|
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.unreadEntries]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let (response, unreadEntries)):
|
2019-06-10 22:53:35 +02:00
|
|
|
|
|
|
|
guard let itemRefs = unreadEntries?.itemRefs else {
|
|
|
|
completion(.success([]))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let itemIds = itemRefs.map{ Int($0.itemId)! }
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.unreadEntries, headers: response.allHeaderFields)
|
2019-06-10 22:53:35 +02:00
|
|
|
completion(.success(itemIds))
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
func updateStateToEntries(entries: [Int], state: ReaderState, add: Bool, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-11 22:42:28 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success(let token):
|
|
|
|
// Do POST asking for data about all the new articles
|
2019-06-19 18:25:37 +02:00
|
|
|
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.editTag.rawValue), credentials: self.credentials)
|
2019-06-11 22:42:28 +02:00
|
|
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
|
|
|
|
// Get ids from above into hex representation of value
|
|
|
|
let idsToFetch = entries.map({ (idValue) -> String in
|
2019-06-12 22:41:44 +02:00
|
|
|
let idHexString = String(format: "%.16llx", idValue)
|
2019-06-11 22:42:28 +02:00
|
|
|
return "i=\(idHexString)"
|
|
|
|
}).joined(separator:"&")
|
|
|
|
|
2019-06-13 15:35:01 +02:00
|
|
|
let actionIndicator = add ? "a" : "r"
|
|
|
|
|
2019-06-15 14:21:13 +02:00
|
|
|
let postData = "T=\(token)&\(idsToFetch)&\(actionIndicator)=\(state.rawValue)".data(using: String.Encoding.utf8)
|
2019-06-11 22:42:28 +02:00
|
|
|
|
|
|
|
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 15:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createUnreadEntries(entries: [Int], completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
updateStateToEntries(entries: entries, state: .read, add: false, completion: completion)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteUnreadEntries(entries: [Int], completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-06-13 15:35:01 +02:00
|
|
|
updateStateToEntries(entries: entries, state: .read, add: true, completion: completion)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func createStarredEntries(entries: [Int], completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
updateStateToEntries(entries: entries, state: .starred, add: true, completion: completion)
|
2019-06-11 22:42:28 +02:00
|
|
|
|
2019-06-13 15:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteStarredEntries(entries: [Int], completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
updateStateToEntries(entries: entries, state: .starred, add: false, completion: completion)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func retrieveStarredEntries(completion: @escaping (Result<[Int]?, Error>) -> Void) {
|
2019-06-11 21:37:21 +02:00
|
|
|
guard let baseURL = APIBaseURL else {
|
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
guard var components = URLComponents(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.itemIds.rawValue), resolvingAgainstBaseURL: false) else {
|
2019-06-11 21:37:21 +02:00
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
components.queryItems = [
|
|
|
|
URLQueryItem(name: "s", value: "user/-/state/com.google/starred"),
|
|
|
|
URLQueryItem(name: "n", value: "10000"),
|
|
|
|
URLQueryItem(name: "output", value: "json")
|
|
|
|
]
|
|
|
|
|
|
|
|
guard let callURL = components.url else {
|
|
|
|
completion(.failure(TransportError.noURL))
|
|
|
|
return
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.starredEntries]
|
|
|
|
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self) { result in
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-06-11 21:37:21 +02:00
|
|
|
case .success(let (response, unreadEntries)):
|
|
|
|
|
|
|
|
guard let itemRefs = unreadEntries?.itemRefs else {
|
|
|
|
completion(.success([]))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let itemIds = itemRefs.map{ Int($0.itemId)! }
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
self.storeConditionalGet(key: ConditionalGetKeys.starredEntries, headers: response.allHeaderFields)
|
2019-06-11 21:37:21 +02:00
|
|
|
completion(.success(itemIds))
|
2019-05-28 19:08:15 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:35:01 +02:00
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
extension ReaderAPICaller {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
func storeConditionalGet(key: String, headers: [AnyHashable : Any]) {
|
|
|
|
if var conditionalGet = accountMetadata?.conditionalGetInfo {
|
|
|
|
conditionalGet[key] = HTTPConditionalGetInfo(headers: headers)
|
|
|
|
accountMetadata?.conditionalGetInfo = conditionalGet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|