Skip to content

Commit 3fbc8cc

Browse files
authored
feat: Add Account API (#6)
1 parent 04b360a commit 3fbc8cc

File tree

9 files changed

+356
-24
lines changed

9 files changed

+356
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88

99
### Added
1010
- Numbers API
11+
- Account API
1112

1213
### Changed
1314
- Explicit return types for all methods

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ You'll need to have [created a Vonage account](https://dashboard.nexmo.com/sign-
1818
* [Contribute!](#contribute)
1919

2020
## Supported APIs
21+
- [Account](https://developer.vonage.com/en/account/overview)
2122
- [Conversion](https://developer.vonage.com/en/messaging/conversion-api/overview)
2223
- [Messages](https://developer.vonage.com/en/messages/overview)
2324
- [Number Insight](https://developer.vonage.com/en/number-insight/overview)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.account.*
19+
20+
class Account internal constructor(private val accountClient: AccountClient) {
21+
22+
fun getBalance(): BalanceResponse = accountClient.balance
23+
24+
fun topUp(transactionId: String): Unit = accountClient.topUp(transactionId)
25+
26+
fun updateSettings(incomingSmsUrl: String? = null, deliverReceiptUrl: String? = null): SettingsResponse =
27+
accountClient.updateSettings(SettingsRequest(incomingSmsUrl, deliverReceiptUrl))
28+
29+
fun secrets(apiKey: String? = null): Secrets = Secrets(apiKey)
30+
31+
inner class Secrets internal constructor(val apiKey: String? = null) {
32+
33+
fun list(): List<SecretResponse> = (
34+
if (apiKey == null) accountClient.listSecrets()
35+
else accountClient.listSecrets(apiKey)
36+
).secrets
37+
38+
fun create(secret: String): SecretResponse =
39+
if (apiKey == null) accountClient.createSecret(secret)
40+
else accountClient.createSecret(apiKey, secret)
41+
42+
fun get(secretId: String): SecretResponse =
43+
if (apiKey == null) accountClient.getSecret(secretId)
44+
else accountClient.getSecret(apiKey, secretId)
45+
46+
fun delete(secretId: String): Unit =
47+
if (apiKey == null) accountClient.revokeSecret(secretId)
48+
else accountClient.revokeSecret(apiKey, secretId)
49+
}
50+
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ import com.vonage.client.HttpConfig
1919
import com.vonage.client.VonageClient
2020

2121
class Vonage(init: VonageClient.Builder.() -> Unit) {
22-
private val vonageClient : VonageClient = VonageClient.builder().apply(init).build();
23-
val messages = Messages(vonageClient.messagesClient)
24-
val verify = Verify(vonageClient.verify2Client)
25-
val voice = Voice(vonageClient.voiceClient)
26-
val sms = Sms(vonageClient.smsClient)
22+
private val vonageClient : VonageClient = VonageClient.builder().apply(init).build()
23+
val account = Account(vonageClient.accountClient)
2724
val conversion = Conversion(vonageClient.conversionClient)
28-
val redact = Redact(vonageClient.redactClient)
29-
val verifyLegacy = VerifyLegacy(vonageClient.verifyClient)
25+
val messages = Messages(vonageClient.messagesClient)
3026
val numberInsight = NumberInsight(vonageClient.insightClient)
3127
val numbers = Numbers(vonageClient.numbersClient)
3228
val numberVerification = NumberVerification(vonageClient.numberVerificationClient)
29+
val redact = Redact(vonageClient.redactClient)
3330
val simSwap = SimSwap(vonageClient.simSwapClient)
31+
val sms = Sms(vonageClient.smsClient)
32+
val verify = Verify(vonageClient.verify2Client)
33+
val verifyLegacy = VerifyLegacy(vonageClient.verifyClient)
34+
val voice = Voice(vonageClient.voiceClient)
35+
3436
}
3537

3638
fun VonageClient.Builder.authFromEnv(): VonageClient.Builder {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import kotlin.test.assertEquals
3636

3737
abstract class AbstractTest {
3838
protected val apiKey = "a1b2c3d4"
39+
protected val apiKey2 = "f9e8d7c6"
3940
protected val applicationId = "00000000-0000-4000-8000-000000000000"
4041
protected val accessToken = "abc123456def"
4142
private val apiSecret = "1234567890abcdef"
@@ -77,6 +78,9 @@ abstract class AbstractTest {
7778
protected val currency = "EUR"
7879
protected val exampleUrlBase = "https://example.com"
7980
protected val callbackUrl = "$exampleUrlBase/callback"
81+
protected val statusCallbackUrl = "$callbackUrl/status"
82+
protected val moCallbackUrl = "$callbackUrl/inbound-sms"
83+
protected val drCallbackUrl = "$callbackUrl/delivery-receipt"
8084

8185
private val port = 8081
8286
private val wiremock: WireMockServer = WireMockServer(
@@ -102,9 +106,12 @@ abstract class AbstractTest {
102106
wiremock.stop()
103107
}
104108

105-
protected fun strToDate(dateStr: String): Date =
109+
private fun strToDate(dateStr: String): Date =
106110
Date(Instant.parse(dateStr.replace(' ', 'T') + 'Z').toEpochMilli())
107111

112+
protected fun linksSelfHref(url: String = "$exampleUrlBase/self"): Map<String, Any> =
113+
mapOf("_links" to mapOf("self" to mapOf("href" to url)))
114+
108115
protected enum class ContentType(val mime: String) {
109116
APPLICATION_JSON("application/json"),
110117
FORM_URLENCODED("application/x-www-form-urlencoded");
@@ -264,6 +271,7 @@ abstract class AbstractTest {
264271
protected inline fun <reified E: VonageApiResponseException> assertApiResponseException(
265272
url: String, requestMethod: HttpMethod, actualCall: () -> Any) {
266273

274+
assert401ApiResponseException<E>(url, requestMethod, actualCall)
267275
assert402ApiResponseException<E>(url, requestMethod, actualCall)
268276
assert429ApiResponseException<E>(url, requestMethod, actualCall)
269277
}
@@ -290,6 +298,16 @@ abstract class AbstractTest {
290298
return exception
291299
}
292300

301+
protected inline fun <reified E: VonageApiResponseException> assert401ApiResponseException(
302+
url: String, requestMethod: HttpMethod, actualCall: () -> Any): E =
303+
304+
assertApiResponseException(url, requestMethod, actualCall, 401,
305+
"https://developer.nexmo.com/api-errors#unauthorized",
306+
"Unauthorized",
307+
"You did not provide correct credentials.",
308+
"bf0ca0bf927b3b52e3cb03217e1a1ddf"
309+
)
310+
293311
protected inline fun <reified E: VonageApiResponseException> assert402ApiResponseException(
294312
url: String, requestMethod: HttpMethod, actualCall: () -> Any): E =
295313

0 commit comments

Comments
 (0)