Rename feeds

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

View File

@ -48,7 +48,7 @@ final class GoogleReaderCompatibleAPICaller: NSObject {
case editTag = "/reader/api/0/edit-tag" case editTag = "/reader/api/0/edit-tag"
} }
private let GoogleReaderCompatibleBaseURL = URL(string: "https://api.GoogleReaderCompatible.com/v2/")! // private let GoogleReaderCompatibleBaseURL = URL(string: "https://api.GoogleReaderCompatible.com/v2/")!
private var transport: Transport! private var transport: Transport!
var credentials: Credentials? var credentials: Credentials?
@ -157,50 +157,50 @@ final class GoogleReaderCompatibleAPICaller: NSObject {
func importOPML(opmlData: Data, completion: @escaping (Result<GoogleReaderCompatibleImportResult, Error>) -> Void) { func importOPML(opmlData: Data, completion: @escaping (Result<GoogleReaderCompatibleImportResult, Error>) -> Void) {
let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("imports.json") // let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("imports.json")
var request = URLRequest(url: callURL, credentials: credentials) // var request = URLRequest(url: callURL, credentials: credentials)
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType) // request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType)
//
transport.send(request: request, method: HTTPMethod.post, payload: opmlData) { result in // transport.send(request: request, method: HTTPMethod.post, payload: opmlData) { result in
//
switch result { // switch result {
case .success(let (_, data)): // case .success(let (_, data)):
//
guard let resultData = data else { // guard let resultData = data else {
completion(.failure(TransportError.noData)) // completion(.failure(TransportError.noData))
break // break
} // }
//
do { // do {
let result = try JSONDecoder().decode(GoogleReaderCompatibleImportResult.self, from: resultData) // let result = try JSONDecoder().decode(GoogleReaderCompatibleImportResult.self, from: resultData)
completion(.success(result)) // completion(.success(result))
} catch { // } catch {
completion(.failure(error)) // completion(.failure(error))
} // }
//
case .failure(let error): // case .failure(let error):
completion(.failure(error)) // completion(.failure(error))
} // }
//
} // }
} }
func retrieveOPMLImportResult(importID: Int, completion: @escaping (Result<GoogleReaderCompatibleImportResult?, Error>) -> Void) { func retrieveOPMLImportResult(importID: Int, completion: @escaping (Result<GoogleReaderCompatibleImportResult?, Error>) -> Void) {
let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("imports/\(importID).json") // let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("imports/\(importID).json")
let request = URLRequest(url: callURL, credentials: credentials) // let request = URLRequest(url: callURL, credentials: credentials)
//
transport.send(request: request, resultType: GoogleReaderCompatibleImportResult.self) { result in // transport.send(request: request, resultType: GoogleReaderCompatibleImportResult.self) { result in
//
switch result { // switch result {
case .success(let (_, importResult)): // case .success(let (_, importResult)):
completion(.success(importResult)) // completion(.success(importResult))
case .failure(let error): // case .failure(let error):
completion(.failure(error)) // completion(.failure(error))
} // }
//
} // }
} }
@ -243,28 +243,28 @@ final class GoogleReaderCompatibleAPICaller: NSObject {
} }
func renameTag(oldName: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) { func renameTag(oldName: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("tags.json") // let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("tags.json")
let request = URLRequest(url: callURL, credentials: credentials) // let request = URLRequest(url: callURL, credentials: credentials)
let payload = GoogleReaderCompatibleRenameTag(oldName: oldName, newName: newName) // let payload = GoogleReaderCompatibleRenameTag(oldName: oldName, newName: newName)
transport.send(request: request, method: HTTPMethod.post, payload: payload, completion: completion) // transport.send(request: request, method: HTTPMethod.post, payload: payload, completion: completion)
} }
func deleteTag(name: String, completion: @escaping (Result<[GoogleReaderCompatibleTagging]?, Error>) -> Void) { func deleteTag(name: String, completion: @escaping (Result<[GoogleReaderCompatibleTagging]?, Error>) -> Void) {
let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("tags.json") // let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("tags.json")
let request = URLRequest(url: callURL, credentials: credentials) // let request = URLRequest(url: callURL, credentials: credentials)
let payload = GoogleReaderCompatibleDeleteTag(name: name) // let payload = GoogleReaderCompatibleDeleteTag(name: name)
//
transport.send(request: request, method: HTTPMethod.delete, payload: payload, resultType: [GoogleReaderCompatibleTagging].self) { result in // transport.send(request: request, method: HTTPMethod.delete, payload: payload, resultType: [GoogleReaderCompatibleTagging].self) { result in
//
switch result { // switch result {
case .success(let (_, taggings)): // case .success(let (_, taggings)):
completion(.success(taggings)) // completion(.success(taggings))
case .failure(let error): // case .failure(let error):
completion(.failure(error)) // completion(.failure(error))
} // }
//
} // }
} }
@ -388,77 +388,134 @@ final class GoogleReaderCompatibleAPICaller: NSObject {
} }
func renameSubscription(subscriptionID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) { func renameSubscription(subscriptionID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("subscriptions/\(subscriptionID)/update.json") guard let baseURL = APIBaseURL else {
let request = URLRequest(url: callURL, credentials: credentials) completion(.failure(CredentialsError.incompleteCredentials))
let payload = GoogleReaderCompatibleUpdateSubscription(title: newName) return
transport.send(request: request, method: HTTPMethod.post, payload: payload, completion: completion) }
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
switch result {
case .success(let token):
var request = URLRequest(url: baseURL.appendingPathComponent(GoogleReaderEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
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))
}
}
} }
func deleteSubscription(subscriptionID: String, completion: @escaping (Result<Void, Error>) -> Void) { func deleteSubscription(subscriptionID: String, completion: @escaping (Result<Void, Error>) -> Void) {
let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("subscriptions/\(subscriptionID).json") guard let baseURL = APIBaseURL else {
let request = URLRequest(url: callURL, credentials: credentials) completion(.failure(CredentialsError.incompleteCredentials))
transport.send(request: request, method: HTTPMethod.delete, completion: completion) return
}
self.requestAuthorizationToken(endpoint: baseURL) { (result) in
switch result {
case .success(let token):
var request = URLRequest(url: baseURL.appendingPathComponent(GoogleReaderEndpoints.subscriptionEdit.rawValue), credentials: self.credentials)
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))
}
}
} }
func createTagging(feedID: Int, name: String, completion: @escaping (Result<Int, Error>) -> Void) { func createTagging(feedID: Int, name: String, completion: @escaping (Result<Int, Error>) -> Void) {
let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("taggings.json") // let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("taggings.json")
var request = URLRequest(url: callURL, credentials: credentials) // var request = URLRequest(url: callURL, credentials: credentials)
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType) // request.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType)
//
let payload: Data // let payload: Data
do { // do {
payload = try JSONEncoder().encode(GoogleReaderCompatibleCreateTagging(feedID: feedID, name: name)) // payload = try JSONEncoder().encode(GoogleReaderCompatibleCreateTagging(feedID: feedID, name: name))
} catch { // } catch {
completion(.failure(error)) // completion(.failure(error))
return // return
} // }
//
transport.send(request: request, method: HTTPMethod.post, payload:payload) { result in // transport.send(request: request, method: HTTPMethod.post, payload:payload) { result in
//
switch result { // switch result {
case .success(let (response, _)): // case .success(let (response, _)):
if let taggingLocation = response.valueForHTTPHeaderField(HTTPResponseHeader.location), // if let taggingLocation = response.valueForHTTPHeaderField(HTTPResponseHeader.location),
let lowerBound = taggingLocation.range(of: "v2/taggings/")?.upperBound, // let lowerBound = taggingLocation.range(of: "v2/taggings/")?.upperBound,
let upperBound = taggingLocation.range(of: ".json")?.lowerBound, // let upperBound = taggingLocation.range(of: ".json")?.lowerBound,
let taggingID = Int(taggingLocation[lowerBound..<upperBound]) { // let taggingID = Int(taggingLocation[lowerBound..<upperBound]) {
completion(.success(taggingID)) // completion(.success(taggingID))
} else { // } else {
completion(.failure(TransportError.noData)) // completion(.failure(TransportError.noData))
} // }
case .failure(let error): // case .failure(let error):
completion(.failure(error)) // completion(.failure(error))
} // }
//
} // }
} }
func deleteTagging(taggingID: String, completion: @escaping (Result<Void, Error>) -> Void) { func deleteTagging(taggingID: String, completion: @escaping (Result<Void, Error>) -> Void) {
let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("taggings/\(taggingID).json") // let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("taggings/\(taggingID).json")
var request = URLRequest(url: callURL, credentials: credentials) // var request = URLRequest(url: callURL, credentials: credentials)
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType) // request.addValue("application/json; charset=utf-8", forHTTPHeaderField: HTTPRequestHeader.contentType)
transport.send(request: request, method: HTTPMethod.delete, completion: completion) // transport.send(request: request, method: HTTPMethod.delete, completion: completion)
} }
func retrieveIcons(completion: @escaping (Result<[GoogleReaderCompatibleIcon]?, Error>) -> Void) { func retrieveIcons(completion: @escaping (Result<[GoogleReaderCompatibleIcon]?, Error>) -> Void) {
let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("icons.json") // let callURL = GoogleReaderCompatibleBaseURL.appendingPathComponent("icons.json")
let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.icons] // let conditionalGet = accountMetadata?.conditionalGetInfo[ConditionalGetKeys.icons]
let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet) // let request = URLRequest(url: callURL, credentials: credentials, conditionalGet: conditionalGet)
//
transport.send(request: request, resultType: [GoogleReaderCompatibleIcon].self) { result in // transport.send(request: request, resultType: [GoogleReaderCompatibleIcon].self) { result in
//
switch result { // switch result {
case .success(let (response, icons)): // case .success(let (response, icons)):
self.storeConditionalGet(key: ConditionalGetKeys.icons, headers: response.allHeaderFields) // self.storeConditionalGet(key: ConditionalGetKeys.icons, headers: response.allHeaderFields)
completion(.success(icons)) // completion(.success(icons))
case .failure(let error): // case .failure(let error):
completion(.failure(error)) // completion(.failure(error))
} // }
//
} // }
//
} }
func retrieveEntries(articleIDs: [String], completion: @escaping (Result<([GoogleReaderCompatibleEntry]?), Error>) -> Void) { func retrieveEntries(articleIDs: [String], completion: @escaping (Result<([GoogleReaderCompatibleEntry]?), Error>) -> Void) {