Skip to content

Commit 8cead81

Browse files
authored
feat: Add Users (#9)
* feat: Add Users * Fix tests * Equals & hashCode for ExistingUser
1 parent 005f1f6 commit 8cead81

File tree

7 files changed

+384
-16
lines changed

7 files changed

+384
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## [0.9.0] - 2024-08-2?
88

99
### Added
10-
- Application API
10+
- Application & Users API
1111

1212
## [0.8.0] - 2024-08-09
1313

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
import com.vonage.client.users.*
19+
20+
class Users internal constructor(private val client: UsersClient) {
21+
22+
fun user(userId: String): ExistingUser = ExistingUser(userId)
23+
24+
fun user(user: BaseUser): ExistingUser = ExistingUser(user.id)
25+
26+
inner class ExistingUser internal constructor(val userId: String) {
27+
fun get(): User = client.getUser(userId)
28+
29+
fun update(properties: User.Builder.() -> Unit): User =
30+
client.updateUser(userId, User.builder().apply(properties).build())
31+
32+
fun delete(): Unit = client.deleteUser(userId)
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 userId == other.userId
40+
}
41+
42+
@Override
43+
override fun hashCode(): Int {
44+
return userId.hashCode()
45+
}
46+
}
47+
48+
fun create(properties: User.Builder.() -> Unit): User =
49+
client.createUser(User.builder().apply(properties).build())
50+
51+
fun list(filter: (ListUsersRequest.Builder.() -> Unit)? = null): ListUsersResponse {
52+
val request = ListUsersRequest.builder()
53+
if (filter == null) {
54+
request.pageSize(100)
55+
}
56+
else {
57+
request.apply(filter)
58+
}
59+
return client.listUsers(request.build())
60+
}
61+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Vonage(init: VonageClient.Builder.() -> Unit) {
3131
val simSwap = SimSwap(client.simSwapClient)
3232
val sms = Sms(client.smsClient)
3333
val subaccounts = Subaccounts(client.subaccountsClient)
34+
val users = Users(client.usersClient)
3435
val verify = Verify(client.verify2Client)
3536
val verifyLegacy = VerifyLegacy(client.verifyClient)
3637
val voice = Voice(client.voiceClient)

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ abstract class AbstractTest {
5757
protected val text = "Hello, World!"
5858
protected val country = "GB"
5959
protected val secret = "ABCDEFGH01234abc"
60+
protected val cursor = "7EjDNQrAcipmOnc0HCzpQRkhBULzY44ljGUX4lXKyUIVfiZay5pv9wg="
61+
protected val vbcExt = "4321"
62+
protected val userName = "Sam_username"
6063
protected val sipUri = "sip:rebekka@sip.example.com"
64+
protected val websocketUri = "wss://example.com/socket"
65+
protected val wsContentType = "audio/l16;rate=8000"
6166
protected val clientRef = "my-personal-reference"
6267
protected val textHexEncoded = "48656c6c6f2c20576f726c6421"
6368
protected val entityId = "1101407360000017170"
@@ -84,6 +89,10 @@ abstract class AbstractTest {
8489
protected val statusCallbackUrl = "$callbackUrl/status"
8590
protected val moCallbackUrl = "$callbackUrl/inbound-sms"
8691
protected val drCallbackUrl = "$callbackUrl/delivery-receipt"
92+
protected val imageUrl = "$exampleUrlBase/image.jpg"
93+
protected val audioUrl = "$exampleUrlBase/audio.mp3"
94+
protected val videoUrl = "$exampleUrlBase/video.mp4"
95+
protected val fileUrl = "$exampleUrlBase/file.pdf"
8796

8897
private val port = 8081
8998
private val wiremock: WireMockServer = WireMockServer(
@@ -281,14 +290,15 @@ abstract class AbstractTest {
281290

282291
protected inline fun <reified E: VonageApiResponseException> assertApiResponseException(
283292
url: String, requestMethod: HttpMethod, actualCall: () -> Any, status: Int,
284-
errorType: String? = null, title: String? = null,
293+
errorType: String? = null, title: String? = null, code: String? = null,
285294
detail: String? = null, instance: String? = null): E {
286295

287296
val responseParams = mutableMapOf<String, Any>()
288297
if (errorType != null) responseParams["type"] = errorType
289298
if (title != null) responseParams["title"] = title
290299
if (detail != null) responseParams["detail"] = detail
291300
if (instance != null) responseParams["instance"] = instance
301+
if (code != null) responseParams["code"] = code
292302

293303
mockRequest(requestMethod, url).mockReturn(status, responseParams)
294304
val exception = assertThrows<E> { actualCall.invoke() }
@@ -298,6 +308,7 @@ abstract class AbstractTest {
298308
assertEquals(title, exception.title)
299309
assertEquals(instance, exception.instance)
300310
assertEquals(detail, exception.detail)
311+
assertEquals(code, exception.code)
301312
return exception
302313
}
303314

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ class MessagesTest : AbstractTest() {
3737
private val viberChannel = "viber_service"
3838
private val messengerChannel = "messenger"
3939
private val caption = "Additional text to accompany the media"
40-
private val imageUrl = "https://example.com/image.jpg"
41-
private val audioUrl = "https://example.com/audio.mp3"
42-
private val videoUrl = "https://example.com/video.mp4"
43-
private val fileUrl = "https://example.com/file.pdf"
4440
private val captionMap = mapOf("caption" to caption)
4541

4642

0 commit comments

Comments
 (0)