Skip to content

Commit

Permalink
Merge pull request #133 from Automattic/etoledom/remove-completion-bl…
Browse files Browse the repository at this point in the history
…ock-from-profile-service

Removing completion block from Profile Service
  • Loading branch information
etoledom authored Mar 25, 2024
2 parents 3cad0a1 + 6958d72 commit afee801
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 71 deletions.
30 changes: 16 additions & 14 deletions Demo/Demo/Gravatar-Demo/DemoFetchProfileViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,31 @@ class DemoFetchProfileViewController: UIViewController {
}
profileTextView.text = nil
activityIndicator.startAnimating()
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)
}
Task {
await fetchProfile(with: email)
}
}

func fetchProfile(with email: String) async {
let service = ProfileService()
do {
let profile = try await service.fetch(withEmail: email)
setProfile(with: profile)
} catch {
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) {
Expand Down
17 changes: 0 additions & 17 deletions Sources/Gravatar/Network/Services/ProfileService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
40 changes: 0 additions & 40 deletions Tests/GravatarTests/ProfileServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]") { 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: "[email protected]") { 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)
Expand Down

0 comments on commit afee801

Please sign in to comment.