From 3433e55f9195fb29570c960b5c397466ca51b26e Mon Sep 17 00:00:00 2001 From: Sina Madani Date: Thu, 6 Jun 2024 17:45:32 +0100 Subject: [PATCH] Wrap Verify2 client --- pom.xml | 12 ++-- .../kotlin/com/vonage/client/kt/Messages.kt | 10 +-- .../kotlin/com/vonage/client/kt/Verify.kt | 70 ++++++++++++++++--- .../kotlin/com/vonage/client/kt/Vonage.kt | 17 +++-- 4 files changed, 80 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index b6ec0d1..10c1abc 100644 --- a/pom.xml +++ b/pom.xml @@ -44,10 +44,9 @@ https://oss.sonatype.org scm:git@github.com:Vonage/vonage-kotlin-sdk UTF-8 - 8 - 2.0.0 - ${kotlin.version} - ${kotlin.version} + 1.8 + 2.0 + 2.0 ${java.version} ${java.version} @@ -56,7 +55,7 @@ org.jetbrains.kotlin kotlin-stdlib - ${kotlin.version} + 2.0.0 com.vonage @@ -66,7 +65,7 @@ org.jetbrains.kotlin kotlin-test-junit - ${kotlin.version} + 2.0.0 test @@ -187,7 +186,6 @@ kotlin-maven-plugin org.jetbrains.kotlin - ${kotlin.version} true diff --git a/src/main/kotlin/com/vonage/client/kt/Messages.kt b/src/main/kotlin/com/vonage/client/kt/Messages.kt index 5a3ad47..9a15700 100644 --- a/src/main/kotlin/com/vonage/client/kt/Messages.kt +++ b/src/main/kotlin/com/vonage/client/kt/Messages.kt @@ -8,17 +8,17 @@ import com.vonage.client.messages.messenger.* import com.vonage.client.messages.viber.* import java.util.UUID -class Messages (private val messagesClient: MessagesClient) { - fun send(message : MessageRequest) : UUID { - return messagesClient.sendMessage(message).messageUuid +class Messages(private val client: MessagesClient) { + fun send(message: MessageRequest): UUID { + return client.sendMessage(message).messageUuid } } -fun sms(init: SmsTextRequest.Builder.() -> Unit): SmsTextRequest { +fun smsText(init: SmsTextRequest.Builder.() -> Unit): SmsTextRequest { return SmsTextRequest.builder().apply(init).build() } -fun vcard(init: MmsVcardRequest.Builder.() -> Unit): MmsVcardRequest { +fun mmsVcard(init: MmsVcardRequest.Builder.() -> Unit): MmsVcardRequest { return MmsVcardRequest.builder().apply(init).build() } diff --git a/src/main/kotlin/com/vonage/client/kt/Verify.kt b/src/main/kotlin/com/vonage/client/kt/Verify.kt index dcb11df..f8d8a2f 100644 --- a/src/main/kotlin/com/vonage/client/kt/Verify.kt +++ b/src/main/kotlin/com/vonage/client/kt/Verify.kt @@ -1,19 +1,69 @@ -package org.example.com.vonage.client.kt +package com.vonage.client.kt -import com.vonage.client.verify2.SmsWorkflow -import com.vonage.client.verify2.VerificationRequest -import com.vonage.client.verify2.Verify2Client -import com.vonage.client.verify2.VoiceWorkflow +import com.vonage.client.verify2.* import java.util.* -fun Verify2Client.sendVerification(init: VerificationRequest.Builder.() -> Unit) : UUID { - return this.sendVerification(VerificationRequest.builder().apply(init).build()).requestId +class Verify(private val verify2Client: Verify2Client) { + + fun sendVerification( + brand: String = "Vonage", + init: VerificationRequest.Builder.() -> Unit + ): UUID { + + return verify2Client.sendVerification( + VerificationRequest.builder().brand(brand).apply(init).build() + ).requestId + } + + fun cancelVerification(requestId: String) { + verify2Client.cancelVerification(UUID.fromString(requestId)) + } + + fun nextWorkflow(requestId: String) { + verify2Client.nextWorkflow(UUID.fromString(requestId)) + } + + fun checkVerificationCode(requestId: String, code: String) { + verify2Client.checkVerificationCode(UUID.fromString(requestId), code) + } + + fun isValidVerificationCode(requestId: String, code: String): Boolean { + try { + verify2Client.checkVerificationCode(UUID.fromString(requestId), code) + return true + } catch (ex: VerifyResponseException) { + if (ex.statusCode == 400 || ex.statusCode == 410) { + return false + } else { + throw ex + } + } + } +} + +fun VerificationRequest.Builder.silentAuth(number: String): VerificationRequest.Builder { + return this.addWorkflow(SilentAuthWorkflow(number)) } -fun VerificationRequest.Builder.sms(number: String, init: SmsWorkflow.Builder.() -> Unit = {}) : VerificationRequest.Builder { +fun VerificationRequest.Builder.sms( + number: String, + init: SmsWorkflow.Builder.() -> Unit = {} +): VerificationRequest.Builder { return this.addWorkflow(SmsWorkflow.builder(number).apply(init).build()) } -fun VerificationRequest.Builder.voice(number: String) : VerificationRequest.Builder { +fun VerificationRequest.Builder.voice(number: String): VerificationRequest.Builder { return this.addWorkflow(VoiceWorkflow(number)) -} \ No newline at end of file +} + +fun VerificationRequest.Builder.email(to: String, from: String? = null): VerificationRequest.Builder { + return this.addWorkflow(EmailWorkflow(to, from)) +} + +fun VerificationRequest.Builder.whatsapp(to: String, from: String): VerificationRequest.Builder { + return this.addWorkflow(WhatsappWorkflow(to, from)) +} + +fun VerificationRequest.Builder.whatsappCodeless(to: String, from: String): VerificationRequest.Builder { + return this.addWorkflow(WhatsappCodelessWorkflow(to, from)) +} diff --git a/src/main/kotlin/com/vonage/client/kt/Vonage.kt b/src/main/kotlin/com/vonage/client/kt/Vonage.kt index 0af2c7e..a682811 100644 --- a/src/main/kotlin/com/vonage/client/kt/Vonage.kt +++ b/src/main/kotlin/com/vonage/client/kt/Vonage.kt @@ -1,13 +1,18 @@ -package org.example.com.vonage.client.kt +package com.vonage.client.kt import com.vonage.client.HttpConfig import com.vonage.client.VonageClient -import com.vonage.client.kt.Messages +import com.vonage.client.account.AccountClient +import com.vonage.client.messages.MessagesClient +import com.vonage.client.verify2.SmsWorkflow +import com.vonage.client.verify2.Verify2Client +import com.vonage.client.voice.VoiceClient class Vonage constructor(init: VonageClient.Builder.() -> Unit) { private val vonageClient : VonageClient = VonageClient.builder().apply(init).build(); - val verify = vonageClient.verify2Client val messages = Messages(vonageClient.messagesClient) + val verify: Verify2Client = vonageClient.verify2Client + } fun VonageClient.Builder.authFromEnv() : VonageClient.Builder { @@ -23,12 +28,10 @@ fun httpConfig(init: HttpConfig.Builder.() -> Unit): HttpConfig { return HttpConfig.builder().apply(init).build() } -private fun env(variable : String) : String { +private fun env(variable: String) : String? { return System.getenv(variable) } fun main() { - val client = Vonage { - authFromEnv() - } + val client = Vonage { authFromEnv() } } \ No newline at end of file