Use Feed Finder before attempting to add feed urls

This commit is contained in:
Jeremy Beker 2019-07-13 08:37:08 -04:00
parent 4edd38d11a
commit fe9129ac4a
No known key found for this signature in database
GPG Key ID: CD5EE767A4A34FD0
1 changed files with 81 additions and 58 deletions

View File

@ -321,79 +321,102 @@ final class ReaderAPICaller: NSObject {
return return
} }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in guard let url = URL(string: url) else {
completion(.failure(LocalAccountDelegateError.invalidParameter))
return
}
FeedFinder.find(url: url) { result in
switch result { switch result {
case .success(let token): case .success(let feedSpecifiers):
guard var components = URLComponents(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionAdd.rawValue), resolvingAgainstBaseURL: false) else { guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers),
completion(.failure(TransportError.noURL)) let url = URL(string: bestFeedSpecifier.urlString) else {
return completion(.failure(AccountError.createErrorNotFound))
return
} }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
components.queryItems = [
URLQueryItem(name: "quickadd", value: url)
]
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 { switch result {
case .success(let (_, subResult)): case .success(let token):
guard var components = URLComponents(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionAdd.rawValue), resolvingAgainstBaseURL: false) else {
completion(.failure(TransportError.noURL))
return
}
switch subResult?.numResults { components.queryItems = [
case 0: URLQueryItem(name: "quickadd", value: url.absoluteString)
completion(.success(.alreadySubscribed)) ]
default:
// We have a feed ID but need to get feed information
guard let streamId = subResult?.streamId else {
completion(.failure(AccountError.createErrorNotFound))
return
}
// There is no call to get a single subscription entry, so we get them all, guard let callURL = components.url else {
// look up the one we just subscribed to and return that completion(.failure(TransportError.noURL))
self.retrieveSubscriptions(completion: { (result) in return
switch result { }
case .success(let subscriptions):
guard let subscriptions = subscriptions else { 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 {
completion(.failure(AccountError.createErrorNotFound)) completion(.failure(AccountError.createErrorNotFound))
return return
} }
let newStreamId = "feed/\(streamId)" // 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
}
guard let subscription = subscriptions.first(where: { (sub) -> Bool in let newStreamId = "feed/\(streamId)"
sub.feedID == newStreamId
}) else { guard let subscription = subscriptions.first(where: { (sub) -> Bool in
completion(.failure(AccountError.createErrorNotFound)) sub.feedID == newStreamId
return }) else {
completion(.failure(AccountError.createErrorNotFound))
return
}
completion(.success(.created(subscription)))
case .failure(let error):
completion(.failure(error))
}
})
} }
case .failure(let error):
completion(.success(.created(subscription))) completion(.failure(error))
case .failure(let error):
completion(.failure(error))
}
})
} }
})
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
}) }
case .failure(let error):
completion(.failure(error)) case .failure:
completion(.failure(AccountError.createErrorNotFound))
} }
} }
//
} }
func renameSubscription(subscriptionID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) { func renameSubscription(subscriptionID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {