Fix lint issues.

This commit is contained in:
Brent Simmons 2025-01-22 21:49:54 -08:00
parent e52d4c28df
commit 361ecd20e3

View File

@ -16,23 +16,23 @@ enum CreateReaderAPISubscriptionResult {
} }
final class ReaderAPICaller: NSObject { final class ReaderAPICaller: NSObject {
enum ItemIDType { enum ItemIDType {
case unread case unread
case starred case starred
case allForAccount case allForAccount
case allForFeed case allForFeed
} }
private enum ReaderState: String { private enum ReaderState: String {
case read = "user/-/state/com.google/read" case read = "user/-/state/com.google/read"
case starred = "user/-/state/com.google/starred" case starred = "user/-/state/com.google/starred"
} }
private enum ReaderStreams: String { private enum ReaderStreams: String {
case readingList = "user/-/state/com.google/reading-list" case readingList = "user/-/state/com.google/reading-list"
} }
private enum ReaderAPIEndpoints: String { private enum ReaderAPIEndpoints: String {
case login = "/accounts/ClientLogin" case login = "/accounts/ClientLogin"
case token = "/reader/api/0/token" case token = "/reader/api/0/token"
@ -46,12 +46,12 @@ final class ReaderAPICaller: NSObject {
case itemIds = "/reader/api/0/stream/items/ids" case itemIds = "/reader/api/0/stream/items/ids"
case editTag = "/reader/api/0/edit-tag" case editTag = "/reader/api/0/edit-tag"
} }
private var transport: Transport! private var transport: Transport!
private let uriComponentAllowed: CharacterSet private let uriComponentAllowed: CharacterSet
private var accessToken: String? private var accessToken: String?
weak var accountMetadata: AccountMetadata? weak var accountMetadata: AccountMetadata?
var variant: ReaderAPIVariant = .generic var variant: ReaderAPIVariant = .generic
@ -62,7 +62,7 @@ final class ReaderAPICaller: NSObject {
return apiBaseURL?.host return apiBaseURL?.host
} }
} }
private var apiBaseURL: URL? { private var apiBaseURL: URL? {
get { get {
switch variant { switch variant {
@ -76,27 +76,27 @@ final class ReaderAPICaller: NSObject {
} }
} }
} }
init(transport: Transport) { init(transport: Transport) {
self.transport = transport self.transport = transport
var urlHostAllowed = CharacterSet.urlHostAllowed var urlHostAllowed = CharacterSet.urlHostAllowed
urlHostAllowed.remove("+") urlHostAllowed.remove("+")
urlHostAllowed.remove("&") urlHostAllowed.remove("&")
uriComponentAllowed = urlHostAllowed uriComponentAllowed = urlHostAllowed
super.init() super.init()
} }
func cancelAll() { func cancelAll() {
transport.cancelAll() transport.cancelAll()
} }
func validateCredentials(endpoint: URL, completion: @escaping (Result<Credentials?, Error>) -> Void) { func validateCredentials(endpoint: URL, completion: @escaping (Result<Credentials?, Error>) -> Void) {
guard let credentials = credentials else { guard let credentials = credentials else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
var request = URLRequest(url: endpoint.appendingPathComponent(ReaderAPIEndpoints.login.rawValue), credentials: credentials) var request = URLRequest(url: endpoint.appendingPathComponent(ReaderAPIEndpoints.login.rawValue), credentials: credentials)
addVariantHeaders(&request) addVariantHeaders(&request)
@ -107,29 +107,29 @@ final class ReaderAPICaller: NSObject {
completion(.failure(TransportError.noData)) completion(.failure(TransportError.noData))
break break
} }
// Convert the return data to UTF8 and then parse out the Auth token // Convert the return data to UTF8 and then parse out the Auth token
guard let rawData = String(data: resultData, encoding: .utf8) else { guard let rawData = String(data: resultData, encoding: .utf8) else {
completion(.failure(TransportError.noData)) completion(.failure(TransportError.noData))
break break
} }
var authData: [String: String] = [:] var authData: [String: String] = [:]
rawData.split(separator: "\n").forEach({ (line: Substring) in rawData.split(separator: "\n").forEach({ (line: Substring) in
let items = line.split(separator: "=").map{String($0)} let items = line.split(separator: "=").map {String($0)}
if items.count == 2 { if items.count == 2 {
authData[items[0]] = items[1] authData[items[0]] = items[1]
} }
}) })
guard let authString = authData["Auth"] else { guard let authString = authData["Auth"] else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
break break
} }
// Save Auth Token for later use // Save Auth Token for later use
self.credentials = Credentials(type: .readerAPIKey, username: credentials.username, secret: authString) self.credentials = Credentials(type: .readerAPIKey, username: credentials.username, secret: authString)
completion(.success(self.credentials)) completion(.success(self.credentials))
case .failure(let error): case .failure(let error):
if let transportError = error as? TransportError, case .httpError(let code) = transportError, code == 404 { if let transportError = error as? TransportError, case .httpError(let code) = transportError, code == 404 {
@ -139,25 +139,25 @@ final class ReaderAPICaller: NSObject {
} }
} }
} }
} }
func requestAuthorizationToken(endpoint: URL, completion: @escaping (Result<String, Error>) -> Void) { func requestAuthorizationToken(endpoint: URL, completion: @escaping (Result<String, Error>) -> Void) {
// If we have a token already, use it // If we have a token already, use it
if let accessToken = accessToken { if let accessToken = accessToken {
completion(.success(accessToken)) completion(.success(accessToken))
return return
} }
// Otherwise request one. // Otherwise request one.
guard let credentials = credentials else { guard let credentials = credentials else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
var request = URLRequest(url: endpoint.appendingPathComponent(ReaderAPIEndpoints.token.rawValue), credentials: credentials) var request = URLRequest(url: endpoint.appendingPathComponent(ReaderAPIEndpoints.token.rawValue), credentials: credentials)
addVariantHeaders(&request) addVariantHeaders(&request)
transport.send(request: request) { result in transport.send(request: request) { result in
switch result { switch result {
case .success(let (_, data)): case .success(let (_, data)):
@ -165,13 +165,13 @@ final class ReaderAPICaller: NSObject {
completion(.failure(TransportError.noData)) completion(.failure(TransportError.noData))
break break
} }
// Convert the return data to UTF8 and then parse out the Auth token // Convert the return data to UTF8 and then parse out the Auth token
guard let accessToken = String(data: resultData, encoding: .utf8) else { guard let accessToken = String(data: resultData, encoding: .utf8) else {
completion(.failure(TransportError.noData)) completion(.failure(TransportError.noData))
break break
} }
self.accessToken = accessToken self.accessToken = accessToken
completion(.success(accessToken)) completion(.success(accessToken))
case .failure(let error): case .failure(let error):
@ -179,22 +179,21 @@ final class ReaderAPICaller: NSObject {
} }
} }
} }
func retrieveTags(completion: @escaping (Result<[ReaderAPITag]?, Error>) -> Void) { func retrieveTags(completion: @escaping (Result<[ReaderAPITag]?, Error>) -> Void) {
guard let baseURL = apiBaseURL else { guard let baseURL = apiBaseURL else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
var url = baseURL var url = baseURL
.appendingPathComponent(ReaderAPIEndpoints.tagList.rawValue) .appendingPathComponent(ReaderAPIEndpoints.tagList.rawValue)
.appendingQueryItem(URLQueryItem(name: "output", value: "json")) .appendingQueryItem(URLQueryItem(name: "output", value: "json"))
if variant == .inoreader { if variant == .inoreader {
url = url?.appendingQueryItem(URLQueryItem(name: "types", value: "1")) url = url?.appendingQueryItem(URLQueryItem(name: "types", value: "1"))
} }
guard let callURL = url else { guard let callURL = url else {
completion(.failure(TransportError.noURL)) completion(.failure(TransportError.noURL))
return return
@ -211,7 +210,7 @@ final class ReaderAPICaller: NSObject {
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func renameTag(oldName: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) { func renameTag(oldName: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
@ -219,7 +218,7 @@ final class ReaderAPICaller: NSObject {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in self.requestAuthorizationToken(endpoint: baseURL) { (result) in
switch result { switch result {
case .success(let token): case .success(let token):
@ -227,45 +226,42 @@ final class ReaderAPICaller: NSObject {
self.addVariantHeaders(&request) self.addVariantHeaders(&request)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST" request.httpMethod = "POST"
guard let encodedOldName = self.encodeForURLPath(oldName), let encodedNewName = self.encodeForURLPath(newName) else { guard let encodedOldName = self.encodeForURLPath(oldName), let encodedNewName = self.encodeForURLPath(newName) else {
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter)) completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
return return
} }
let oldTagName = "user/-/label/\(encodedOldName)" let oldTagName = "user/-/label/\(encodedOldName)"
let newTagName = "user/-/label/\(encodedNewName)" let newTagName = "user/-/label/\(encodedNewName)"
let postData = "T=\(token)&s=\(oldTagName)&dest=\(newTagName)".data(using: String.Encoding.utf8) 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 self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
switch result { switch result {
case .success: case .success:
completion(.success(())) completion(.success(()))
break case .failure(let error):
case .failure(let error):
completion(.failure(error)) completion(.failure(error))
break
} }
}) })
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func deleteTag(folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) { func deleteTag(folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
guard let baseURL = apiBaseURL else { guard let baseURL = apiBaseURL else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
guard let folderExternalID = folder.externalID else { guard let folderExternalID = folder.externalID else {
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter)) completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
return return
} }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in self.requestAuthorizationToken(endpoint: baseURL) { (result) in
switch result { switch result {
case .success(let token): case .success(let token):
@ -273,42 +269,39 @@ final class ReaderAPICaller: NSObject {
self.addVariantHeaders(&request) self.addVariantHeaders(&request)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST" request.httpMethod = "POST"
let postData = "T=\(token)&s=\(folderExternalID)".data(using: String.Encoding.utf8) let postData = "T=\(token)&s=\(folderExternalID)".data(using: String.Encoding.utf8)
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
switch result { switch result {
case .success: case .success:
completion(.success(())) completion(.success(()))
break case .failure(let error):
case .failure(let error):
completion(.failure(error)) completion(.failure(error))
break
} }
}) })
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func retrieveSubscriptions(completion: @escaping (Result<[ReaderAPISubscription]?, Error>) -> Void) { func retrieveSubscriptions(completion: @escaping (Result<[ReaderAPISubscription]?, Error>) -> Void) {
guard let baseURL = apiBaseURL else { guard let baseURL = apiBaseURL else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
let url = baseURL let url = baseURL
.appendingPathComponent(ReaderAPIEndpoints.subscriptionList.rawValue) .appendingPathComponent(ReaderAPIEndpoints.subscriptionList.rawValue)
.appendingQueryItem(URLQueryItem(name: "output", value: "json")) .appendingQueryItem(URLQueryItem(name: "output", value: "json"))
guard let callURL = url else { guard let callURL = url else {
completion(.failure(TransportError.noURL)) completion(.failure(TransportError.noURL))
return return
} }
var request = URLRequest(url: callURL, credentials: credentials) var request = URLRequest(url: callURL, credentials: credentials)
addVariantHeaders(&request) addVariantHeaders(&request)
@ -321,13 +314,13 @@ final class ReaderAPICaller: NSObject {
} }
} }
} }
func createSubscription(url: String, name: String?, folder: Folder?, completion: @escaping (Result<CreateReaderAPISubscriptionResult, Error>) -> Void) { func createSubscription(url: String, name: String?, folder: Folder?, completion: @escaping (Result<CreateReaderAPISubscriptionResult, Error>) -> Void) {
guard let baseURL = apiBaseURL else { guard let baseURL = apiBaseURL else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
func findSubscription(streamID: String, completion: @escaping (Result<CreateReaderAPISubscriptionResult, Error>) -> Void) { func findSubscription(streamID: String, completion: @escaping (Result<CreateReaderAPISubscriptionResult, Error>) -> Void) {
// There is no call to get a single subscription entry, so we get them all, // 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 // look up the one we just subscribed to and return that
@ -353,19 +346,18 @@ final class ReaderAPICaller: NSObject {
} }
}) })
} }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in self.requestAuthorizationToken(endpoint: baseURL) { (result) in
switch result { switch result {
case .success(let token): case .success(let token):
let callURL = baseURL let callURL = baseURL
.appendingPathComponent(ReaderAPIEndpoints.subscriptionAdd.rawValue) .appendingPathComponent(ReaderAPIEndpoints.subscriptionAdd.rawValue)
var request = URLRequest(url: callURL, credentials: self.credentials) var request = URLRequest(url: callURL, credentials: self.credentials)
self.addVariantHeaders(&request) self.addVariantHeaders(&request)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST" request.httpMethod = "POST"
guard let encodedFeedURL = self.encodeForURLPath(url) else { guard let encodedFeedURL = self.encodeForURLPath(url) else {
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter)) completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
return return
@ -387,7 +379,7 @@ final class ReaderAPICaller: NSObject {
findSubscription(streamID: streamId, completion: completion) findSubscription(streamID: streamId, completion: completion)
} }
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
@ -397,21 +389,21 @@ final class ReaderAPICaller: NSObject {
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func renameSubscription(subscriptionID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) { func renameSubscription(subscriptionID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
changeSubscription(subscriptionID: subscriptionID, title: newName, completion: completion) changeSubscription(subscriptionID: subscriptionID, title: newName, completion: completion)
} }
func deleteSubscription(subscriptionID: String, completion: @escaping (Result<Void, Error>) -> Void) { func deleteSubscription(subscriptionID: String, completion: @escaping (Result<Void, Error>) -> Void) {
guard let baseURL = apiBaseURL else { guard let baseURL = apiBaseURL else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in self.requestAuthorizationToken(endpoint: baseURL) { (result) in
switch result { switch result {
case .success(let token): case .success(let token):
@ -419,49 +411,47 @@ final class ReaderAPICaller: NSObject {
self.addVariantHeaders(&request) self.addVariantHeaders(&request)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST" request.httpMethod = "POST"
let postData = "T=\(token)&s=\(subscriptionID)&ac=unsubscribe".data(using: String.Encoding.utf8) 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 self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
switch result { switch result {
case .success: case .success:
completion(.success(())) completion(.success(()))
break case .failure(let error):
case .failure(let error):
completion(.failure(error)) completion(.failure(error))
break
} }
}) })
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func createTagging(subscriptionID: String, tagName: String, completion: @escaping (Result<Void, Error>) -> Void) { func createTagging(subscriptionID: String, tagName: String, completion: @escaping (Result<Void, Error>) -> Void) {
changeSubscription(subscriptionID: subscriptionID, addTagName: tagName, completion: completion) changeSubscription(subscriptionID: subscriptionID, addTagName: tagName, completion: completion)
} }
func deleteTagging(subscriptionID: String, tagName: String, completion: @escaping (Result<Void, Error>) -> Void) { func deleteTagging(subscriptionID: String, tagName: String, completion: @escaping (Result<Void, Error>) -> Void) {
changeSubscription(subscriptionID: subscriptionID, removeTagName: tagName, completion: completion) changeSubscription(subscriptionID: subscriptionID, removeTagName: tagName, completion: completion)
} }
func moveSubscription(subscriptionID: String, fromTag: String, toTag: String, completion: @escaping (Result<Void, Error>) -> Void) { func moveSubscription(subscriptionID: String, fromTag: String, toTag: String, completion: @escaping (Result<Void, Error>) -> Void) {
changeSubscription(subscriptionID: subscriptionID, removeTagName: fromTag, addTagName: toTag, completion: completion) changeSubscription(subscriptionID: subscriptionID, removeTagName: fromTag, addTagName: toTag, completion: completion)
} }
private func changeSubscription(subscriptionID: String, removeTagName: String? = nil, addTagName: String? = nil, title: String? = nil, completion: @escaping (Result<Void, Error>) -> Void) { private func changeSubscription(subscriptionID: String, removeTagName: String? = nil, addTagName: String? = nil, title: String? = nil, completion: @escaping (Result<Void, Error>) -> Void) {
guard removeTagName != nil || addTagName != nil || title != nil else { guard removeTagName != nil || addTagName != nil || title != nil else {
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter)) completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
return return
} }
guard let baseURL = apiBaseURL else { guard let baseURL = apiBaseURL else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in self.requestAuthorizationToken(endpoint: baseURL) { (result) in
switch result { switch result {
case .success(let token): case .success(let token):
@ -469,7 +459,7 @@ final class ReaderAPICaller: NSObject {
self.addVariantHeaders(&request) self.addVariantHeaders(&request)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST" request.httpMethod = "POST"
var postString = "T=\(token)&s=\(subscriptionID)&ac=edit" var postString = "T=\(token)&s=\(subscriptionID)&ac=edit"
if let fromLabel = self.encodeForURLPath(removeTagName) { if let fromLabel = self.encodeForURLPath(removeTagName) {
postString += "&r=user/-/label/\(fromLabel)" postString += "&r=user/-/label/\(fromLabel)"
@ -481,36 +471,34 @@ final class ReaderAPICaller: NSObject {
postString += "&t=\(encodedTitle)" postString += "&t=\(encodedTitle)"
} }
let postData = postString.data(using: String.Encoding.utf8) let postData = postString.data(using: String.Encoding.utf8)
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
switch result { switch result {
case .success: case .success:
completion(.success(())) completion(.success(()))
break case .failure(let error):
case .failure(let error):
completion(.failure(error)) completion(.failure(error))
break
} }
}) })
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func retrieveEntries(articleIDs: [String], completion: @escaping (Result<([ReaderAPIEntry]?), Error>) -> Void) { func retrieveEntries(articleIDs: [String], completion: @escaping (Result<([ReaderAPIEntry]?), Error>) -> Void) {
guard !articleIDs.isEmpty else { guard !articleIDs.isEmpty else {
completion(.success(([ReaderAPIEntry]()))) completion(.success(([ReaderAPIEntry]())))
return return
} }
guard let baseURL = apiBaseURL else { guard let baseURL = apiBaseURL else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in self.requestAuthorizationToken(endpoint: baseURL) { (result) in
switch result { switch result {
case .success(let token): case .success(let token):
@ -518,7 +506,7 @@ final class ReaderAPICaller: NSObject {
self.addVariantHeaders(&request) self.addVariantHeaders(&request)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST" request.httpMethod = "POST"
// Get ids from above into hex representation of value // Get ids from above into hex representation of value
let idsToFetch = articleIDs.map({ articleID -> String in let idsToFetch = articleIDs.map({ articleID -> String in
if self.variant == .theOldReader { if self.variant == .theOldReader {
@ -528,10 +516,10 @@ final class ReaderAPICaller: NSObject {
let idHexString = String(idValue, radix: 16, uppercase: false) let idHexString = String(idValue, radix: 16, uppercase: false)
return "i=tag:google.com,2005:reader/item/\(idHexString)" return "i=tag:google.com,2005:reader/item/\(idHexString)"
} }
}).joined(separator:"&") }).joined(separator: "&")
let postData = "T=\(token)&output=json&\(idsToFetch)".data(using: String.Encoding.utf8) let postData = "T=\(token)&output=json&\(idsToFetch)".data(using: String.Encoding.utf8)
self.transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: ReaderAPIEntryWrapper.self, completion: { (result) in self.transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: ReaderAPIEntryWrapper.self, completion: { (result) in
switch result { switch result {
case .success(let (_, entryWrapper)): case .success(let (_, entryWrapper)):
@ -539,32 +527,31 @@ final class ReaderAPICaller: NSObject {
completion(.failure(ReaderAPIAccountDelegateError.invalidResponse)) completion(.failure(ReaderAPIAccountDelegateError.invalidResponse))
return return
} }
completion(.success((entryWrapper.entries))) completion(.success((entryWrapper.entries)))
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
}) })
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func retrieveItemIDs(type: ItemIDType, feedID: String? = nil, completion: @escaping ((Result<[String], Error>) -> Void)) { func retrieveItemIDs(type: ItemIDType, feedID: String? = nil, completion: @escaping ((Result<[String], Error>) -> Void)) {
guard let baseURL = apiBaseURL else { guard let baseURL = apiBaseURL else {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
var queryItems = [ var queryItems = [
URLQueryItem(name: "n", value: "1000"), URLQueryItem(name: "n", value: "1000"),
URLQueryItem(name: "output", value: "json") URLQueryItem(name: "output", value: "json")
] ]
switch type { switch type {
case .allForAccount: case .allForAccount:
let since: Date = { let since: Date = {
@ -574,7 +561,7 @@ final class ReaderAPICaller: NSObject {
return Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date() return Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()
} }
}() }()
let sinceTimeInterval = since.timeIntervalSince1970 let sinceTimeInterval = since.timeIntervalSince1970
queryItems.append(URLQueryItem(name: "ot", value: String(Int(sinceTimeInterval)))) queryItems.append(URLQueryItem(name: "ot", value: String(Int(sinceTimeInterval))))
queryItems.append(URLQueryItem(name: "s", value: ReaderStreams.readingList.rawValue)) queryItems.append(URLQueryItem(name: "s", value: ReaderStreams.readingList.rawValue))
@ -592,16 +579,16 @@ final class ReaderAPICaller: NSObject {
case .starred: case .starred:
queryItems.append(URLQueryItem(name: "s", value: ReaderState.starred.rawValue)) queryItems.append(URLQueryItem(name: "s", value: ReaderState.starred.rawValue))
} }
let url = baseURL let url = baseURL
.appendingPathComponent(ReaderAPIEndpoints.itemIds.rawValue) .appendingPathComponent(ReaderAPIEndpoints.itemIds.rawValue)
.appendingQueryItems(queryItems) .appendingQueryItems(queryItems)
guard let callURL = url else { guard let callURL = url else {
completion(.failure(TransportError.noURL)) completion(.failure(TransportError.noURL))
return return
} }
var request: URLRequest = URLRequest(url: callURL, credentials: credentials) var request: URLRequest = URLRequest(url: callURL, credentials: credentials)
addVariantHeaders(&request) addVariantHeaders(&request)
@ -630,21 +617,21 @@ final class ReaderAPICaller: NSObject {
completion(.success(itemIDs)) completion(.success(itemIDs))
return return
} }
guard var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else { guard var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter)) completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
return return
} }
var queryItems = urlComponents.queryItems!.filter({ $0.name != "c" }) var queryItems = urlComponents.queryItems!.filter({ $0.name != "c" })
queryItems.append(URLQueryItem(name: "c", value: continuation)) queryItems.append(URLQueryItem(name: "c", value: continuation))
urlComponents.queryItems = queryItems urlComponents.queryItems = queryItems
guard let callURL = urlComponents.url else { guard let callURL = urlComponents.url else {
completion(.failure(TransportError.noURL)) completion(.failure(TransportError.noURL))
return return
} }
var request: URLRequest = URLRequest(url: callURL, credentials: credentials) var request: URLRequest = URLRequest(url: callURL, credentials: credentials)
addVariantHeaders(&request) addVariantHeaders(&request)
@ -663,34 +650,34 @@ final class ReaderAPICaller: NSObject {
} }
} }
} }
func createUnreadEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) { func createUnreadEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) {
updateStateToEntries(entries: entries, state: .read, add: false, completion: completion) updateStateToEntries(entries: entries, state: .read, add: false, completion: completion)
} }
func deleteUnreadEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) { func deleteUnreadEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) {
updateStateToEntries(entries: entries, state: .read, add: true, completion: completion) updateStateToEntries(entries: entries, state: .read, add: true, completion: completion)
} }
func createStarredEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) { func createStarredEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) {
updateStateToEntries(entries: entries, state: .starred, add: true, completion: completion) updateStateToEntries(entries: entries, state: .starred, add: true, completion: completion)
} }
func deleteStarredEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) { func deleteStarredEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) {
updateStateToEntries(entries: entries, state: .starred, add: false, completion: completion) updateStateToEntries(entries: entries, state: .starred, add: false, completion: completion)
} }
} }
// MARK: Private // MARK: Private
private extension ReaderAPICaller { private extension ReaderAPICaller {
func encodeForURLPath(_ pathComponent: String?) -> String? { func encodeForURLPath(_ pathComponent: String?) -> String? {
guard let pathComponent = pathComponent else { return nil } guard let pathComponent = pathComponent else { return nil }
return pathComponent.addingPercentEncoding(withAllowedCharacters: uriComponentAllowed) return pathComponent.addingPercentEncoding(withAllowedCharacters: uriComponentAllowed)
} }
func addVariantHeaders(_ request: inout URLRequest) { func addVariantHeaders(_ request: inout URLRequest) {
if variant == .inoreader { if variant == .inoreader {
request.addValue(SecretKey.inoreaderAppID, forHTTPHeaderField: "AppId") request.addValue(SecretKey.inoreaderAppID, forHTTPHeaderField: "AppId")
@ -703,7 +690,7 @@ private extension ReaderAPICaller {
completion(.failure(CredentialsError.incompleteCredentials)) completion(.failure(CredentialsError.incompleteCredentials))
return return
} }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in self.requestAuthorizationToken(endpoint: baseURL) { (result) in
switch result { switch result {
case .success(let token): case .success(let token):
@ -712,7 +699,7 @@ private extension ReaderAPICaller {
self.addVariantHeaders(&request) self.addVariantHeaders(&request)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST" request.httpMethod = "POST"
// Get ids from above into hex representation of value // Get ids from above into hex representation of value
let idsToFetch = entries.compactMap({ idValue -> String? in let idsToFetch = entries.compactMap({ idValue -> String? in
if self.variant == .theOldReader { if self.variant == .theOldReader {
@ -722,12 +709,12 @@ private extension ReaderAPICaller {
let idHexString = String(format: "%.16llx", intValue) let idHexString = String(format: "%.16llx", intValue)
return "i=tag:google.com,2005:reader/item/\(idHexString)" return "i=tag:google.com,2005:reader/item/\(idHexString)"
} }
}).joined(separator:"&") }).joined(separator: "&")
let actionIndicator = add ? "a" : "r" let actionIndicator = add ? "a" : "r"
let postData = "T=\(token)&\(idsToFetch)&\(actionIndicator)=\(state.rawValue)".data(using: String.Encoding.utf8) let postData = "T=\(token)&\(idsToFetch)&\(actionIndicator)=\(state.rawValue)".data(using: String.Encoding.utf8)
self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in self.transport.send(request: request, method: HTTPMethod.post, payload: postData!, completion: { (result) in
switch result { switch result {
case .success: case .success:
@ -736,13 +723,11 @@ private extension ReaderAPICaller {
completion(.failure(error)) completion(.failure(error))
} }
}) })
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
} }