Skip to content

Commit

Permalink
feat: Add Subaccounts API (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani authored Aug 8, 2024
1 parent 468cc54 commit cdf3c5a
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ 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/).

## [0.8.0] - 2024-08-??

### Added
- Subaccounts API

## [0.7.0] - 2024-08-06

### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ You'll need to have [created a Vonage account](https://dashboard.nexmo.com/sign-
- [Redact](https://developer.vonage.com/en/redact/overview)
- [SIM Swap](https://developer.vonage.com/en/sim-swap/overview)
- [SMS](https://developer.vonage.com/en/messaging/sms/overview)
- [Subaccounts](https://developer.vonage.com/en/account/subaccounts/overview)
- [Verify](https://developer.vonage.com/en/verify/overview)
- [Voice](https://developer.vonage.com/en/voice/voice-api/overview)

Expand Down
72 changes: 72 additions & 0 deletions src/main/kotlin/com/vonage/client/kt/Subaccounts.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.subaccounts.*
import com.vonage.client.subaccounts.Account
import java.time.Instant

class Subaccounts internal constructor(private val client: SubaccountsClient) {

fun listSubaccounts(): ListSubaccountsResponse = client.listSubaccounts()

fun createSubaccount(name: String, secret: String? = null, usePrimaryAccountBalance: Boolean? = null): Account {
val builder = CreateSubaccountRequest.builder().name(name).secret(secret)
if (usePrimaryAccountBalance != null) {
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
}
return client.createSubaccount(builder.build())
}

fun getSubaccount(subaccountKey: String): Account = client.getSubaccount(subaccountKey)

fun updateSubaccount(subaccountKey: String, name: String? = null,
usePrimaryAccountBalance: Boolean? = null, suspend: Boolean? = null): Account {
val builder = UpdateSubaccountRequest.builder(subaccountKey)
if (name != null) {
builder.name(name)
}
if (usePrimaryAccountBalance != null) {
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
}
if (suspend != null) {
builder.suspended(suspend)
}
return client.updateSubaccount(builder.build())
}

fun listCreditTransfers(startDate: Instant? = null, endDate: Instant? = null,
subaccount: String? = null): List<MoneyTransfer> =
client.listCreditTransfers(ListTransfersFilter.builder()
.startDate(startDate).endDate(endDate).subaccount(subaccount).build()
)

fun listBalanceTransfers(startDate: Instant? = null, endDate: Instant? = null,
subaccount: String? = null): List<MoneyTransfer> =
client.listBalanceTransfers(ListTransfersFilter.builder()
.startDate(startDate).endDate(endDate).subaccount(subaccount).build()
)

fun transferCredit(from: String, to: String, amount: Double, ref: String? = null): MoneyTransfer =
client.transferCredit(MoneyTransfer.builder().from(from).to(to).amount(amount).reference(ref).build())

fun transferBalance(from: String, to: String, amount: Double, ref: String? = null): MoneyTransfer =
client.transferBalance(MoneyTransfer.builder().from(from).to(to).amount(amount).reference(ref).build())

fun transferNumber(from: String, to: String, number: String, country: String): NumberTransfer =
client.transferNumber(NumberTransfer.builder().from(from).to(to).number(number).country(country).build())

}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/vonage/client/kt/Verify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Verify(private val client: Verify2Client) {
VerificationRequest.builder().brand(brand).apply(init).build()
)

inner class ExistingRequest internal constructor(private val requestId: UUID) {
inner class ExistingRequest internal constructor(val requestId: UUID) {

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

Expand Down
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 @@ -29,6 +29,7 @@ class Vonage(init: VonageClient.Builder.() -> Unit) {
val redact = Redact(client.redactClient)
val simSwap = SimSwap(client.simSwapClient)
val sms = Sms(client.smsClient)
val subaccounts = Subaccounts(client.subaccountsClient)
val verify = Verify(client.verify2Client)
val verifyLegacy = VerifyLegacy(client.verifyClient)
val voice = Voice(client.voiceClient)
Expand Down
4 changes: 3 additions & 1 deletion src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ abstract class AbstractTest {
protected val altNumber = "447700900001"
protected val brand = "Nexmo KT"
protected val text = "Hello, World!"
protected val country = "GB"
protected val secret = "ABCDEFGH01234abc"
protected val sipUri = "sip:rebekka@sip.example.com"
protected val clientRef = "my-personal-reference"
protected val textHexEncoded = "48656c6c6f2c20576f726c6421"
Expand Down Expand Up @@ -242,7 +244,7 @@ abstract class AbstractTest {
protected fun mockPatch(expectedUrl: String, expectedRequestParams: Map<String, Any>? = null,
status: Int = 200, contentType: ContentType? = ContentType.APPLICATION_JSON,
authType: AuthType? = AuthType.JWT, expectedResponseParams: Map<String, Any>? = null) =
mockP(HttpMethod.PUT, expectedUrl, expectedRequestParams, status, authType, contentType, expectedResponseParams)
mockP(HttpMethod.PATCH, expectedUrl, expectedRequestParams, status, authType, contentType, expectedResponseParams)

protected fun mockDelete(expectedUrl: String, authType: AuthType? = null,
expectedResponseParams: Map<String, Any>? = null) =
Expand Down
1 change: 0 additions & 1 deletion src/test/kotlin/com/vonage/client/kt/AccountTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class AccountTest : AbstractTest() {
private val account = vonage.account
private val authType = AuthType.API_KEY_SECRET_HEADER
private val secretId = "ad6dc56f-07b5-46e1-a527-85530e625800"
private val secret = "ABCDEFGH01234abc"
private val trx = "8ef2447e69604f642ae59363aa5f781b"
private val baseUrl = "/account"
private val secretsUrl = "${baseUrl}s/$apiKey/secrets"
Expand Down
1 change: 0 additions & 1 deletion src/test/kotlin/com/vonage/client/kt/NumbersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import kotlin.test.*
class NumbersTest : AbstractTest() {
private val client = vonage.numbers
private val authType = AuthType.API_KEY_SECRET_HEADER
private val country = "GB"
private val targetApiKey = "1a2345b7"
private val moSmppSysType = "inbound"
private val buyEndpoint = "buy"
Expand Down
Loading

0 comments on commit cdf3c5a

Please sign in to comment.