From 0b6c865a91426f5413ee492e477263e26ac3db2a Mon Sep 17 00:00:00 2001 From: Eduardo Toledo Date: Mon, 25 Mar 2024 10:07:23 +0100 Subject: [PATCH 1/2] Removing completion block from Profile Service --- .../DemoFetchProfileViewController.swift | 29 ++++++++------ .../Network/Services/ProfileService.swift | 17 -------- Tests/GravatarTests/ProfileServiceTests.swift | 40 ------------------- 3 files changed, 16 insertions(+), 70 deletions(-) diff --git a/Demo/Demo/Gravatar-Demo/DemoFetchProfileViewController.swift b/Demo/Demo/Gravatar-Demo/DemoFetchProfileViewController.swift index c8fabc7f..eddade0e 100644 --- a/Demo/Demo/Gravatar-Demo/DemoFetchProfileViewController.swift +++ b/Demo/Demo/Gravatar-Demo/DemoFetchProfileViewController.swift @@ -66,29 +66,32 @@ class DemoFetchProfileViewController: UIViewController { } profileTextView.text = nil activityIndicator.startAnimating() + Task { + await fetchProfile(with: email) + } + } + + nonisolated + func fetchProfile(with email: String) async { let service = Gravatar.ProfileService() - service.fetchProfile(with: email) { [weak self] result in - switch result { - case .success(let profile): - self?.setProfile(with: profile) - case .failure(let error): - self?.showError(error) - } + do { + let profile = try await service.fetch(withEmail: email) + await setProfile(with: profile) + } catch { + await showError(error) } } func setProfile(with profile: UserProfile) { - DispatchQueue.main.async { [weak self] in - self?.activityIndicator.stopAnimating() - self?.profileTextView.text = """ + activityIndicator.stopAnimating() + profileTextView.text = """ Profile URL: \(profile.profileUrl) -Display name: \(profile.displayName) -Name: \(profile.displayName) +Display name: \(profile.displayName ?? "") +Name: \(profile.displayName ?? "") Preferred User Name: \(profile.preferredUsername) Thumbnail URL: \(profile.thumbnailUrl) Last edit: \(String(describing: profile.lastProfileEditDate)) """ - } } func showError(_ error: Error) { diff --git a/Sources/Gravatar/Network/Services/ProfileService.swift b/Sources/Gravatar/Network/Services/ProfileService.swift index ecca83f1..16194cdf 100644 --- a/Sources/Gravatar/Network/Services/ProfileService.swift +++ b/Sources/Gravatar/Network/Services/ProfileService.swift @@ -19,23 +19,6 @@ public struct ProfileService: ProfileFetching { self.client = client ?? URLSessionHTTPClient() } - /// Fetches a Gravatar user's profile information. - /// - Parameters: - /// - email: The user account email. - /// - onCompletion: The completion handler to call when the fetch request is complete. - public func fetchProfile(with email: String, onCompletion: @escaping ((_ result: GravatarProfileFetchResult) -> Void)) { - Task { - do { - let profile = try await fetch(withEmail: email) - onCompletion(.success(profile)) - } catch let error as ProfileServiceError { - onCompletion(.failure(error)) - } catch { - onCompletion(.failure(.responseError(reason: .unexpected(error)))) - } - } - } - public func fetch(withEmail email: String) async throws -> UserProfile { try await fetch(withPath: email.sha256()) } diff --git a/Tests/GravatarTests/ProfileServiceTests.swift b/Tests/GravatarTests/ProfileServiceTests.swift index d6c1319c..f948ce05 100644 --- a/Tests/GravatarTests/ProfileServiceTests.swift +++ b/Tests/GravatarTests/ProfileServiceTests.swift @@ -39,46 +39,6 @@ final class ProfileServiceTests: XCTestCase { } } - func testFetchGravatarProfileWithCompletionHandler() { - let session = URLSessionMock(returnData: jsonData, response: .successResponse()) - let client = URLSessionHTTPClient(urlSession: session) - let service = ProfileService(client: client) - let expectation = expectation(description: "request finishes") - - service.fetchProfile(with: "some@email.com") { result in - switch result { - case .success(let profile): - XCTAssertEqual(profile.displayName, "Beau Lebens") - case .failure(let error): - XCTFail(error.localizedDescription) - } - expectation.fulfill() - } - - wait(for: [expectation], timeout: 0.1) - } - - func testFetchGravatarProfileWithCompletionHandlerError() { - let session = URLSessionMock(returnData: jsonData, response: .errorResponse(code: 404)) - let client = URLSessionHTTPClient(urlSession: session) - let service = ProfileService(client: client) - let expectation = expectation(description: "request finishes") - - service.fetchProfile(with: "some@email.com") { result in - switch result { - case .success: - XCTFail("Should error") - case .failure(.responseError(let reason)): - XCTAssertEqual(reason.httpStatusCode, 404) - default: - XCTFail() - } - expectation.fulfill() - } - - wait(for: [expectation], timeout: 0.1) - } - func testFetchGravatarProfileLastEditDate() async throws { let session = URLSessionMock(returnData: jsonData, response: .successResponse()) let client = URLSessionHTTPClient(urlSession: session) From 650190e25ef72860c796b5fbe04ecd25965627d0 Mon Sep 17 00:00:00 2001 From: Eduardo Toledo Date: Mon, 25 Mar 2024 16:30:00 +0100 Subject: [PATCH 2/2] Removing nonisolated from `fetchProfile` in demo app. --- .../Gravatar-Demo/DemoFetchProfileViewController.swift | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Demo/Demo/Gravatar-Demo/DemoFetchProfileViewController.swift b/Demo/Demo/Gravatar-Demo/DemoFetchProfileViewController.swift index eddade0e..cfd318c6 100644 --- a/Demo/Demo/Gravatar-Demo/DemoFetchProfileViewController.swift +++ b/Demo/Demo/Gravatar-Demo/DemoFetchProfileViewController.swift @@ -71,14 +71,13 @@ class DemoFetchProfileViewController: UIViewController { } } - nonisolated func fetchProfile(with email: String) async { - let service = Gravatar.ProfileService() + let service = ProfileService() do { let profile = try await service.fetch(withEmail: email) - await setProfile(with: profile) + setProfile(with: profile) } catch { - await showError(error) + showError(error) } }