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 Account API #6

Merged
merged 3 commits into from
Aug 6, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added
- Numbers API
- Account API

### Changed
- Explicit return types for all methods
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ You'll need to have [created a Vonage account](https://dashboard.nexmo.com/sign-
* [Contribute!](#contribute)

## Supported APIs
- [Account](https://developer.vonage.com/en/account/overview)
- [Conversion](https://developer.vonage.com/en/messaging/conversion-api/overview)
- [Messages](https://developer.vonage.com/en/messages/overview)
- [Number Insight](https://developer.vonage.com/en/number-insight/overview)
Expand Down
50 changes: 50 additions & 0 deletions src/main/kotlin/com/vonage/client/kt/Account.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.account.*

class Account internal constructor(private val accountClient: AccountClient) {

fun getBalance(): BalanceResponse = accountClient.balance

fun topUp(transactionId: String): Unit = accountClient.topUp(transactionId)

fun updateSettings(incomingSmsUrl: String? = null, deliverReceiptUrl: String? = null): SettingsResponse =
accountClient.updateSettings(SettingsRequest(incomingSmsUrl, deliverReceiptUrl))

fun secrets(apiKey: String? = null): Secrets = Secrets(apiKey)

inner class Secrets internal constructor(val apiKey: String? = null) {

fun list(): List<SecretResponse> = (
if (apiKey == null) accountClient.listSecrets()
else accountClient.listSecrets(apiKey)
).secrets

fun create(secret: String): SecretResponse =
if (apiKey == null) accountClient.createSecret(secret)
else accountClient.createSecret(apiKey, secret)

fun get(secretId: String): SecretResponse =
if (apiKey == null) accountClient.getSecret(secretId)
else accountClient.getSecret(apiKey, secretId)

fun delete(secretId: String): Unit =
if (apiKey == null) accountClient.revokeSecret(secretId)
else accountClient.revokeSecret(apiKey, secretId)
}
}
16 changes: 9 additions & 7 deletions src/main/kotlin/com/vonage/client/kt/Vonage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ import com.vonage.client.HttpConfig
import com.vonage.client.VonageClient

class Vonage(init: VonageClient.Builder.() -> Unit) {
private val vonageClient : VonageClient = VonageClient.builder().apply(init).build();
val messages = Messages(vonageClient.messagesClient)
val verify = Verify(vonageClient.verify2Client)
val voice = Voice(vonageClient.voiceClient)
val sms = Sms(vonageClient.smsClient)
private val vonageClient : VonageClient = VonageClient.builder().apply(init).build()
val account = Account(vonageClient.accountClient)
val conversion = Conversion(vonageClient.conversionClient)
val redact = Redact(vonageClient.redactClient)
val verifyLegacy = VerifyLegacy(vonageClient.verifyClient)
val messages = Messages(vonageClient.messagesClient)
val numberInsight = NumberInsight(vonageClient.insightClient)
val numbers = Numbers(vonageClient.numbersClient)
val numberVerification = NumberVerification(vonageClient.numberVerificationClient)
val redact = Redact(vonageClient.redactClient)
val simSwap = SimSwap(vonageClient.simSwapClient)
val sms = Sms(vonageClient.smsClient)
val verify = Verify(vonageClient.verify2Client)
val verifyLegacy = VerifyLegacy(vonageClient.verifyClient)
val voice = Voice(vonageClient.voiceClient)

}

fun VonageClient.Builder.authFromEnv(): VonageClient.Builder {
Expand Down
20 changes: 19 additions & 1 deletion src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import kotlin.test.assertEquals

abstract class AbstractTest {
protected val apiKey = "a1b2c3d4"
protected val apiKey2 = "f9e8d7c6"
protected val applicationId = "00000000-0000-4000-8000-000000000000"
protected val accessToken = "abc123456def"
private val apiSecret = "1234567890abcdef"
Expand Down Expand Up @@ -77,6 +78,9 @@ abstract class AbstractTest {
protected val currency = "EUR"
protected val exampleUrlBase = "https://example.com"
protected val callbackUrl = "$exampleUrlBase/callback"
protected val statusCallbackUrl = "$callbackUrl/status"
protected val moCallbackUrl = "$callbackUrl/inbound-sms"
protected val drCallbackUrl = "$callbackUrl/delivery-receipt"

private val port = 8081
private val wiremock: WireMockServer = WireMockServer(
Expand All @@ -102,9 +106,12 @@ abstract class AbstractTest {
wiremock.stop()
}

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

protected fun linksSelfHref(url: String = "$exampleUrlBase/self"): Map<String, Any> =
mapOf("_links" to mapOf("self" to mapOf("href" to url)))

protected enum class ContentType(val mime: String) {
APPLICATION_JSON("application/json"),
FORM_URLENCODED("application/x-www-form-urlencoded");
Expand Down Expand Up @@ -264,6 +271,7 @@ abstract class AbstractTest {
protected inline fun <reified E: VonageApiResponseException> assertApiResponseException(
url: String, requestMethod: HttpMethod, actualCall: () -> Any) {

assert401ApiResponseException<E>(url, requestMethod, actualCall)
assert402ApiResponseException<E>(url, requestMethod, actualCall)
assert429ApiResponseException<E>(url, requestMethod, actualCall)
}
Expand All @@ -290,6 +298,16 @@ abstract class AbstractTest {
return exception
}

protected inline fun <reified E: VonageApiResponseException> assert401ApiResponseException(
url: String, requestMethod: HttpMethod, actualCall: () -> Any): E =

assertApiResponseException(url, requestMethod, actualCall, 401,
"https://developer.nexmo.com/api-errors#unauthorized",
"Unauthorized",
"You did not provide correct credentials.",
"bf0ca0bf927b3b52e3cb03217e1a1ddf"
)

protected inline fun <reified E: VonageApiResponseException> assert402ApiResponseException(
url: String, requestMethod: HttpMethod, actualCall: () -> Any): E =

Expand Down
Loading