Skip to content

Commit

Permalink
refactor: Add ExistingResource base class
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Sep 2, 2024
1 parent 44175f3 commit d375a59
Show file tree
Hide file tree
Showing 20 changed files with 132 additions and 101 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.0.0-beta1] - 2024-09-??
## [1.0.0-beta1] - 2024-09-02

### Added
- Video API

### Changed
- Standardised `Existing*` classes to extend `ExistingResource` for consistency

## [0.9.0] - 2024-08-19

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/vonage/client/kt/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Application internal constructor(private val client: ApplicationClient) {

fun application(applicationId: UUID) = application(applicationId.toString())

inner class ExistingApplication internal constructor(val id: String) {
inner class ExistingApplication internal constructor(id: String): ExistingResource(id) {
fun get(): Application = client.getApplication(id)

fun update(properties: Application.Builder.() -> Unit): Application =
Expand Down
35 changes: 35 additions & 0 deletions src/main/kotlin/com/vonage/client/kt/ExistingResource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2024 Vonage
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.vonage.client.kt

open class ExistingResource internal constructor(val id: String) {

@Override
override fun toString(): String = "${this::class.qualifiedName} {id=$id}"

@Override
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as ExistingResource
return id == other.id
}

@Override
override fun hashCode(): Int {
return id.hashCode()
}
}
8 changes: 4 additions & 4 deletions src/main/kotlin/com/vonage/client/kt/Subaccounts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ class Subaccounts internal constructor(private val client: SubaccountsClient) {

fun subaccount(subaccountKey: String): ExistingSubaccount = ExistingSubaccount(subaccountKey)

inner class ExistingSubaccount internal constructor(val key: String) {
inner class ExistingSubaccount internal constructor(key: String): ExistingResource(key) {

fun get(): Account = client.getSubaccount(key)
fun get(): Account = client.getSubaccount(id)

fun suspended(suspend: Boolean): Account =
client.updateSubaccount(UpdateSubaccountRequest.builder(key).suspended(suspend).build())
client.updateSubaccount(UpdateSubaccountRequest.builder(id).suspended(suspend).build())

fun update(name: String? = null, usePrimaryAccountBalance: Boolean? = null): Account {
val builder = UpdateSubaccountRequest.builder(key).name(name)
val builder = UpdateSubaccountRequest.builder(id).name(name)
if (usePrimaryAccountBalance != null) {
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
}
Expand Down
15 changes: 1 addition & 14 deletions src/main/kotlin/com/vonage/client/kt/Users.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,13 @@ class Users internal constructor(private val client: UsersClient) {

fun user(user: BaseUser): ExistingUser = ExistingUser(user.id)

inner class ExistingUser internal constructor(val id: String) {
inner class ExistingUser internal constructor(id: String): ExistingResource(id) {
fun get(): User = client.getUser(id)

fun update(properties: User.Builder.() -> Unit): User =
client.updateUser(id, User.builder().apply(properties).build())

fun delete(): Unit = client.deleteUser(id)

@Override
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as ExistingUser
return id == other.id
}

@Override
override fun hashCode(): Int {
return id.hashCode()
}
}

fun create(properties: User.Builder.() -> Unit): User =
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/vonage/client/kt/Verify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ class Verify(private val client: Verify2Client) {
VerificationRequest.builder().brand(brand).apply(init).build()
)

inner class ExistingRequest internal constructor(val id: UUID) {
inner class ExistingRequest internal constructor(private val uuid: UUID): ExistingResource(uuid.toString()) {

fun cancel(): Unit = client.cancelVerification(id)
fun cancel(): Unit = client.cancelVerification(uuid)

fun nextWorkflow(): Unit = client.nextWorkflow(id)
fun nextWorkflow(): Unit = client.nextWorkflow(uuid)

fun checkVerificationCode(code: String): VerifyCodeResponse =
client.checkVerificationCode(id, code)
client.checkVerificationCode(uuid, code)

fun isValidVerificationCode(code: String): Boolean {
try {
Expand Down
14 changes: 1 addition & 13 deletions src/main/kotlin/com/vonage/client/kt/VerifyLegacy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class VerifyLegacy internal constructor(private val client: VerifyClient) {

fun request(response: VerifyResponse): ExistingRequest = request(response.requestId)

inner class ExistingRequest internal constructor(val id: String) {
inner class ExistingRequest internal constructor(id: String): ExistingResource(id) {

fun cancel(): ControlResponse = client.cancelVerification(id)

Expand All @@ -42,18 +42,6 @@ class VerifyLegacy internal constructor(private val client: VerifyClient) {

fun search(): SearchVerifyResponse = client.search(id)

@Override
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as ExistingRequest
return id == other.id
}

@Override
override fun hashCode(): Int {
return id.hashCode()
}
}

}
20 changes: 10 additions & 10 deletions src/main/kotlin/com/vonage/client/kt/Video.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ class Video(private val client: VideoClient) {

fun session(sessionId: String): ExistingSession = ExistingSession(sessionId)

inner class ExistingSession internal constructor(val id: String) {
inner class ExistingSession internal constructor(id: String): ExistingResource(id) {

fun stream(streamId: String): ExistingStream = ExistingStream(streamId)

inner class ExistingStream internal constructor(val streamId: String) {
inner class ExistingStream internal constructor(id: String): ExistingResource(id) {

fun info(): GetStreamResponse = client.getStream(id, streamId)
fun info(): GetStreamResponse = client.getStream(this@ExistingSession.id, id)

fun mute(): Unit = client.muteStream(id, streamId)
fun mute(): Unit = client.muteStream(this@ExistingSession.id, id)

fun setLayout(vararg layoutClasses: String): Unit =
client.setStreamLayout(id,
SessionStream.builder(streamId).layoutClassList(layoutClasses.toList()).build()
client.setStreamLayout(this@ExistingSession.id,
SessionStream.builder(id).layoutClassList(layoutClasses.toList()).build()
)
}

fun connection(connectionId: String): ExistingConnection = ExistingConnection(connectionId)

inner class ExistingConnection internal constructor(val id: String) {
inner class ExistingConnection internal constructor(id: String): ExistingResource(id) {

fun disconnect(): Unit = client.forceDisconnect(this@ExistingSession.id, id)

Expand Down Expand Up @@ -99,7 +99,7 @@ class Video(private val client: VideoClient) {

fun archive(archiveId: String): ExistingArchive = ExistingArchive(archiveId)

inner class ExistingArchive internal constructor(val id: String) {
inner class ExistingArchive internal constructor(id: String): ExistingResource(id) {

fun info(): Archive = client.getArchive(id)

Expand Down Expand Up @@ -128,7 +128,7 @@ class Video(private val client: VideoClient) {

fun broadcast(broadcastId: String): ExistingBroadcast = ExistingBroadcast(broadcastId)

inner class ExistingBroadcast internal constructor(val id: String) {
inner class ExistingBroadcast internal constructor(id: String): ExistingResource(id) {

fun info(): Broadcast = client.getBroadcast(id)

Expand Down Expand Up @@ -158,7 +158,7 @@ class Video(private val client: VideoClient) {

fun render(renderId: String): ExistingRender = ExistingRender(renderId)

inner class ExistingRender internal constructor(val id: String) {
inner class ExistingRender internal constructor(id: String): ExistingResource(id) {

fun info(): RenderResponse = client.getRender(id)

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/vonage/client/kt/Voice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Voice internal constructor(private val client: VoiceClient) {

fun call(callId: String): ExistingCall = ExistingCall(callId)

inner class ExistingCall internal constructor(val id: String) {
inner class ExistingCall internal constructor(id: String): ExistingResource(id) {

fun info(): CallInfo = client.getCallDetails(id)

Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/com/vonage/client/kt/AccountTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AccountTest : AbstractTest() {
)
)

val response = invocation.invoke(account)
val response = invocation(account)
assertNotNull(response)
assertEquals(moCallbackUrl, response.incomingSmsUrl)
assertEquals(drCallbackUrl, response.deliveryReceiptUrl)
Expand Down Expand Up @@ -98,7 +98,7 @@ class AccountTest : AbstractTest() {
)
val invocation = { getSecretsObj(withApiKey).list() }

val response = invocation.invoke()
val response = invocation()
assertNotNull(response)
assertEquals(2, response.size)
assertSecretResponse(response[0])
Expand All @@ -118,7 +118,7 @@ class AccountTest : AbstractTest() {
expectedRequestParams = secretRequest,
status = 201, expectedResponseParams = secretResponse
)
assertSecretResponse(invocation.invoke())
assertSecretResponse(invocation())
assert401ApiResponseException<AccountResponseException>(url, HttpMethod.POST, invocation)
}

Expand All @@ -129,15 +129,15 @@ class AccountTest : AbstractTest() {
expectedResponseParams = secretResponse
)
val invocation = { getSecretsObj(withApiKey).get(secretId) }
assertSecretResponse(invocation.invoke())
assertSecretResponse(invocation())
assert401ApiResponseException<AccountResponseException>(url, HttpMethod.GET, invocation)
}

private fun assertDeleteSecret(withApiKey: Boolean) {
val url = getSecretUrl(withApiKey)
val invocation = { getSecretsObj(withApiKey).delete(secretId) }
mockDelete(url, authType)
invocation.invoke()
invocation()

mockRequest(HttpMethod.DELETE, expectedUrl = url, authType = authType)
.mockReturn(status = errorCode, errorResponse)
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/vonage/client/kt/ApplicationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class ApplicationTest : AbstractTest() {
)
)
)
val response = invocation.invoke()
val response = invocation()
assertNotNull(response)
assertEquals(4, response.size)
assertEqualsIdOnlyApplication(response[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class NumberVerificationTest : AbstractTest() {
expectedResponseParams = if (result != null)
mapOf("devicePhoneNumberVerified" to result) else mapOf()
)
assertEquals(result ?: false, invocation.invoke(nvClient))
assertEquals(result ?: false, invocation(nvClient))
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/com/vonage/client/kt/NumbersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ class NumbersTest : AbstractTest() {
status = errorCode, authType = authType,
expectedResponseParams = errorResponse
)
assertThrows<NumbersResponseException> { invocation.invoke(client) }
assertThrows<NumbersResponseException> { invocation(client) }
}

private fun assertThrowsPost(endpoint: String, invocation: Numbers.ExistingNumber.() -> Any) {
mockPost(expectedUrl = "/number/$endpoint",
status = errorCode, authType = authType,
expectedResponseParams = errorResponse
)
assertThrows<NumbersResponseException> { invocation.invoke(existingNumber) }
assertThrows<NumbersResponseException> { invocation(existingNumber) }
}

private fun mockAction(endpoint: String, additionalParams: Map<String, String> = mapOf()) {
Expand Down Expand Up @@ -101,7 +101,7 @@ class NumbersTest : AbstractTest() {
)
)

val response = invocation.invoke(client)
val response = invocation(client)
assertNotNull(response)
assertEquals(count, response.count)
val numbers = response.numbers
Expand Down Expand Up @@ -159,7 +159,7 @@ class NumbersTest : AbstractTest() {
)
)

val response = invocation.invoke(client)
val response = invocation(client)
assertNotNull(response)
assertEquals(count, response.count)
val numbers = response.numbers
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/vonage/client/kt/RedactTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RedactTest : AbstractTest() {
"product" to product
) + if (type != null) mapOf("type" to type.name.lowercase()) else mapOf()
)
invocation.invoke(vonage.redact)
invocation(vonage.redact)
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/vonage/client/kt/SimSwapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class SimSwapTest : AbstractTest() {
expectedRequestParams = phoneNumberMap + mapOf("maxAge" to maxAge),
expectedResponseParams = if (result != null) mapOf("swapped" to result) else mapOf()
)
assertEquals(result ?: false, invocation.invoke(simSwapClient))
assertEquals(result == true, invocation(simSwapClient))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/vonage/client/kt/SmsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SmsTest : AbstractTest() {
)
)

val response = invocation.invoke()
val response = invocation()
assertNotNull(response)
assertEquals(1, response.size)
val first = response.first()
Expand Down
Loading

0 comments on commit d375a59

Please sign in to comment.