Subscribing and loading initial articles functioning.

This commit is contained in:
Jeremy Beker 2019-06-15 15:46:13 -04:00
parent b2bc941d2d
commit 62afd312f0
No known key found for this signature in database
GPG Key ID: CD5EE767A4A34FD0
1 changed files with 45 additions and 13 deletions

View File

@ -491,16 +491,12 @@ final class GoogleReaderCompatibleAPICaller: NSObject {
self.transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: GoogleReaderCompatibleEntryWrapper.self, completion: { (result) in self.transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: GoogleReaderCompatibleEntryWrapper.self, completion: { (result) in
switch result { switch result {
case .success(let (response, entryWrapper)): case .success(let (_, entryWrapper)):
guard let entryWrapper = entryWrapper else { guard let entryWrapper = entryWrapper else {
completion(.failure(GoogleReaderCompatibleAccountDelegateError.invalidResponse)) completion(.failure(GoogleReaderCompatibleAccountDelegateError.invalidResponse))
return return
} }
let dateInfo = HTTPDateInfo(urlResponse: response)
self.accountMetadata?.lastArticleFetch = dateInfo?.date
completion(.success((entryWrapper.entries))) completion(.success((entryWrapper.entries)))
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
@ -518,19 +514,55 @@ final class GoogleReaderCompatibleAPICaller: NSObject {
func retrieveEntries(feedID: String, completion: @escaping (Result<([GoogleReaderCompatibleEntry]?, String?), Error>) -> Void) { func retrieveEntries(feedID: String, completion: @escaping (Result<([GoogleReaderCompatibleEntry]?, String?), Error>) -> Void) {
let since = Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date() let since = Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()
let sinceString = GoogleReaderCompatibleDate.formatter.string(from: since)
var callComponents = URLComponents(url: GoogleReaderCompatibleBaseURL.appendingPathComponent("feeds/\(feedID)/entries.json"), resolvingAgainstBaseURL: false)! guard let baseURL = APIBaseURL else {
callComponents.queryItems = [URLQueryItem(name: "since", value: sinceString), URLQueryItem(name: "per_page", value: "100"), URLQueryItem(name: "mode", value: "extended")] completion(.failure(CredentialsError.incompleteCredentials))
let request = URLRequest(url: callComponents.url!, credentials: credentials) return
}
transport.send(request: request, resultType: [GoogleReaderCompatibleEntry].self) { result in // Add query string for getting JSON (probably should break this out as I will be doing it a lot)
guard var components = URLComponents(url: baseURL.appendingPathComponent(GoogleReaderEndpoints.itemIds.rawValue), resolvingAgainstBaseURL: false) else {
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)
transport.send(request: request, resultType: GoogleReaderCompatibleReferenceWrapper.self) { result in
switch result { switch result {
case .success(let (response, entries)): case .success(let (_, unreadEntries)):
let pagingInfo = HTTPLinkPagingInfo(urlResponse: response) guard let itemRefs = unreadEntries?.itemRefs else {
completion(.success((entries, pagingInfo.nextPage))) 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))
}
}
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))