Skip to content

Commit

Permalink
feat: Add Voice.getCalls
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jun 26, 2024
1 parent 74c63fc commit 77c022c
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added
- Sandbox support in Messages
- Voice API

### Fixed
- `authFromEnv` now checks for absent environment variables before attempting to set them

## [0.1.0] - 2024-06-25

Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/com/vonage/client/kt/Voice.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.vonage.client.kt

import com.vonage.client.voice.CallInfo
import com.vonage.client.voice.VoiceClient
import java.util.*

class Voice(private val voiceClient: VoiceClient) {

fun getCall(callId: UUID): CallInfo = getCall(callId.toString())

fun getCall(callId: String): CallInfo = voiceClient.getCallDetails(callId)


}
6 changes: 1 addition & 5 deletions src/main/kotlin/com/vonage/client/kt/Vonage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package com.vonage.client.kt

import com.vonage.client.HttpConfig
import com.vonage.client.VonageClient
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 messages = Messages(vonageClient.messagesClient)
val verify = Verify(vonageClient.verify2Client)
val voice = Voice(vonageClient.voiceClient)
}

fun VonageClient.Builder.authFromEnv(): VonageClient.Builder {
Expand Down
36 changes: 29 additions & 7 deletions src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ abstract class AbstractTest {
protected val applicationId = "00000000-0000-4000-8000-000000000000"
private val apiSecret = "1234567890abcdef"
private val apiKeySecretEncoded = "YTFiMmMzZDQ6MTIzNDU2Nzg5MGFiY2RlZg=="
protected val testUuid = UUID.fromString("aaaaaaaa-bbbb-4ccc-8ddd-0123456789ab")
protected val toNumber = "447712345689"
protected val altNumber = "447700900001"
protected val testUuid = UUID.fromString("aaaaaaaa-bbbb-4ccc-8ddd-0123456789ab")
protected val networkCode = "65512"
protected val startTime = "2020-09-17T12:34:56Z"
protected val endTime = "2021-09-17T12:35:28Z"

private val port = 8081
val wiremock: WireMockServer = WireMockServer(
options().port(port).notifier(ConsoleNotifier(false))
)

val vonageClient = Vonage {
val vonage = Vonage {
apiKey(apiKey); apiSecret(apiSecret); applicationId(applicationId)
privateKeyPath("src/test/resources/com/vonage/client/kt/application_key")
httpConfig {
Expand Down Expand Up @@ -68,7 +71,7 @@ abstract class AbstractTest {
contentType: ContentType? = null,
accept: ContentType? = null,
authType: AuthType? = null,
expectedBodyParams: Map<String, Any>? = null): BuildingStep =
expectedParams: Map<String, Any>? = null): BuildingStep =
wiremock.requestServerBuilderStep({
url equalTo expectedUrl
headers contains "User-Agent" like "vonage-java-sdk\\/.+ java\\/.+"
Expand All @@ -87,13 +90,19 @@ abstract class AbstractTest {
}
if (contentType != null) {
headers contains "Content-Type" equalTo contentType.mime
if (expectedParams != null) when(contentType) {
ContentType.APPLICATION_JSON -> {
body equalTo ObjectMapper().writeValueAsString(expectedParams)
}
ContentType.FORM_URLENCODED -> {
// TODO recursive decomposition for nested params
expectedParams.forEach {k, v -> headers contains k equalTo v.toString() }
}
}
}
if (accept != null) {
headers contains "Accept" equalTo accept.mime
}
if (expectedBodyParams != null) {
body equalTo ObjectMapper().writeValueAsString(expectedBodyParams)
}
}, when (httpMethod) {
HttpMethod.GET -> WireMock::get
HttpMethod.POST -> WireMock::post
Expand All @@ -114,9 +123,22 @@ abstract class AbstractTest {
AuthType.JWT, expectedRequestParams
).mockReturn(status, expectedResponseParams)

protected fun mockDelete(expectedUrl: String, authType: AuthType? = null): ReturnsStep =
protected fun mockDelete(expectedUrl: String, authType: AuthType? = null) =
mockRequest(HttpMethod.DELETE, expectedUrl, authType = authType).mockReturn(204)

protected fun mockGet(expectedUrl: String,
expectedQueryParams: Map<String, Any>? = null,
status: Int = 200,
authType: AuthType? = AuthType.JWT,
expectedResponseParams: Map<String, Any>) =

mockRequest(HttpMethod.GET, expectedUrl,
contentType = if (expectedQueryParams != null) ContentType.FORM_URLENCODED else null,
accept = ContentType.APPLICATION_JSON,
authType = authType, expectedQueryParams
).mockReturn(status, expectedResponseParams)


protected fun BuildingStep.mockReturn(
status: Int? = null, expectedBody: Map<String, Any>? = null): ReturnsStep =
returns {
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/vonage/client/kt/MessagesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class MessagesTest : AbstractTest() {
private val messagesClient = vonageClient.messages
private val messagesClient = vonage.messages
private val sendUrl = "/v1/messages"
private val messageUuid = testUuid
private val mmsChannel = "mms"
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/vonage/client/kt/VerifyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.util.UUID
import kotlin.test.*

class VerifyTest : AbstractTest() {
private val verifyClient = vonageClient.verify
private val verifyClient = vonage.verify
private val baseUrl = "/v2/verify"
private val requestIdStr = "c11236f4-00bf-4b89-84ba-88b25df97315"
private val requestId = UUID.fromString(requestIdStr)
Expand Down
72 changes: 72 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/VoiceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.vonage.client.kt

import com.vonage.client.voice.CallDirection
import com.vonage.client.voice.CallStatus
import com.vonage.client.voice.PhoneEndpoint
import java.time.Instant
import java.util.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class VoiceTest : AbstractTest() {
private val voiceClient = vonage.voice
private val callsUrl = "/v1/calls"
private val callIdStr = "63f61863-4a51-4f6b-86e1-46edebcf9356"
private val conversationId = "CON-f972836a-550f-45fa-956c-12a2ab5b7d22"

@Test
fun `get call`() {
val price = "23.40"
val duration = 60
val rate = "0.39"
val phoneType = "phone"

mockGet(expectedUrl = "$callsUrl/$callIdStr", expectedResponseParams = mapOf(
"_links" to mapOf(
"self" to mapOf(
"href" to "/calls/$callIdStr"
)
),
"uuid" to callIdStr,
"conversation_uuid" to conversationId,
"to" to mapOf(
"type" to phoneType,
"number" to toNumber
),
"from" to mapOf(
"type" to phoneType,
"number" to altNumber
),
"status" to "completed",
"direction" to "outbound",
"rate" to rate,
"price" to price,
"duration" to "$duration",
"start_time" to startTime,
"end_time" to endTime,
"network" to networkCode
))

val response = voiceClient.getCall(UUID.fromString(callIdStr))
assertNotNull(response)
assertEquals(callIdStr, response.uuid)
assertEquals(conversationId, response.conversationUuid)
val to = response.to
assertNotNull(to)
assertEquals(phoneType, to.type)
assertEquals(toNumber, (to as PhoneEndpoint).number)
val from = response.from
assertNotNull(from)
assertEquals(phoneType, from.type)
assertEquals(altNumber, (from as PhoneEndpoint).number)
assertEquals(CallStatus.COMPLETED, response.status)
assertEquals(CallDirection.OUTBOUND, response.direction)
assertEquals(rate, response.rate)
assertEquals(price, response.price)
assertEquals(duration, response.duration)
assertEquals(Instant.parse(startTime), response.startTime.toInstant())
assertEquals(Instant.parse(endTime), response.endTime.toInstant())
assertEquals(networkCode, response.network)
}
}

0 comments on commit 77c022c

Please sign in to comment.