From aa05b7f514d9a9d4f7a7d3fb8f5b116fc454a488 Mon Sep 17 00:00:00 2001 From: Cassius Pacheco Date: Wed, 9 Nov 2022 16:55:06 +1100 Subject: [PATCH] feat: expose getUser method Get User is now part of the ImmutableX interface. --- CHANGELOG.md | 2 +- Sources/ImmutableXCore/ImmutableX.swift | 16 ++++++++- .../ImmutableXCoreTests/ImmutableXTests.swift | 35 +++++++++++++++++-- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34e2721..2bc26f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -for added features. +- `getUser(ethAddress:)` has been added to ImmutableX instance. ### Changed diff --git a/Sources/ImmutableXCore/ImmutableX.swift b/Sources/ImmutableXCore/ImmutableX.swift index add1960..d5775b5 100644 --- a/Sources/ImmutableXCore/ImmutableX.swift +++ b/Sources/ImmutableXCore/ImmutableX.swift @@ -34,6 +34,7 @@ public struct ImmutableX { private let transferWorkflow: TransferWorkflow.Type private let registerWorkflow: RegisterWorkflow.Type private let buyCryptoWorkflow: BuyCryptoWorkflow.Type + private let usersAPI: UsersAPI.Type /// Internal init method that includes dependencies. For the public facing API use ``initialize(base:logLevel:)`` /// instead. @@ -45,7 +46,8 @@ public struct ImmutableX { cancelOrderWorkflow: CancelOrderWorkflow.Type = CancelOrderWorkflow.self, transferWorkflow: TransferWorkflow.Type = TransferWorkflow.self, registerWorkflow: RegisterWorkflow.Type = RegisterWorkflow.self, - buyCryptoWorkflow: BuyCryptoWorkflow.Type = BuyCryptoWorkflow.self + buyCryptoWorkflow: BuyCryptoWorkflow.Type = BuyCryptoWorkflow.self, + usersAPI: UsersAPI.Type = UsersAPI.self ) { self.base = base self.logLevel = logLevel @@ -55,6 +57,7 @@ public struct ImmutableX { self.transferWorkflow = transferWorkflow self.registerWorkflow = registerWorkflow self.buyCryptoWorkflow = buyCryptoWorkflow + self.usersAPI = usersAPI } /// Initializes the SDK with the given ``base`` and ``logLevel`` by assigning a shared instance accessible via @@ -157,6 +160,17 @@ public struct ImmutableX { _ = try await registerWorkflow.registerOffchain(signer: signer, starkSigner: starkSigner) } + /// Get stark keys for a registered user + /// + /// - Parameter ethAddress: User ETH address + /// - Returns: ``GetUsersApiResponse`` + /// - Throws: A variation of ``ImmutableXError`` + public func getUser(ethAddress: String) async throws -> GetUsersApiResponse { + try await APIErrorMapper.map(caller: "Get Users") { + try await self.usersAPI.getUsers(user: ethAddress) + } + } + /// Gets a URL to MoonPay that provides a service for buying crypto directly on Immutable in exchange for fiat. /// /// - Parameters: diff --git a/Tests/ImmutableXCoreTests/ImmutableXTests.swift b/Tests/ImmutableXCoreTests/ImmutableXTests.swift index 2149f58..648cbed 100644 --- a/Tests/ImmutableXCoreTests/ImmutableXTests.swift +++ b/Tests/ImmutableXCoreTests/ImmutableXTests.swift @@ -8,8 +8,17 @@ final class ImmutableXTests: XCTestCase { let transferWorkflowMock = TransferWorkflowMock.self let registerWorkflowMock = RegisterWorkflowMock.self let buyCryptoWorkflowMock = BuyCryptoWorkflowMock.self - - lazy var core = ImmutableX(buyWorkflow: buyWorkflow, sellWorkflow: sellWorkflow, cancelOrderWorkflow: cancelOrderWorkflow, transferWorkflow: transferWorkflowMock, registerWorkflow: registerWorkflowMock, buyCryptoWorkflow: buyCryptoWorkflowMock) + let usersAPIMock = UsersAPIMock.self + + lazy var core = ImmutableX( + buyWorkflow: buyWorkflow, + sellWorkflow: sellWorkflow, + cancelOrderWorkflow: cancelOrderWorkflow, + transferWorkflow: transferWorkflowMock, + registerWorkflow: registerWorkflowMock, + buyCryptoWorkflow: buyCryptoWorkflowMock, + usersAPI: usersAPIMock + ) override func setUp() { super.setUp() @@ -19,6 +28,7 @@ final class ImmutableXTests: XCTestCase { transferWorkflowMock.resetMock() registerWorkflowMock.resetMock() buyCryptoWorkflowMock.resetMock() + usersAPIMock.resetMock() ImmutableX.initialize() let buyCompanion = BuyWorkflowCompanion() @@ -44,6 +54,10 @@ final class ImmutableXTests: XCTestCase { let buyCryptoCompanion = BuyCryptoWorkflowCompanion() buyCryptoCompanion.returnValue = "expected url" buyCryptoWorkflowMock.mock(buyCryptoCompanion) + + let usersCompanion = UsersAPIMockGetUsersCompanion() + usersCompanion.returnValue = GetUsersApiResponse(accounts: ["some key"]) + usersAPIMock.mock(usersCompanion) } func testSdkVersion() { @@ -162,4 +176,21 @@ final class ImmutableXTests: XCTestCase { _ = try await self.core.buyCryptoURL(signer: SignerMock()) } } + + // MARK: - Get User + + func testGetUserSuccess() async throws { + let response = try await core.getUser(ethAddress: "address") + XCTAssertEqual(response, GetUsersApiResponse(accounts: ["some key"])) + } + + func testGetUserFailure() async { + let usersCompanion = UsersAPIMockGetUsersCompanion() + usersCompanion.throwableError = DummyError.something + usersAPIMock.mock(usersCompanion) + + await XCTAssertThrowsErrorAsync { + _ = try await core.getUser(ethAddress: "address") + } + } }