Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Users #9

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.9.0] - 2024-08-2?

### Added
- Application API
- Application & Users API

## [0.8.0] - 2024-08-09

Expand Down
61 changes: 61 additions & 0 deletions src/main/kotlin/com/vonage/client/kt/Users.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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

import com.vonage.client.users.*

class Users internal constructor(private val client: UsersClient) {

fun user(userId: String): ExistingUser = ExistingUser(userId)

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

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

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

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

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

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

fun create(properties: User.Builder.() -> Unit): User =
client.createUser(User.builder().apply(properties).build())

fun list(filter: (ListUsersRequest.Builder.() -> Unit)? = null): ListUsersResponse {
val request = ListUsersRequest.builder()
if (filter == null) {
request.pageSize(100)
}
else {
request.apply(filter)
}
return client.listUsers(request.build())
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/com/vonage/client/kt/Vonage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Vonage(init: VonageClient.Builder.() -> Unit) {
val simSwap = SimSwap(client.simSwapClient)
val sms = Sms(client.smsClient)
val subaccounts = Subaccounts(client.subaccountsClient)
val users = Users(client.usersClient)
val verify = Verify(client.verify2Client)
val verifyLegacy = VerifyLegacy(client.verifyClient)
val voice = Voice(client.voiceClient)
Expand Down
13 changes: 12 additions & 1 deletion src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ abstract class AbstractTest {
protected val text = "Hello, World!"
protected val country = "GB"
protected val secret = "ABCDEFGH01234abc"
protected val cursor = "7EjDNQrAcipmOnc0HCzpQRkhBULzY44ljGUX4lXKyUIVfiZay5pv9wg="
protected val vbcExt = "4321"
protected val userName = "Sam_username"
protected val sipUri = "sip:rebekka@sip.example.com"
protected val websocketUri = "wss://example.com/socket"
protected val wsContentType = "audio/l16;rate=8000"
protected val clientRef = "my-personal-reference"
protected val textHexEncoded = "48656c6c6f2c20576f726c6421"
protected val entityId = "1101407360000017170"
Expand All @@ -84,6 +89,10 @@ abstract class AbstractTest {
protected val statusCallbackUrl = "$callbackUrl/status"
protected val moCallbackUrl = "$callbackUrl/inbound-sms"
protected val drCallbackUrl = "$callbackUrl/delivery-receipt"
protected val imageUrl = "$exampleUrlBase/image.jpg"
protected val audioUrl = "$exampleUrlBase/audio.mp3"
protected val videoUrl = "$exampleUrlBase/video.mp4"
protected val fileUrl = "$exampleUrlBase/file.pdf"

private val port = 8081
private val wiremock: WireMockServer = WireMockServer(
Expand Down Expand Up @@ -281,14 +290,15 @@ abstract class AbstractTest {

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

val responseParams = mutableMapOf<String, Any>()
if (errorType != null) responseParams["type"] = errorType
if (title != null) responseParams["title"] = title
if (detail != null) responseParams["detail"] = detail
if (instance != null) responseParams["instance"] = instance
if (code != null) responseParams["code"] = code

mockRequest(requestMethod, url).mockReturn(status, responseParams)
val exception = assertThrows<E> { actualCall.invoke() }
Expand All @@ -298,6 +308,7 @@ abstract class AbstractTest {
assertEquals(title, exception.title)
assertEquals(instance, exception.instance)
assertEquals(detail, exception.detail)
assertEquals(code, exception.code)
return exception
}

Expand Down
4 changes: 0 additions & 4 deletions src/test/kotlin/com/vonage/client/kt/MessagesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ class MessagesTest : AbstractTest() {
private val viberChannel = "viber_service"
private val messengerChannel = "messenger"
private val caption = "Additional text to accompany the media"
private val imageUrl = "https://example.com/image.jpg"
private val audioUrl = "https://example.com/audio.mp3"
private val videoUrl = "https://example.com/video.mp4"
private val fileUrl = "https://example.com/file.pdf"
private val captionMap = mapOf("caption" to caption)


Expand Down
Loading