Skip to content

Commit cdf3c5a

Browse files
authored
feat: Add Subaccounts API (#7)
1 parent 468cc54 commit cdf3c5a

File tree

9 files changed

+445
-4
lines changed

9 files changed

+445
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ 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+
## [0.8.0] - 2024-08-??
8+
9+
### Added
10+
- Subaccounts API
11+
712
## [0.7.0] - 2024-08-06
813

914
### Added

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ You'll need to have [created a Vonage account](https://dashboard.nexmo.com/sign-
2727
- [Redact](https://developer.vonage.com/en/redact/overview)
2828
- [SIM Swap](https://developer.vonage.com/en/sim-swap/overview)
2929
- [SMS](https://developer.vonage.com/en/messaging/sms/overview)
30+
- [Subaccounts](https://developer.vonage.com/en/account/subaccounts/overview)
3031
- [Verify](https://developer.vonage.com/en/verify/overview)
3132
- [Voice](https://developer.vonage.com/en/voice/voice-api/overview)
3233

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.subaccounts.*
19+
import com.vonage.client.subaccounts.Account
20+
import java.time.Instant
21+
22+
class Subaccounts internal constructor(private val client: SubaccountsClient) {
23+
24+
fun listSubaccounts(): ListSubaccountsResponse = client.listSubaccounts()
25+
26+
fun createSubaccount(name: String, secret: String? = null, usePrimaryAccountBalance: Boolean? = null): Account {
27+
val builder = CreateSubaccountRequest.builder().name(name).secret(secret)
28+
if (usePrimaryAccountBalance != null) {
29+
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
30+
}
31+
return client.createSubaccount(builder.build())
32+
}
33+
34+
fun getSubaccount(subaccountKey: String): Account = client.getSubaccount(subaccountKey)
35+
36+
fun updateSubaccount(subaccountKey: String, name: String? = null,
37+
usePrimaryAccountBalance: Boolean? = null, suspend: Boolean? = null): Account {
38+
val builder = UpdateSubaccountRequest.builder(subaccountKey)
39+
if (name != null) {
40+
builder.name(name)
41+
}
42+
if (usePrimaryAccountBalance != null) {
43+
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
44+
}
45+
if (suspend != null) {
46+
builder.suspended(suspend)
47+
}
48+
return client.updateSubaccount(builder.build())
49+
}
50+
51+
fun listCreditTransfers(startDate: Instant? = null, endDate: Instant? = null,
52+
subaccount: String? = null): List<MoneyTransfer> =
53+
client.listCreditTransfers(ListTransfersFilter.builder()
54+
.startDate(startDate).endDate(endDate).subaccount(subaccount).build()
55+
)
56+
57+
fun listBalanceTransfers(startDate: Instant? = null, endDate: Instant? = null,
58+
subaccount: String? = null): List<MoneyTransfer> =
59+
client.listBalanceTransfers(ListTransfersFilter.builder()
60+
.startDate(startDate).endDate(endDate).subaccount(subaccount).build()
61+
)
62+
63+
fun transferCredit(from: String, to: String, amount: Double, ref: String? = null): MoneyTransfer =
64+
client.transferCredit(MoneyTransfer.builder().from(from).to(to).amount(amount).reference(ref).build())
65+
66+
fun transferBalance(from: String, to: String, amount: Double, ref: String? = null): MoneyTransfer =
67+
client.transferBalance(MoneyTransfer.builder().from(from).to(to).amount(amount).reference(ref).build())
68+
69+
fun transferNumber(from: String, to: String, number: String, country: String): NumberTransfer =
70+
client.transferNumber(NumberTransfer.builder().from(from).to(to).number(number).country(country).build())
71+
72+
}

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

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

30-
inner class ExistingRequest internal constructor(private val requestId: UUID) {
30+
inner class ExistingRequest internal constructor(val requestId: UUID) {
3131

3232
fun cancel(): Unit = client.cancelVerification(requestId)
3333

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Vonage(init: VonageClient.Builder.() -> Unit) {
2929
val redact = Redact(client.redactClient)
3030
val simSwap = SimSwap(client.simSwapClient)
3131
val sms = Sms(client.smsClient)
32+
val subaccounts = Subaccounts(client.subaccountsClient)
3233
val verify = Verify(client.verify2Client)
3334
val verifyLegacy = VerifyLegacy(client.verifyClient)
3435
val voice = Voice(client.voiceClient)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ abstract class AbstractTest {
5555
protected val altNumber = "447700900001"
5656
protected val brand = "Nexmo KT"
5757
protected val text = "Hello, World!"
58+
protected val country = "GB"
59+
protected val secret = "ABCDEFGH01234abc"
5860
protected val sipUri = "sip:rebekka@sip.example.com"
5961
protected val clientRef = "my-personal-reference"
6062
protected val textHexEncoded = "48656c6c6f2c20576f726c6421"
@@ -242,7 +244,7 @@ abstract class AbstractTest {
242244
protected fun mockPatch(expectedUrl: String, expectedRequestParams: Map<String, Any>? = null,
243245
status: Int = 200, contentType: ContentType? = ContentType.APPLICATION_JSON,
244246
authType: AuthType? = AuthType.JWT, expectedResponseParams: Map<String, Any>? = null) =
245-
mockP(HttpMethod.PUT, expectedUrl, expectedRequestParams, status, authType, contentType, expectedResponseParams)
247+
mockP(HttpMethod.PATCH, expectedUrl, expectedRequestParams, status, authType, contentType, expectedResponseParams)
246248

247249
protected fun mockDelete(expectedUrl: String, authType: AuthType? = null,
248250
expectedResponseParams: Map<String, Any>? = null) =

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class AccountTest : AbstractTest() {
2424
private val account = vonage.account
2525
private val authType = AuthType.API_KEY_SECRET_HEADER
2626
private val secretId = "ad6dc56f-07b5-46e1-a527-85530e625800"
27-
private val secret = "ABCDEFGH01234abc"
2827
private val trx = "8ef2447e69604f642ae59363aa5f781b"
2928
private val baseUrl = "/account"
3029
private val secretsUrl = "${baseUrl}s/$apiKey/secrets"

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import kotlin.test.*
2424
class NumbersTest : AbstractTest() {
2525
private val client = vonage.numbers
2626
private val authType = AuthType.API_KEY_SECRET_HEADER
27-
private val country = "GB"
2827
private val targetApiKey = "1a2345b7"
2928
private val moSmppSysType = "inbound"
3029
private val buyEndpoint = "buy"

0 commit comments

Comments
 (0)