Created reusable fetch method instead of coding a new one for each Reddit fetch
This commit is contained in:
parent
baf970745e
commit
cd4b3260b7
@ -46,6 +46,7 @@
|
|||||||
5133230C2281088A00C30F19 /* subscriptions_add.json in Resources */ = {isa = PBXBuildFile; fileRef = 5133230B2281088A00C30F19 /* subscriptions_add.json */; };
|
5133230C2281088A00C30F19 /* subscriptions_add.json in Resources */ = {isa = PBXBuildFile; fileRef = 5133230B2281088A00C30F19 /* subscriptions_add.json */; };
|
||||||
5133BB47245FD8140001E3D0 /* RedditLinkListing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5133BB46245FD8140001E3D0 /* RedditLinkListing.swift */; };
|
5133BB47245FD8140001E3D0 /* RedditLinkListing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5133BB46245FD8140001E3D0 /* RedditLinkListing.swift */; };
|
||||||
5133BB49246078910001E3D0 /* RedditLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5133BB48246078910001E3D0 /* RedditLink.swift */; };
|
5133BB49246078910001E3D0 /* RedditLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5133BB48246078910001E3D0 /* RedditLink.swift */; };
|
||||||
|
5133BB4B2460BDF30001E3D0 /* RedditSubreddit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5133BB4A2460BDF30001E3D0 /* RedditSubreddit.swift */; };
|
||||||
5139A6382459822D004D960C /* CloudKitArticleStatusUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5139A6372459822D004D960C /* CloudKitArticleStatusUpdate.swift */; };
|
5139A6382459822D004D960C /* CloudKitArticleStatusUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5139A6372459822D004D960C /* CloudKitArticleStatusUpdate.swift */; };
|
||||||
5144EA49227B497600D19003 /* FeedbinAPICaller.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5144EA48227B497600D19003 /* FeedbinAPICaller.swift */; };
|
5144EA49227B497600D19003 /* FeedbinAPICaller.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5144EA48227B497600D19003 /* FeedbinAPICaller.swift */; };
|
||||||
5144EA4E227B829A00D19003 /* FeedbinAccountDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5144EA4D227B829A00D19003 /* FeedbinAccountDelegate.swift */; };
|
5144EA4E227B829A00D19003 /* FeedbinAccountDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5144EA4D227B829A00D19003 /* FeedbinAccountDelegate.swift */; };
|
||||||
@ -304,6 +305,7 @@
|
|||||||
5133230B2281088A00C30F19 /* subscriptions_add.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = subscriptions_add.json; sourceTree = "<group>"; };
|
5133230B2281088A00C30F19 /* subscriptions_add.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = subscriptions_add.json; sourceTree = "<group>"; };
|
||||||
5133BB46245FD8140001E3D0 /* RedditLinkListing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedditLinkListing.swift; sourceTree = "<group>"; };
|
5133BB46245FD8140001E3D0 /* RedditLinkListing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedditLinkListing.swift; sourceTree = "<group>"; };
|
||||||
5133BB48246078910001E3D0 /* RedditLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedditLink.swift; sourceTree = "<group>"; };
|
5133BB48246078910001E3D0 /* RedditLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedditLink.swift; sourceTree = "<group>"; };
|
||||||
|
5133BB4A2460BDF30001E3D0 /* RedditSubreddit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedditSubreddit.swift; sourceTree = "<group>"; };
|
||||||
5139A6372459822D004D960C /* CloudKitArticleStatusUpdate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudKitArticleStatusUpdate.swift; sourceTree = "<group>"; };
|
5139A6372459822D004D960C /* CloudKitArticleStatusUpdate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudKitArticleStatusUpdate.swift; sourceTree = "<group>"; };
|
||||||
5144EA48227B497600D19003 /* FeedbinAPICaller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinAPICaller.swift; sourceTree = "<group>"; };
|
5144EA48227B497600D19003 /* FeedbinAPICaller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinAPICaller.swift; sourceTree = "<group>"; };
|
||||||
5144EA4D227B829A00D19003 /* FeedbinAccountDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinAccountDelegate.swift; sourceTree = "<group>"; };
|
5144EA4D227B829A00D19003 /* FeedbinAccountDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinAccountDelegate.swift; sourceTree = "<group>"; };
|
||||||
@ -639,6 +641,7 @@
|
|||||||
5193CD53245E3F7A0092735E /* RedditFeedProvider.swift */,
|
5193CD53245E3F7A0092735E /* RedditFeedProvider.swift */,
|
||||||
5133BB48246078910001E3D0 /* RedditLink.swift */,
|
5133BB48246078910001E3D0 /* RedditLink.swift */,
|
||||||
5133BB46245FD8140001E3D0 /* RedditLinkListing.swift */,
|
5133BB46245FD8140001E3D0 /* RedditLinkListing.swift */,
|
||||||
|
5133BB4A2460BDF30001E3D0 /* RedditSubreddit.swift */,
|
||||||
5193CD80245F295E0092735E /* RedditUser.swift */,
|
5193CD80245F295E0092735E /* RedditUser.swift */,
|
||||||
);
|
);
|
||||||
path = Reddit;
|
path = Reddit;
|
||||||
@ -1246,6 +1249,7 @@
|
|||||||
51B3630F244B6CB9000DEF2A /* TwitterExtendedEntities.swift in Sources */,
|
51B3630F244B6CB9000DEF2A /* TwitterExtendedEntities.swift in Sources */,
|
||||||
84B99C9F1FAE8D3200ECDEDB /* ContainerPath.swift in Sources */,
|
84B99C9F1FAE8D3200ECDEDB /* ContainerPath.swift in Sources */,
|
||||||
51BC8FCC237EC055004F8B56 /* Feed.swift in Sources */,
|
51BC8FCC237EC055004F8B56 /* Feed.swift in Sources */,
|
||||||
|
5133BB4B2460BDF30001E3D0 /* RedditSubreddit.swift in Sources */,
|
||||||
846E77501F6EF9C400A165E2 /* LocalAccountRefresher.swift in Sources */,
|
846E77501F6EF9C400A165E2 /* LocalAccountRefresher.swift in Sources */,
|
||||||
9EA643CF2391D3560018A28C /* FeedlyAddExistingFeedOperation.swift in Sources */,
|
9EA643CF2391D3560018A28C /* FeedlyAddExistingFeedOperation.swift in Sources */,
|
||||||
55203300229D5D5A009559E0 /* ReaderAPICaller.swift in Sources */,
|
55203300229D5D5A009559E0 /* ReaderAPICaller.swift in Sources */,
|
||||||
|
@ -106,8 +106,8 @@ public final class RedditFeedProvider: FeedProvider {
|
|||||||
completion(.failure(TwitterFeedProviderError.unknown))
|
completion(.failure(TwitterFeedProviderError.unknown))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let api = urlComponents.path
|
let api = "\(urlComponents.path).json"
|
||||||
retrieveListing(api: api, parameters: [:]) { result in
|
fetch(api: api, parameters: [:], resultType: RedditLinkListing.self) { result in
|
||||||
switch result {
|
switch result {
|
||||||
case .success(let linkListing):
|
case .success(let linkListing):
|
||||||
let parsedItems = self.makeParsedItems(webFeed.url, linkListing)
|
let parsedItems = self.makeParsedItems(webFeed.url, linkListing)
|
||||||
@ -124,7 +124,7 @@ public final class RedditFeedProvider: FeedProvider {
|
|||||||
let oauthRefreshToken = tokenSuccess.credential.oauthRefreshToken
|
let oauthRefreshToken = tokenSuccess.credential.oauthRefreshToken
|
||||||
let redditFeedProvider = RedditFeedProvider(oauthToken: oauthToken, oauthRefreshToken: oauthRefreshToken)
|
let redditFeedProvider = RedditFeedProvider(oauthToken: oauthToken, oauthRefreshToken: oauthRefreshToken)
|
||||||
|
|
||||||
redditFeedProvider.retrieveUser(api: "/api/v1/me") { result in
|
redditFeedProvider.fetch(api: "/api/v1/me", resultType: RedditUser.self) { result in
|
||||||
switch result {
|
switch result {
|
||||||
case .success(let user):
|
case .success(let user):
|
||||||
guard let username = user.name else {
|
guard let username = user.name else {
|
||||||
@ -185,7 +185,7 @@ extension RedditFeedProvider: OAuth2SwiftProvider {
|
|||||||
|
|
||||||
private extension RedditFeedProvider {
|
private extension RedditFeedProvider {
|
||||||
|
|
||||||
func retrieveUser(api: String, completion: @escaping (Result<RedditUser, Error>) -> Void) {
|
func fetch<R: Decodable>(api: String, parameters: [String: Any] = [:], resultType: R.Type, completion: @escaping (Result<R, Error>) -> Void) {
|
||||||
guard let client = client else {
|
guard let client = client else {
|
||||||
completion(.failure(RedditFeedProviderError.unknown))
|
completion(.failure(RedditFeedProviderError.unknown))
|
||||||
return
|
return
|
||||||
@ -193,28 +193,6 @@ private extension RedditFeedProvider {
|
|||||||
|
|
||||||
let url = "\(Self.apiBase)\(api)"
|
let url = "\(Self.apiBase)\(api)"
|
||||||
|
|
||||||
client.request(url, method: .GET, headers: Self.userAgentHeaders) { result in
|
|
||||||
switch result {
|
|
||||||
case .success(let response):
|
|
||||||
if let redditUser = try? JSONDecoder().decode(RedditUser.self, from: response.data) {
|
|
||||||
completion(.success(redditUser))
|
|
||||||
} else {
|
|
||||||
completion(.failure(RedditFeedProviderError.unknown))
|
|
||||||
}
|
|
||||||
case .failure(let error):
|
|
||||||
completion(.failure(error))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func retrieveListing(api: String, parameters: [String: Any], completion: @escaping (Result<RedditLinkListing, Error>) -> Void) {
|
|
||||||
guard let client = client else {
|
|
||||||
completion(.failure(RedditFeedProviderError.unknown))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let url = "\(Self.apiBase)\(api).json"
|
|
||||||
|
|
||||||
var expandedParameters = parameters
|
var expandedParameters = parameters
|
||||||
expandedParameters["raw_json"] = "1"
|
expandedParameters["raw_json"] = "1"
|
||||||
|
|
||||||
@ -230,8 +208,8 @@ private extension RedditFeedProvider {
|
|||||||
let decoder = JSONDecoder()
|
let decoder = JSONDecoder()
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let listing = try decoder.decode(RedditLinkListing.self, from: response.data)
|
let result = try decoder.decode(resultType, from: response.data)
|
||||||
completion(.success(listing))
|
completion(.success(result))
|
||||||
} catch {
|
} catch {
|
||||||
completion(.failure(error))
|
completion(.failure(error))
|
||||||
}
|
}
|
||||||
@ -241,7 +219,7 @@ private extension RedditFeedProvider {
|
|||||||
if let error = error {
|
if let error = error {
|
||||||
completion(.failure(error))
|
completion(.failure(error))
|
||||||
} else {
|
} else {
|
||||||
self.retrieveListing(api: api, parameters: parameters, completion: completion)
|
self.fetch(api: api, parameters: parameters, resultType: resultType, completion: completion)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
Frameworks/Account/FeedProvider/Reddit/RedditSubreddit.swift
Normal file
19
Frameworks/Account/FeedProvider/Reddit/RedditSubreddit.swift
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// RedditSubreddit.swift
|
||||||
|
// Account
|
||||||
|
//
|
||||||
|
// Created by Maurice Parker on 5/4/20.
|
||||||
|
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct RedditSubreddit: Codable {
|
||||||
|
|
||||||
|
let iconImg: String?
|
||||||
|
|
||||||
|
enum CodingKeys: String, CodingKey {
|
||||||
|
case iconImg = "iconImg"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -16,25 +16,4 @@ struct RedditUser: Codable {
|
|||||||
case name = "name"
|
case name = "name"
|
||||||
}
|
}
|
||||||
|
|
||||||
// var url: String {
|
|
||||||
// return "https://twitter.com/\(screenName ?? "")"
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// func renderAsHTML() -> String? {
|
|
||||||
// var html = String()
|
|
||||||
// html += "<div><a href=\"\(url)\">"
|
|
||||||
// if let avatarURL = avatarURL {
|
|
||||||
// html += "<img class=\"twitterAvatar\" src=\"\(avatarURL)\">"
|
|
||||||
// }
|
|
||||||
// html += "<span class=\"twitterUsername\">"
|
|
||||||
// if let name = name {
|
|
||||||
// html += " \(name)"
|
|
||||||
// }
|
|
||||||
// if let screenName = screenName {
|
|
||||||
// html += " @\(screenName)"
|
|
||||||
// }
|
|
||||||
// html += "</span></a></div>"
|
|
||||||
// return html
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user