mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-02-03 12:27:32 +01:00
Convert ReaderAPI code to async await.
This commit is contained in:
parent
e7abe3fa7a
commit
547fc4a676
File diff suppressed because it is too large
Load Diff
@ -140,162 +140,107 @@ enum CreateReaderAPISubscriptionResult {
|
||||
}
|
||||
}
|
||||
|
||||
func requestAuthorizationToken(endpoint: URL, completion: @escaping (Result<String, Error>) -> Void) {
|
||||
func requestAuthorizationToken(endpoint: URL) async throws -> String {
|
||||
|
||||
// If we have a token already, use it
|
||||
if let accessToken = accessToken {
|
||||
completion(.success(accessToken))
|
||||
return
|
||||
if let accessToken {
|
||||
return accessToken
|
||||
}
|
||||
|
||||
|
||||
// Otherwise request one.
|
||||
guard let credentials = credentials else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
guard let credentials else {
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
|
||||
var request = URLRequest(url: endpoint.appendingPathComponent(ReaderAPIEndpoints.token.rawValue), credentials: credentials)
|
||||
addVariantHeaders(&request)
|
||||
|
||||
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 accessToken = String(data: resultData, encoding: .utf8) else {
|
||||
completion(.failure(TransportError.noData))
|
||||
break
|
||||
}
|
||||
|
||||
self.accessToken = accessToken
|
||||
completion(.success(accessToken))
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
|
||||
let (_, data) = try await transport.send(request: request)
|
||||
|
||||
// Convert the return data to UTF8 and then parse out the Auth token
|
||||
guard let data, let accessToken = String(data: data, encoding: .utf8) else {
|
||||
throw TransportError.noData
|
||||
}
|
||||
|
||||
self.accessToken = accessToken
|
||||
return accessToken
|
||||
}
|
||||
|
||||
|
||||
func retrieveTags(completion: @escaping (Result<[ReaderAPITag]?, Error>) -> Void) {
|
||||
func retrieveTags() async throws -> [ReaderAPITag]? {
|
||||
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
|
||||
var url = baseURL
|
||||
.appendingPathComponent(ReaderAPIEndpoints.tagList.rawValue)
|
||||
.appendingQueryItem(URLQueryItem(name: "output", value: "json"))
|
||||
|
||||
|
||||
if variant == .inoreader {
|
||||
url = url?.appendingQueryItem(URLQueryItem(name: "types", value: "1"))
|
||||
}
|
||||
|
||||
|
||||
guard let callURL = url else {
|
||||
completion(.failure(TransportError.noURL))
|
||||
return
|
||||
throw TransportError.noURL
|
||||
}
|
||||
|
||||
var request = URLRequest(url: callURL, credentials: credentials)
|
||||
addVariantHeaders(&request)
|
||||
|
||||
transport.send(request: request, resultType: ReaderAPITagContainer.self) { result in
|
||||
switch result {
|
||||
case .success(let (_, wrapper)):
|
||||
completion(.success(wrapper?.tags))
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
let (_, wrapper) = try await transport.send(request: request, resultType: ReaderAPITagContainer.self)
|
||||
return wrapper?.tags
|
||||
}
|
||||
|
||||
func renameTag(oldName: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
func renameTag(oldName: String, newName: String) async throws {
|
||||
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
||||
switch result {
|
||||
case .success(let token):
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.renameTag.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
request.httpMethod = "POST"
|
||||
|
||||
guard let encodedOldName = self.encodeForURLPath(oldName), let encodedNewName = self.encodeForURLPath(newName) else {
|
||||
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
|
||||
return
|
||||
}
|
||||
|
||||
let oldTagName = "user/-/label/\(encodedOldName)"
|
||||
let newTagName = "user/-/label/\(encodedNewName)"
|
||||
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))
|
||||
}
|
||||
|
||||
let token = try await requestAuthorizationToken(endpoint: baseURL)
|
||||
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.renameTag.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
request.httpMethod = "POST"
|
||||
|
||||
guard let encodedOldName = self.encodeForURLPath(oldName), let encodedNewName = self.encodeForURLPath(newName) else {
|
||||
throw ReaderAPIAccountDelegateError.invalidParameter
|
||||
}
|
||||
|
||||
let oldTagName = "user/-/label/\(encodedOldName)"
|
||||
let newTagName = "user/-/label/\(encodedNewName)"
|
||||
let postData = "T=\(token)&s=\(oldTagName)&dest=\(newTagName)".data(using: String.Encoding.utf8)
|
||||
|
||||
try await transport.send(request: request, method: HTTPMethod.post, payload: postData!)
|
||||
}
|
||||
|
||||
@MainActor func deleteTag(folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
|
||||
|
||||
func deleteTag(folder: Folder) async throws {
|
||||
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
guard let folderExternalID = folder.externalID else {
|
||||
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
|
||||
return
|
||||
throw ReaderAPIAccountDelegateError.invalidParameter
|
||||
}
|
||||
|
||||
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
||||
switch result {
|
||||
case .success(let token):
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.disableTag.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
request.httpMethod = "POST"
|
||||
let token = try await self.requestAuthorizationToken(endpoint: baseURL)
|
||||
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.disableTag.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
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
|
||||
switch result {
|
||||
case .success:
|
||||
completion(.success(()))
|
||||
break
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
break
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
try await self.transport.send(request: request, method: HTTPMethod.post, payload: postData!)
|
||||
}
|
||||
|
||||
func retrieveSubscriptions(completion: @escaping (Result<[ReaderAPISubscription]?, Error>) -> Void) {
|
||||
func retrieveSubscriptions() async throws -> [ReaderAPISubscription]? {
|
||||
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
let url = baseURL
|
||||
@ -303,266 +248,178 @@ enum CreateReaderAPISubscriptionResult {
|
||||
.appendingQueryItem(URLQueryItem(name: "output", value: "json"))
|
||||
|
||||
guard let callURL = url else {
|
||||
completion(.failure(TransportError.noURL))
|
||||
return
|
||||
throw TransportError.noURL
|
||||
}
|
||||
|
||||
var request = URLRequest(url: callURL, credentials: credentials)
|
||||
addVariantHeaders(&request)
|
||||
|
||||
transport.send(request: request, resultType: ReaderAPISubscriptionContainer.self) { result in
|
||||
switch result {
|
||||
case .success(let (_, container)):
|
||||
completion(.success(container?.subscriptions))
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
let (_, container) = try await transport.send(request: request, resultType: ReaderAPISubscriptionContainer.self)
|
||||
return container?.subscriptions
|
||||
}
|
||||
|
||||
func createSubscription(url: String, name: String?, folder: Folder?, completion: @escaping (Result<CreateReaderAPISubscriptionResult, Error>) -> Void) {
|
||||
func createSubscription(url: String, name: String?, folder: Folder?) async throws -> CreateReaderAPISubscriptionResult {
|
||||
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
let token = try await self.requestAuthorizationToken(endpoint: baseURL)
|
||||
|
||||
let callURL = baseURL
|
||||
.appendingPathComponent(ReaderAPIEndpoints.subscriptionAdd.rawValue)
|
||||
|
||||
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,
|
||||
// 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
|
||||
}
|
||||
var request = URLRequest(url: callURL, credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
request.httpMethod = "POST"
|
||||
|
||||
guard let subscription = subscriptions.first(where: { (sub) -> Bool in
|
||||
sub.feedID == streamID
|
||||
}) else {
|
||||
completion(.failure(AccountError.createErrorNotFound))
|
||||
return
|
||||
}
|
||||
|
||||
completion(.success(.created(subscription)))
|
||||
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
||||
switch result {
|
||||
case .success(let token):
|
||||
let callURL = baseURL
|
||||
.appendingPathComponent(ReaderAPIEndpoints.subscriptionAdd.rawValue)
|
||||
|
||||
var request = URLRequest(url: callURL, credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
request.httpMethod = "POST"
|
||||
|
||||
guard let encodedFeedURL = self.encodeForURLPath(url) else {
|
||||
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
|
||||
return
|
||||
}
|
||||
let postData = "T=\(token)&quickadd=\(encodedFeedURL)".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(.notFound))
|
||||
default:
|
||||
guard let streamId = subResult?.streamId else {
|
||||
completion(.failure(AccountError.createErrorNotFound))
|
||||
return
|
||||
}
|
||||
|
||||
findSubscription(streamID: streamId, completion: completion)
|
||||
}
|
||||
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
|
||||
guard let encodedFeedURL = self.encodeForURLPath(url) else {
|
||||
throw ReaderAPIAccountDelegateError.invalidParameter
|
||||
}
|
||||
|
||||
let postData = "T=\(token)&quickadd=\(encodedFeedURL)".data(using: String.Encoding.utf8)
|
||||
|
||||
let (_, subResult) = try await self.transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: ReaderAPIQuickAddResult.self)
|
||||
|
||||
guard let subResult else {
|
||||
return .notFound
|
||||
}
|
||||
if subResult.numResults == 0 {
|
||||
return .notFound
|
||||
}
|
||||
|
||||
// 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
|
||||
guard let subscriptions = try await retrieveSubscriptions() else {
|
||||
throw AccountError.createErrorNotFound
|
||||
}
|
||||
guard let subscription = subscriptions.first(where: { $0.feedID == subResult.streamId }) else {
|
||||
throw AccountError.createErrorNotFound
|
||||
}
|
||||
|
||||
return .created(subscription)
|
||||
}
|
||||
|
||||
func renameSubscription(subscriptionID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
changeSubscription(subscriptionID: subscriptionID, title: newName, completion: completion)
|
||||
func renameSubscription(subscriptionID: String, newName: String) async throws {
|
||||
|
||||
try await changeSubscription(subscriptionID: subscriptionID, title: newName)
|
||||
}
|
||||
|
||||
func deleteSubscription(subscriptionID: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
func deleteSubscription(subscriptionID: String) async throws {
|
||||
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
}
|
||||
|
||||
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
||||
switch result {
|
||||
case .success(let token):
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
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
|
||||
}
|
||||
})
|
||||
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
let token = try await self.requestAuthorizationToken(endpoint: baseURL)
|
||||
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
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)
|
||||
|
||||
try await self.transport.send(request: request, method: HTTPMethod.post, payload: postData!)
|
||||
}
|
||||
|
||||
func createTagging(subscriptionID: String, tagName: String) async throws {
|
||||
|
||||
try await changeSubscription(subscriptionID: subscriptionID, addTagName: tagName)
|
||||
}
|
||||
|
||||
func createTagging(subscriptionID: String, tagName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
changeSubscription(subscriptionID: subscriptionID, addTagName: tagName, completion: completion)
|
||||
func deleteTagging(subscriptionID: String, tagName: String) async throws {
|
||||
|
||||
try await changeSubscription(subscriptionID: subscriptionID, removeTagName: tagName)
|
||||
}
|
||||
|
||||
func deleteTagging(subscriptionID: String, tagName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
changeSubscription(subscriptionID: subscriptionID, removeTagName: tagName, completion: completion)
|
||||
func moveSubscription(subscriptionID: String, sourceTag: String, destinationTag: String) async throws {
|
||||
|
||||
try await changeSubscription(subscriptionID: subscriptionID, removeTagName: sourceTag, addTagName: destinationTag)
|
||||
}
|
||||
|
||||
func moveSubscription(subscriptionID: String, fromTag: String, toTag: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
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) async throws {
|
||||
|
||||
guard removeTagName != nil || addTagName != nil || title != nil else {
|
||||
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
|
||||
return
|
||||
}
|
||||
|
||||
throw ReaderAPIAccountDelegateError.invalidParameter
|
||||
}
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
throw CredentialsError.incompleteCredentials
|
||||
return
|
||||
}
|
||||
|
||||
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
||||
switch result {
|
||||
case .success(let token):
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
request.httpMethod = "POST"
|
||||
|
||||
var postString = "T=\(token)&s=\(subscriptionID)&ac=edit"
|
||||
if let fromLabel = self.encodeForURLPath(removeTagName) {
|
||||
postString += "&r=user/-/label/\(fromLabel)"
|
||||
}
|
||||
if let toLabel = self.encodeForURLPath(addTagName) {
|
||||
postString += "&a=user/-/label/\(toLabel)"
|
||||
}
|
||||
if let encodedTitle = self.encodeForURLPath(title) {
|
||||
postString += "&t=\(encodedTitle)"
|
||||
}
|
||||
let postData = postString.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))
|
||||
}
|
||||
let token = try await requestAuthorizationToken(endpoint: baseURL)
|
||||
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
request.httpMethod = "POST"
|
||||
|
||||
var postString = "T=\(token)&s=\(subscriptionID)&ac=edit"
|
||||
if let fromLabel = self.encodeForURLPath(removeTagName) {
|
||||
postString += "&r=user/-/label/\(fromLabel)"
|
||||
}
|
||||
if let toLabel = self.encodeForURLPath(addTagName) {
|
||||
postString += "&a=user/-/label/\(toLabel)"
|
||||
}
|
||||
if let encodedTitle = self.encodeForURLPath(title) {
|
||||
postString += "&t=\(encodedTitle)"
|
||||
}
|
||||
let postData = postString.data(using: String.Encoding.utf8)
|
||||
|
||||
try await transport.send(request: request, method: HTTPMethod.post, payload: postData!)
|
||||
}
|
||||
|
||||
func retrieveEntries(articleIDs: [String], completion: @escaping (Result<([ReaderAPIEntry]?), Error>) -> Void) {
|
||||
|
||||
func retrieveEntries(articleIDs: [String]) async throws -> [ReaderAPIEntry]? {
|
||||
|
||||
guard !articleIDs.isEmpty else {
|
||||
completion(.success(([ReaderAPIEntry]())))
|
||||
return
|
||||
return [ReaderAPIEntry]()
|
||||
}
|
||||
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
}
|
||||
|
||||
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
||||
switch result {
|
||||
case .success(let token):
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.contents.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
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({ articleID -> String in
|
||||
if self.variant == .theOldReader {
|
||||
return "i=tag:google.com,2005:reader/item/\(articleID)"
|
||||
} else {
|
||||
let idValue = Int(articleID)!
|
||||
let idHexString = String(idValue, radix: 16, uppercase: false)
|
||||
return "i=tag:google.com,2005:reader/item/\(idHexString)"
|
||||
}
|
||||
}).joined(separator:"&")
|
||||
|
||||
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
|
||||
switch result {
|
||||
case .success(let (_, entryWrapper)):
|
||||
guard let entryWrapper = entryWrapper else {
|
||||
completion(.failure(ReaderAPIAccountDelegateError.invalidResponse))
|
||||
return
|
||||
}
|
||||
|
||||
completion(.success((entryWrapper.entries)))
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
let token = try await requestAuthorizationToken(endpoint: baseURL)
|
||||
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.contents.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
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({ articleID -> String in
|
||||
if self.variant == .theOldReader {
|
||||
return "i=tag:google.com,2005:reader/item/\(articleID)"
|
||||
} else {
|
||||
let idValue = Int(articleID)!
|
||||
let idHexString = String(idValue, radix: 16, uppercase: false)
|
||||
return "i=tag:google.com,2005:reader/item/\(idHexString)"
|
||||
}
|
||||
}).joined(separator:"&")
|
||||
|
||||
let postData = "T=\(token)&output=json&\(idsToFetch)".data(using: String.Encoding.utf8)
|
||||
|
||||
let (_, entryWrapper) = try await transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: ReaderAPIEntryWrapper.self)
|
||||
|
||||
guard let entryWrapper else {
|
||||
throw ReaderAPIAccountDelegateError.invalidResponse
|
||||
}
|
||||
|
||||
return entryWrapper.entries
|
||||
}
|
||||
|
||||
func retrieveItemIDs(type: ItemIDType, feedID: String? = nil, completion: @escaping ((Result<[String], Error>) -> Void)) {
|
||||
func retrieveItemIDs(type: ItemIDType, feedID: String? = nil) async throws -> [String] {
|
||||
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
|
||||
var queryItems = [
|
||||
URLQueryItem(name: "n", value: "1000"),
|
||||
URLQueryItem(name: "output", value: "json")
|
||||
]
|
||||
|
||||
|
||||
switch type {
|
||||
case .allForAccount:
|
||||
let since: Date = {
|
||||
@ -572,14 +429,13 @@ enum CreateReaderAPISubscriptionResult {
|
||||
return Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
let sinceTimeInterval = since.timeIntervalSince1970
|
||||
queryItems.append(URLQueryItem(name: "ot", value: String(Int(sinceTimeInterval))))
|
||||
queryItems.append(URLQueryItem(name: "s", value: ReaderStreams.readingList.rawValue))
|
||||
case .allForFeed:
|
||||
guard let feedID = feedID else {
|
||||
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
|
||||
return
|
||||
guard let feedID else {
|
||||
throw ReaderAPIAccountDelegateError.invalidParameter
|
||||
}
|
||||
let sinceTimeInterval = (Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()).timeIntervalSince1970
|
||||
queryItems.append(URLQueryItem(name: "ot", value: String(Int(sinceTimeInterval))))
|
||||
@ -590,48 +446,42 @@ enum CreateReaderAPISubscriptionResult {
|
||||
case .starred:
|
||||
queryItems.append(URLQueryItem(name: "s", value: ReaderState.starred.rawValue))
|
||||
}
|
||||
|
||||
|
||||
let url = baseURL
|
||||
.appendingPathComponent(ReaderAPIEndpoints.itemIds.rawValue)
|
||||
.appendingQueryItems(queryItems)
|
||||
|
||||
|
||||
guard let callURL = url else {
|
||||
completion(.failure(TransportError.noURL))
|
||||
return
|
||||
throw TransportError.noURL
|
||||
}
|
||||
|
||||
|
||||
var request: URLRequest = URLRequest(url: callURL, credentials: credentials)
|
||||
addVariantHeaders(&request)
|
||||
|
||||
self.transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self) { result in
|
||||
switch result {
|
||||
case .success(let (response, entries)):
|
||||
guard let entriesItemRefs = entries?.itemRefs, entriesItemRefs.count > 0 else {
|
||||
completion(.success([String]()))
|
||||
return
|
||||
}
|
||||
let dateInfo = HTTPDateInfo(urlResponse: response)
|
||||
let itemIDs = entriesItemRefs.compactMap { $0.itemId }
|
||||
self.retrieveItemIDs(type: type, url: callURL, dateInfo: dateInfo, itemIDs: itemIDs, continuation: entries?.continuation, completion: completion)
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
let (response, entries) = try await transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self)
|
||||
|
||||
guard let entriesItemRefs = entries?.itemRefs, entriesItemRefs.count > 0 else {
|
||||
return [String]()
|
||||
}
|
||||
|
||||
let dateInfo = HTTPDateInfo(urlResponse: response)
|
||||
let itemIDs = entriesItemRefs.compactMap { $0.itemId }
|
||||
|
||||
return try await retrieveItemIDs(type: type, url: callURL, dateInfo: dateInfo, itemIDs: itemIDs, continuation: entries?.continuation)
|
||||
}
|
||||
|
||||
func retrieveItemIDs(type: ItemIDType, url: URL, dateInfo: HTTPDateInfo?, itemIDs: [String], continuation: String?, completion: @escaping ((Result<[String], Error>) -> Void)) {
|
||||
guard let continuation = continuation else {
|
||||
func retrieveItemIDs(type: ItemIDType, url: URL, dateInfo: HTTPDateInfo?, itemIDs: [String], continuation: String?) async throws -> [String] {
|
||||
|
||||
guard let continuation else {
|
||||
if type == .allForAccount {
|
||||
self.accountMetadata?.lastArticleFetchStartTime = dateInfo?.date
|
||||
self.accountMetadata?.lastArticleFetchEndTime = Date()
|
||||
}
|
||||
completion(.success(itemIDs))
|
||||
return
|
||||
return itemIDs
|
||||
}
|
||||
|
||||
guard var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
||||
completion(.failure(ReaderAPIAccountDelegateError.invalidParameter))
|
||||
return
|
||||
throw ReaderAPIAccountDelegateError.invalidParameter
|
||||
}
|
||||
|
||||
var queryItems = urlComponents.queryItems!.filter({ $0.name != "c" })
|
||||
@ -639,45 +489,43 @@ enum CreateReaderAPISubscriptionResult {
|
||||
urlComponents.queryItems = queryItems
|
||||
|
||||
guard let callURL = urlComponents.url else {
|
||||
completion(.failure(TransportError.noURL))
|
||||
return
|
||||
throw TransportError.noURL
|
||||
}
|
||||
|
||||
var request: URLRequest = URLRequest(url: callURL, credentials: credentials)
|
||||
addVariantHeaders(&request)
|
||||
|
||||
self.transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self) { result in
|
||||
switch result {
|
||||
case .success(let (_, entries)):
|
||||
guard let entriesItemRefs = entries?.itemRefs, entriesItemRefs.count > 0 else {
|
||||
self.retrieveItemIDs(type: type, url: callURL, dateInfo: dateInfo, itemIDs: itemIDs, continuation: entries?.continuation, completion: completion)
|
||||
return
|
||||
}
|
||||
var totalItemIDs = itemIDs
|
||||
totalItemIDs.append(contentsOf: entriesItemRefs.compactMap { $0.itemId })
|
||||
self.retrieveItemIDs(type: type, url: callURL, dateInfo: dateInfo, itemIDs: totalItemIDs, continuation: entries?.continuation, completion: completion)
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
let (_, entries) = try await self.transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self)
|
||||
|
||||
guard let entriesItemRefs = entries?.itemRefs, entriesItemRefs.count > 0 else {
|
||||
return try await retrieveItemIDs(type: type, url: callURL, dateInfo: dateInfo, itemIDs: itemIDs, continuation: entries?.continuation)
|
||||
}
|
||||
|
||||
var totalItemIDs = itemIDs
|
||||
totalItemIDs.append(contentsOf: entriesItemRefs.compactMap { $0.itemId })
|
||||
|
||||
return try await retrieveItemIDs(type: type, url: callURL, dateInfo: dateInfo, itemIDs: totalItemIDs, continuation: entries?.continuation)
|
||||
}
|
||||
|
||||
func createUnreadEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
updateStateToEntries(entries: entries, state: .read, add: false, completion: completion)
|
||||
func createUnreadEntries(entries: [String]) async throws {
|
||||
|
||||
try await updateStateToEntries(entries: entries, state: .read, add: false)
|
||||
}
|
||||
|
||||
func deleteUnreadEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
updateStateToEntries(entries: entries, state: .read, add: true, completion: completion)
|
||||
}
|
||||
|
||||
func createStarredEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
updateStateToEntries(entries: entries, state: .starred, add: true, completion: completion)
|
||||
}
|
||||
|
||||
func deleteStarredEntries(entries: [String], completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
updateStateToEntries(entries: entries, state: .starred, add: false, completion: completion)
|
||||
}
|
||||
func deleteUnreadEntries(entries: [String]) async throws {
|
||||
|
||||
try await updateStateToEntries(entries: entries, state: .read, add: true)
|
||||
}
|
||||
|
||||
func createStarredEntries(entries: [String]) async throws {
|
||||
|
||||
try await updateStateToEntries(entries: entries, state: .starred, add: true)
|
||||
}
|
||||
|
||||
func deleteStarredEntries(entries: [String]) async throws {
|
||||
|
||||
try await updateStateToEntries(entries: entries, state: .starred, add: false)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Private
|
||||
@ -696,51 +544,35 @@ private extension ReaderAPICaller {
|
||||
}
|
||||
}
|
||||
|
||||
private func updateStateToEntries(entries: [String], state: ReaderState, add: Bool, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
private func updateStateToEntries(entries: [String], state: ReaderState, add: Bool) async throws {
|
||||
|
||||
guard let baseURL = apiBaseURL else {
|
||||
completion(.failure(CredentialsError.incompleteCredentials))
|
||||
return
|
||||
throw CredentialsError.incompleteCredentials
|
||||
}
|
||||
|
||||
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
|
||||
switch result {
|
||||
case .success(let token):
|
||||
// Do POST asking for data about all the new articles
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.editTag.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
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.compactMap({ idValue -> String? in
|
||||
if self.variant == .theOldReader {
|
||||
return "i=tag:google.com,2005:reader/item/\(idValue)"
|
||||
} else {
|
||||
guard let intValue = Int(idValue) else { return nil }
|
||||
let idHexString = String(format: "%.16llx", intValue)
|
||||
return "i=tag:google.com,2005:reader/item/\(idHexString)"
|
||||
}
|
||||
}).joined(separator:"&")
|
||||
|
||||
let actionIndicator = add ? "a" : "r"
|
||||
|
||||
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
|
||||
switch result {
|
||||
case .success:
|
||||
completion(.success(()))
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let token = try await requestAuthorizationToken(endpoint: baseURL)
|
||||
|
||||
// Do POST asking for data about all the new articles
|
||||
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.editTag.rawValue), credentials: self.credentials)
|
||||
self.addVariantHeaders(&request)
|
||||
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.compactMap({ idValue -> String? in
|
||||
if self.variant == .theOldReader {
|
||||
return "i=tag:google.com,2005:reader/item/\(idValue)"
|
||||
} else {
|
||||
guard let intValue = Int(idValue) else { return nil }
|
||||
let idHexString = String(format: "%.16llx", intValue)
|
||||
return "i=tag:google.com,2005:reader/item/\(idHexString)"
|
||||
}
|
||||
}).joined(separator:"&")
|
||||
|
||||
let actionIndicator = add ? "a" : "r"
|
||||
|
||||
let postData = "T=\(token)&\(idsToFetch)&\(actionIndicator)=\(state.rawValue)".data(using: String.Encoding.utf8)
|
||||
|
||||
try await transport.send(request: request, method: HTTPMethod.post, payload: postData!)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user