cleanup code

most network calls follow the same structure
This commit is contained in:
Jonathan Bennett 2019-11-20 11:53:39 -05:00
parent e867991ec8
commit ac33bf982e
1 changed files with 27 additions and 79 deletions

View File

@ -29,11 +29,10 @@ final class FeedWranglerAPICaller: NSObject {
} }
func validateCredentials(completion: @escaping (Result<Credentials?, Error>) -> Void) { func validateCredentials(completion: @escaping (Result<Credentials?, Error>) -> Void) {
let callURL = FeedWranglerConfig.clientURL.appendingPathComponent("users/authorize") let url = FeedWranglerConfig.clientURL.appendingPathComponent("users/authorize")
let request = URLRequest(url: callURL, credentials: credentials)
let username = self.credentials?.username ?? "" let username = self.credentials?.username ?? ""
transport.send(request: request, resultType: FeedWranglerAuthorizationResult.self) { result in standardSend(url: url, resultType: FeedWranglerAuthorizationResult.self) { result in
switch result { switch result {
case .success(let (_, results)): case .success(let (_, results)):
if let accessToken = results?.accessToken { if let accessToken = results?.accessToken {
@ -50,9 +49,8 @@ final class FeedWranglerAPICaller: NSObject {
func retrieveSubscriptions(completion: @escaping (Result<[FeedWranglerSubscription], Error>) -> Void) { func retrieveSubscriptions(completion: @escaping (Result<[FeedWranglerSubscription], Error>) -> Void) {
let url = FeedWranglerConfig.clientURL.appendingPathComponent("subscriptions/list") let url = FeedWranglerConfig.clientURL.appendingPathComponent("subscriptions/list")
let request = URLRequest(url: url, credentials: credentials)
transport.send(request: request, resultType: FeedWranglerSubscriptionsRequest.self) { result in standardSend(url: url, resultType: FeedWranglerSubscriptionsRequest.self) { result in
switch result { switch result {
case .success(let (_, results)): case .success(let (_, results)):
completion(.success(results?.feeds ?? [])) completion(.success(results?.feeds ?? []))
@ -64,17 +62,12 @@ final class FeedWranglerAPICaller: NSObject {
} }
func addSubscription(url: String, completion: @escaping (Result<Void, Error>) -> Void) { func addSubscription(url: String, completion: @escaping (Result<Void, Error>) -> Void) {
guard let callURL = FeedWranglerConfig let url = FeedWranglerConfig
.clientURL .clientURL
.appendingPathComponent("subscriptions/add_feed") .appendingPathComponent("subscriptions/add_feed")
.appendingQueryItem(URLQueryItem(name: "feed_url", value: url)) else { .appendingQueryItem(URLQueryItem(name: "feed_url", value: url))
completion(.failure(TransportError.noURL))
return
}
let request = URLRequest(url: callURL, credentials: credentials) standardSend(url: url, resultType: FeedWranglerGenericResult.self) { result in
transport.send(request: request, resultType: FeedWranglerGenericResult.self) { result in
switch result { switch result {
case .success(let (_, results)): case .success(let (_, results)):
if let error = results?.error { if let error = results?.error {
@ -90,7 +83,6 @@ final class FeedWranglerAPICaller: NSObject {
} }
func renameSubscription(feedID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) { func renameSubscription(feedID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
let url = FeedWranglerConfig.clientURL let url = FeedWranglerConfig.clientURL
.appendingPathComponent("subscriptions/rename_feed") .appendingPathComponent("subscriptions/rename_feed")
.appendingQueryItems([ .appendingQueryItems([
@ -98,14 +90,7 @@ final class FeedWranglerAPICaller: NSObject {
URLQueryItem(name: "feed_name", value: newName), URLQueryItem(name: "feed_name", value: newName),
]) ])
guard let callURL = url else { standardSend(url: url, resultType: FeedWranglerSubscriptionsRequest.self) { result in
completion(.failure(TransportError.noURL))
return
}
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: FeedWranglerSubscriptionsRequest.self) { result in
switch result { switch result {
case .success: case .success:
completion(.success(())) completion(.success(()))
@ -117,19 +102,11 @@ final class FeedWranglerAPICaller: NSObject {
} }
func removeSubscription(feedID: String, completion: @escaping (Result<Void, Error>) -> Void) { func removeSubscription(feedID: String, completion: @escaping (Result<Void, Error>) -> Void) {
let url = FeedWranglerConfig.clientURL let url = FeedWranglerConfig.clientURL
.appendingPathComponent("subscriptions/remove_feed") .appendingPathComponent("subscriptions/remove_feed")
.appendingQueryItem(URLQueryItem(name: "feed_id", value: feedID)) .appendingQueryItem(URLQueryItem(name: "feed_id", value: feedID))
guard let callURL = url else { standardSend(url: url, resultType: FeedWranglerGenericResult.self) { result in
completion(.failure(TransportError.noURL))
return
}
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: FeedWranglerGenericResult.self) { result in
switch result { switch result {
case .success: case .success:
completion(.success(())) completion(.success(()))
@ -146,16 +123,8 @@ final class FeedWranglerAPICaller: NSObject {
let url = FeedWranglerConfig.clientURL let url = FeedWranglerConfig.clientURL
.appendingPathComponent("feed_items/get") .appendingPathComponent("feed_items/get")
.appendingQueryItem(URLQueryItem(name: "feed_item_ids", value: IDs)) .appendingQueryItem(URLQueryItem(name: "feed_item_ids", value: IDs))
print("\(url!)")
guard let callURL = url else { standardSend(url: url, resultType: FeedWranglerFeedItemsRequest.self) { result in
completion(.failure(TransportError.noURL))
return
}
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: FeedWranglerFeedItemsRequest.self) { result in
switch result { switch result {
case .success(let (_, results)): case .success(let (_, results)):
completion(.success(results?.feedItems ?? [])) completion(.success(results?.feedItems ?? []))
@ -168,7 +137,6 @@ final class FeedWranglerAPICaller: NSObject {
} }
func retrieveFeedItems(page: Int = 0, completion: @escaping (Result<[FeedWranglerFeedItem], Error>) -> Void) { func retrieveFeedItems(page: Int = 0, completion: @escaping (Result<[FeedWranglerFeedItem], Error>) -> Void) {
// todo: handle initial sync better // todo: handle initial sync better
let url = FeedWranglerConfig.clientURL let url = FeedWranglerConfig.clientURL
.appendingPathComponent("feed_items/list") .appendingPathComponent("feed_items/list")
@ -177,14 +145,7 @@ final class FeedWranglerAPICaller: NSObject {
URLQueryItem(name: "offset", value: String(page * FeedWranglerConfig.pageSize)), URLQueryItem(name: "offset", value: String(page * FeedWranglerConfig.pageSize)),
]) ])
guard let callURL = url else { standardSend(url: url, resultType: FeedWranglerFeedItemsRequest.self) { result in
completion(.failure(TransportError.noURL))
return
}
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: FeedWranglerFeedItemsRequest.self) { result in
switch result { switch result {
case .success(let (_, results)): case .success(let (_, results)):
completion(.success(results?.feedItems ?? [])) completion(.success(results?.feedItems ?? []))
@ -196,7 +157,6 @@ final class FeedWranglerAPICaller: NSObject {
} }
func retrieveUnreadFeedItems(page: Int = 0, completion: @escaping (Result<[FeedWranglerFeedItem], Error>) -> Void) { func retrieveUnreadFeedItems(page: Int = 0, completion: @escaping (Result<[FeedWranglerFeedItem], Error>) -> Void) {
let url = FeedWranglerConfig.clientURL let url = FeedWranglerConfig.clientURL
.appendingPathComponent("feed_items/list") .appendingPathComponent("feed_items/list")
.appendingQueryItems([ .appendingQueryItems([
@ -204,14 +164,7 @@ final class FeedWranglerAPICaller: NSObject {
URLQueryItem(name: "offset", value: String(page * FeedWranglerConfig.pageSize)), URLQueryItem(name: "offset", value: String(page * FeedWranglerConfig.pageSize)),
]) ])
guard let callURL = url else { standardSend(url: url, resultType: FeedWranglerFeedItemsRequest.self) { result in
completion(.failure(TransportError.noURL))
return
}
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: FeedWranglerFeedItemsRequest.self) { result in
switch result { switch result {
case .success(let (_, results)): case .success(let (_, results)):
completion(.success(results?.feedItems ?? [])) completion(.success(results?.feedItems ?? []))
@ -239,7 +192,6 @@ final class FeedWranglerAPICaller: NSObject {
} }
func retrieveStarredFeedItems(page: Int = 0, completion: @escaping (Result<[FeedWranglerFeedItem], Error>) -> Void) { func retrieveStarredFeedItems(page: Int = 0, completion: @escaping (Result<[FeedWranglerFeedItem], Error>) -> Void) {
let url = FeedWranglerConfig.clientURL let url = FeedWranglerConfig.clientURL
.appendingPathComponent("feed_items/list") .appendingPathComponent("feed_items/list")
.appendingQueryItems([ .appendingQueryItems([
@ -247,14 +199,7 @@ final class FeedWranglerAPICaller: NSObject {
URLQueryItem(name: "offset", value: String(page * FeedWranglerConfig.pageSize)), URLQueryItem(name: "offset", value: String(page * FeedWranglerConfig.pageSize)),
]) ])
guard let callURL = url else { standardSend(url: url, resultType: FeedWranglerFeedItemsRequest.self) { result in
completion(.failure(TransportError.noURL))
return
}
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: FeedWranglerFeedItemsRequest.self) { result in
switch result { switch result {
case .success(let (_, results)): case .success(let (_, results)):
completion(.success(results?.feedItems ?? [])) completion(.success(results?.feedItems ?? []))
@ -300,16 +245,19 @@ final class FeedWranglerAPICaller: NSObject {
.appendingPathComponent("feed_items/update") .appendingPathComponent("feed_items/update")
.appendingQueryItems(queryItems) .appendingQueryItems(queryItems)
guard let callURL = url else { standardSend(url: url, resultType: FeedWranglerGenericResult.self) { result in
completion()
return
}
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: FeedWranglerGenericResult.self) { result in
completion() completion()
} }
} }
private func standardSend<R: Decodable>(url: URL?, resultType: R.Type, completion: @escaping (Result<(HTTPURLResponse, R?), Error>) -> Void) {
guard let callURL = url else {
completion(.failure(TransportError.noURL))
return
}
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: resultType, completion: completion)
}
} }