From 9c4131928db3298c97fda7f2c1736e2ad697824a Mon Sep 17 00:00:00 2001 From: Sina Madani Date: Mon, 5 Aug 2024 14:55:19 +0100 Subject: [PATCH] refactor!: ExistingRequest for Verify v2 --- .../kotlin/com/vonage/client/kt/Verify.kt | 41 +++++++++---------- .../kotlin/com/vonage/client/kt/VerifyTest.kt | 24 +++++------ 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/main/kotlin/com/vonage/client/kt/Verify.kt b/src/main/kotlin/com/vonage/client/kt/Verify.kt index 73b7aa9..07b74d0 100644 --- a/src/main/kotlin/com/vonage/client/kt/Verify.kt +++ b/src/main/kotlin/com/vonage/client/kt/Verify.kt @@ -27,35 +27,32 @@ class Verify(private val verify2Client: Verify2Client) { VerificationRequest.builder().brand(brand).apply(init).build() ) - fun cancelVerification(requestId: UUID): Unit = verify2Client.cancelVerification(requestId) + inner class ExistingRequest internal constructor(private val requestId: UUID) { - fun cancelVerification(requestId: String): Unit = cancelVerification(UUID.fromString(requestId)) + fun cancel(): Unit = verify2Client.cancelVerification(requestId) - fun nextWorkflow(requestId: UUID): Unit = verify2Client.nextWorkflow(requestId) + fun nextWorkflow(): Unit = verify2Client.nextWorkflow(requestId) - fun nextWorkflow(requestId: String): Unit = nextWorkflow(UUID.fromString(requestId)) + fun checkVerificationCode(code: String): VerifyCodeResponse = + verify2Client.checkVerificationCode(requestId, code) - fun checkVerificationCode(requestId: UUID, code: String): VerifyCodeResponse = - verify2Client.checkVerificationCode(requestId, code) - - fun checkVerificationCode(requestId: String, code: String): VerifyCodeResponse = - checkVerificationCode(UUID.fromString(requestId), code) - - fun isValidVerificationCode(requestId: String, code: String): Boolean = - isValidVerificationCode(UUID.fromString(requestId), code) - - fun isValidVerificationCode(requestId: UUID, code: String): Boolean { - try { - checkVerificationCode(requestId, code) - return true - } catch (ex: VerifyResponseException) { - if (ex.statusCode == 400 || ex.statusCode == 410) { - return false - } else { - throw ex + fun isValidVerificationCode(code: String): Boolean { + try { + checkVerificationCode(code) + return true + } catch (ex: VerifyResponseException) { + if (ex.statusCode == 400 || ex.statusCode == 410) { + return false + } else { + throw ex + } } } } + + fun request(requestId: UUID): ExistingRequest = ExistingRequest(requestId) + + fun request(requestId: String): ExistingRequest = request(UUID.fromString(requestId)) } fun VerificationRequest.Builder.silentAuth( diff --git a/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt b/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt index 0d429f1..8db0ae8 100644 --- a/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt @@ -29,6 +29,7 @@ class VerifyTest : AbstractTest() { private val requestIdStr = "c11236f4-00bf-4b89-84ba-88b25df97315" private val requestId = UUID.fromString(requestIdStr) private val requestIdUrl = "$baseUrl/$requestIdStr" + private val existingRequest = verifyClient.request(requestIdStr) private val timeout = 60 private val fraudCheck = false private val sandbox = true @@ -171,10 +172,9 @@ class VerifyTest : AbstractTest() { @Test fun `cancel verification`() { mockDelete(requestIdUrl) - verifyClient.cancelVerification(requestIdStr) - verifyClient.cancelVerification(requestId) + existingRequest.cancel() assertVerifyResponseException(requestIdUrl, HttpMethod.DELETE) { - verifyClient.cancelVerification(requestIdStr) + existingRequest.cancel() } } @@ -182,26 +182,24 @@ class VerifyTest : AbstractTest() { fun `next workflow`() { val expectedUrl = "$requestIdUrl/next-workflow" mockPost(expectedUrl) - verifyClient.nextWorkflow(requestIdStr) - verifyClient.nextWorkflow(requestId) + existingRequest.nextWorkflow() assertVerifyResponseException(expectedUrl, HttpMethod.POST) { - verifyClient.nextWorkflow(requestId) + existingRequest.nextWorkflow() } assertVerifyResponseException(expectedUrl, HttpMethod.POST) { - verifyClient.nextWorkflow(requestIdStr) + existingRequest.nextWorkflow() } } @Test fun `check valid verification code`() { - val call: () -> Boolean = {verifyClient.isValidVerificationCode(requestIdStr, code)} + val call: () -> Boolean = { + existingRequest.isValidVerificationCode(code) + } mockPost(requestIdUrl, checkCodeRequestParams, 200) assertTrue(call.invoke()) - verifyClient.checkVerificationCode(requestIdStr, code) - verifyClient.checkVerificationCode(requestId, code) - assertTrue(verifyClient.isValidVerificationCode(requestId, code)) - + existingRequest.checkVerificationCode(code) val title = "Invalid Code" @@ -221,7 +219,7 @@ class VerifyTest : AbstractTest() { assertVerifyResponseException(requestIdUrl, HttpMethod.POST, call) assertVerifyResponseException(requestIdUrl, HttpMethod.POST) { - verifyClient.checkVerificationCode(requestIdStr, code) + existingRequest.checkVerificationCode(code) } } } \ No newline at end of file