Skip to content

Commit d375a59

Browse files
committed
refactor: Add ExistingResource base class
1 parent 44175f3 commit d375a59

20 files changed

+132
-101
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7-
## [1.0.0-beta1] - 2024-09-??
7+
## [1.0.0-beta1] - 2024-09-02
88

99
### Added
1010
- Video API
1111

12+
### Changed
13+
- Standardised `Existing*` classes to extend `ExistingResource` for consistency
14+
1215
## [0.9.0] - 2024-08-19
1316

1417
### Added

src/main/kotlin/com/vonage/client/kt/Application.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Application internal constructor(private val client: ApplicationClient) {
3030

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

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

3636
fun update(properties: Application.Builder.() -> Unit): Application =
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2024 Vonage
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.vonage.client.kt
17+
18+
open class ExistingResource internal constructor(val id: String) {
19+
20+
@Override
21+
override fun toString(): String = "${this::class.qualifiedName} {id=$id}"
22+
23+
@Override
24+
override fun equals(other: Any?): Boolean {
25+
if (this === other) return true
26+
if (javaClass != other?.javaClass) return false
27+
other as ExistingResource
28+
return id == other.id
29+
}
30+
31+
@Override
32+
override fun hashCode(): Int {
33+
return id.hashCode()
34+
}
35+
}

src/main/kotlin/com/vonage/client/kt/Subaccounts.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ class Subaccounts internal constructor(private val client: SubaccountsClient) {
3333

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

36-
inner class ExistingSubaccount internal constructor(val key: String) {
36+
inner class ExistingSubaccount internal constructor(key: String): ExistingResource(key) {
3737

38-
fun get(): Account = client.getSubaccount(key)
38+
fun get(): Account = client.getSubaccount(id)
3939

4040
fun suspended(suspend: Boolean): Account =
41-
client.updateSubaccount(UpdateSubaccountRequest.builder(key).suspended(suspend).build())
41+
client.updateSubaccount(UpdateSubaccountRequest.builder(id).suspended(suspend).build())
4242

4343
fun update(name: String? = null, usePrimaryAccountBalance: Boolean? = null): Account {
44-
val builder = UpdateSubaccountRequest.builder(key).name(name)
44+
val builder = UpdateSubaccountRequest.builder(id).name(name)
4545
if (usePrimaryAccountBalance != null) {
4646
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
4747
}

src/main/kotlin/com/vonage/client/kt/Users.kt

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,13 @@ class Users internal constructor(private val client: UsersClient) {
2323

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

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

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

3232
fun delete(): Unit = client.deleteUser(id)
33-
34-
@Override
35-
override fun equals(other: Any?): Boolean {
36-
if (this === other) return true
37-
if (javaClass != other?.javaClass) return false
38-
other as ExistingUser
39-
return id == other.id
40-
}
41-
42-
@Override
43-
override fun hashCode(): Int {
44-
return id.hashCode()
45-
}
4633
}
4734

4835
fun create(properties: User.Builder.() -> Unit): User =

src/main/kotlin/com/vonage/client/kt/Verify.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class Verify(private val client: Verify2Client) {
2727
VerificationRequest.builder().brand(brand).apply(init).build()
2828
)
2929

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

32-
fun cancel(): Unit = client.cancelVerification(id)
32+
fun cancel(): Unit = client.cancelVerification(uuid)
3333

34-
fun nextWorkflow(): Unit = client.nextWorkflow(id)
34+
fun nextWorkflow(): Unit = client.nextWorkflow(uuid)
3535

3636
fun checkVerificationCode(code: String): VerifyCodeResponse =
37-
client.checkVerificationCode(id, code)
37+
client.checkVerificationCode(uuid, code)
3838

3939
fun isValidVerificationCode(code: String): Boolean {
4040
try {

src/main/kotlin/com/vonage/client/kt/VerifyLegacy.kt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class VerifyLegacy internal constructor(private val client: VerifyClient) {
3232

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

35-
inner class ExistingRequest internal constructor(val id: String) {
35+
inner class ExistingRequest internal constructor(id: String): ExistingResource(id) {
3636

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

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

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

45-
@Override
46-
override fun equals(other: Any?): Boolean {
47-
if (this === other) return true
48-
if (javaClass != other?.javaClass) return false
49-
other as ExistingRequest
50-
return id == other.id
51-
}
52-
53-
@Override
54-
override fun hashCode(): Int {
55-
return id.hashCode()
56-
}
5745
}
5846

5947
}

src/main/kotlin/com/vonage/client/kt/Video.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@ class Video(private val client: VideoClient) {
2525

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

28-
inner class ExistingSession internal constructor(val id: String) {
28+
inner class ExistingSession internal constructor(id: String): ExistingResource(id) {
2929

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

32-
inner class ExistingStream internal constructor(val streamId: String) {
32+
inner class ExistingStream internal constructor(id: String): ExistingResource(id) {
3333

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

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

3838
fun setLayout(vararg layoutClasses: String): Unit =
39-
client.setStreamLayout(id,
40-
SessionStream.builder(streamId).layoutClassList(layoutClasses.toList()).build()
39+
client.setStreamLayout(this@ExistingSession.id,
40+
SessionStream.builder(id).layoutClassList(layoutClasses.toList()).build()
4141
)
4242
}
4343

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

46-
inner class ExistingConnection internal constructor(val id: String) {
46+
inner class ExistingConnection internal constructor(id: String): ExistingResource(id) {
4747

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

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

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

102-
inner class ExistingArchive internal constructor(val id: String) {
102+
inner class ExistingArchive internal constructor(id: String): ExistingResource(id) {
103103

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

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

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

131-
inner class ExistingBroadcast internal constructor(val id: String) {
131+
inner class ExistingBroadcast internal constructor(id: String): ExistingResource(id) {
132132

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

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

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

161-
inner class ExistingRender internal constructor(val id: String) {
161+
inner class ExistingRender internal constructor(id: String): ExistingResource(id) {
162162

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

src/main/kotlin/com/vonage/client/kt/Voice.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Voice internal constructor(private val client: VoiceClient) {
2525

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

28-
inner class ExistingCall internal constructor(val id: String) {
28+
inner class ExistingCall internal constructor(id: String): ExistingResource(id) {
2929

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

src/test/kotlin/com/vonage/client/kt/AccountTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class AccountTest : AbstractTest() {
6060
)
6161
)
6262

63-
val response = invocation.invoke(account)
63+
val response = invocation(account)
6464
assertNotNull(response)
6565
assertEquals(moCallbackUrl, response.incomingSmsUrl)
6666
assertEquals(drCallbackUrl, response.deliveryReceiptUrl)
@@ -98,7 +98,7 @@ class AccountTest : AbstractTest() {
9898
)
9999
val invocation = { getSecretsObj(withApiKey).list() }
100100

101-
val response = invocation.invoke()
101+
val response = invocation()
102102
assertNotNull(response)
103103
assertEquals(2, response.size)
104104
assertSecretResponse(response[0])
@@ -118,7 +118,7 @@ class AccountTest : AbstractTest() {
118118
expectedRequestParams = secretRequest,
119119
status = 201, expectedResponseParams = secretResponse
120120
)
121-
assertSecretResponse(invocation.invoke())
121+
assertSecretResponse(invocation())
122122
assert401ApiResponseException<AccountResponseException>(url, HttpMethod.POST, invocation)
123123
}
124124

@@ -129,15 +129,15 @@ class AccountTest : AbstractTest() {
129129
expectedResponseParams = secretResponse
130130
)
131131
val invocation = { getSecretsObj(withApiKey).get(secretId) }
132-
assertSecretResponse(invocation.invoke())
132+
assertSecretResponse(invocation())
133133
assert401ApiResponseException<AccountResponseException>(url, HttpMethod.GET, invocation)
134134
}
135135

136136
private fun assertDeleteSecret(withApiKey: Boolean) {
137137
val url = getSecretUrl(withApiKey)
138138
val invocation = { getSecretsObj(withApiKey).delete(secretId) }
139139
mockDelete(url, authType)
140-
invocation.invoke()
140+
invocation()
141141

142142
mockRequest(HttpMethod.DELETE, expectedUrl = url, authType = authType)
143143
.mockReturn(status = errorCode, errorResponse)

0 commit comments

Comments
 (0)