From baf970745e0fbe457116406d93a1448ee3262772 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Mon, 4 May 2020 16:08:55 -0500 Subject: [PATCH] Make RedditUser fetch reusable --- .../Reddit/RedditFeedProvider.swift | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Frameworks/Account/FeedProvider/Reddit/RedditFeedProvider.swift b/Frameworks/Account/FeedProvider/Reddit/RedditFeedProvider.swift index 4b423db60..67f7fb525 100644 --- a/Frameworks/Account/FeedProvider/Reddit/RedditFeedProvider.swift +++ b/Frameworks/Account/FeedProvider/Reddit/RedditFeedProvider.swift @@ -124,10 +124,14 @@ public final class RedditFeedProvider: FeedProvider { let oauthRefreshToken = tokenSuccess.credential.oauthRefreshToken let redditFeedProvider = RedditFeedProvider(oauthToken: oauthToken, oauthRefreshToken: oauthRefreshToken) - redditFeedProvider.retrieveUserName() { result in + redditFeedProvider.retrieveUser(api: "/api/v1/me") { result in switch result { - case .success(let username): - + case .success(let user): + guard let username = user.name else { + completion(.failure(RedditFeedProviderError.unknown)) + return + } + do { redditFeedProvider.username = username try storeCredentials(username: username, oauthToken: oauthToken, oauthRefreshToken: oauthRefreshToken) @@ -181,17 +185,19 @@ extension RedditFeedProvider: OAuth2SwiftProvider { private extension RedditFeedProvider { - func retrieveUserName(completion: @escaping (Result) -> Void) { + func retrieveUser(api: String, completion: @escaping (Result) -> Void) { guard let client = client else { completion(.failure(RedditFeedProviderError.unknown)) return } + + let url = "\(Self.apiBase)\(api)" - client.request(Self.apiBase + "/api/v1/me", method: .GET, headers: Self.userAgentHeaders) { result in + 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), let username = redditUser.name { - completion(.success(username)) + if let redditUser = try? JSONDecoder().decode(RedditUser.self, from: response.data) { + completion(.success(redditUser)) } else { completion(.failure(RedditFeedProviderError.unknown)) }