Skip to content

Commit

Permalink
fix: Support custom user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Dec 12, 2024
1 parent e962031 commit 1cdee62
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ 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/).

## [1.1.2] - 2024-12-12

### Fixed
- `HttpConfig.appendUserAgent` now works as expected in the SDK rather than being overridden

### Removed
- `Application.Builder.removeCapabilities`, since it was added in Java SDK 8.14.0

## [1.1.1] - 2024-12-03
- Bumped Java SDK version to 8.15.0

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ See all of our SDKs and integrations on the [Vonage Developer portal](https://de
## Installation
Releases are published to [Maven Central](https://central.sonatype.com/artifact/com.vonage/server-sdk-kotlin).
Instructions for your build system can be found in the snippets section.
They're also available from [here](https://search.maven.org/artifact/com.vonage/server-sdk-kotlin/1.1.1/jar).
They're also available from [here](https://search.maven.org/artifact/com.vonage/server-sdk-kotlin/1.1.2/jar).
Release notes for each version can be found in the [changelog](CHANGELOG.md).

Here are the instructions for including the SDK in your project:
Expand All @@ -63,7 +63,7 @@ Add the following to your `build.gradle` or `build.gradle.kts` file:

```groovy
dependencies {
implementation("com.vonage:server-sdk-kotlin:1.1.1")
implementation("com.vonage:server-sdk-kotlin:1.1.2")
}
```

Expand All @@ -74,7 +74,7 @@ Add the following to the `<dependencies>` section of your `pom.xml` file:
<dependency>
<groupId>com.vonage</groupId>
<artifactId>server-sdk-kotlin</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</dependency>
```

Expand Down Expand Up @@ -160,8 +160,8 @@ including [**a searchable list of snippets**](https://github.com/Vonage/vonage-k

The SDK is fully documented with [KDocs](https://kotlinlang.org/docs/kotlin-doc.html), so you should have complete
documentation from your IDE. You may need to click "Download Sources" in IntelliJ to get the full documentation.
Alternatively, you can browse the documentation using a service like [Javadoc.io](https://javadoc.io/doc/com.vonage/server-sdk-kotlin/1.1.1/index.html),
which renders the documentation for you from [the artifacts on Maven Central](https://repo.maven.apache.org/maven2/com/vonage/server-sdk-kotlin/1.1.1/).
Alternatively, you can browse the documentation using a service like [Javadoc.io](https://javadoc.io/doc/com.vonage/server-sdk-kotlin/1.1.2/index.html),
which renders the documentation for you from [the artifacts on Maven Central](https://repo.maven.apache.org/maven2/com/vonage/server-sdk-kotlin/1.1.2/).

For help with any specific APIs, refer to the relevant documentation on our [developer portal](https://developer.vonage.com/en/documentation),
using the links provided in the [Supported APIs](#supported-apis) section. For completeness, you can also consult the
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.vonage</groupId>
<artifactId>server-sdk-kotlin</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>

<name>Vonage Kotlin Server SDK</name>
<description>Kotlin client for Vonage APIs</description>
Expand Down
15 changes: 0 additions & 15 deletions src/main/kotlin/com/vonage/client/kt/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,6 @@ fun Messages.Builder.inbound(properties: Webhook.Builder.() -> Unit): Messages.B
fun Messages.Builder.status(properties: Webhook.Builder.() -> Unit): Messages.Builder =
addWebhook(Webhook.Type.STATUS, webhookBuilder(properties))

/**
* Removes one or more capabilities from the application, used in conjunction with
* [com.vonage.client.kt.Application.ExistingApplication#update].
*
* @param capabilities The capability types to remove.
*
* @return The application builder.
*/
fun Application.Builder.removeCapabilities(vararg capabilities: Capability.Type): Application.Builder {
for (capability in capabilities) {
removeCapability(capability)
}
return this
}

/**
* Adds the [Voice] capability to the application.
*
Expand Down
12 changes: 9 additions & 3 deletions src/main/kotlin/com/vonage/client/kt/Vonage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.vonage.client.kt
import com.vonage.client.HttpConfig
import com.vonage.client.VonageClient

const val VONAGE_KOTLIN_SDK_VERSION = "1.1.1"
const val VONAGE_KOTLIN_SDK_VERSION = "1.1.2"
private const val SDK_USER_AGENT = "vonage-kotlin-sdk/$VONAGE_KOTLIN_SDK_VERSION"

/**
Expand All @@ -37,6 +37,7 @@ private const val SDK_USER_AGENT = "vonage-kotlin-sdk/$VONAGE_KOTLIN_SDK_VERSION
* @param config The configuration lambda, where you provide your Vonage account credentials.
*/
class Vonage(config: VonageClient.Builder.() -> Unit) {
// This ensures the user agent is always applied on top of the underlying Java SDK's user agent
private val client : VonageClient = VonageClient.builder().httpConfig{}.apply(config).build()

/**
Expand Down Expand Up @@ -191,5 +192,10 @@ fun VonageClient.Builder.authFromEnv(): VonageClient.Builder {
* @param init The config options lambda.
* @return The builder.
*/
fun VonageClient.Builder.httpConfig(init: HttpConfig.Builder.() -> Unit): VonageClient.Builder =
httpConfig(HttpConfig.builder().apply(init).appendUserAgent(SDK_USER_AGENT).build())
fun VonageClient.Builder.httpConfig(init: HttpConfig.Builder.() -> Unit): VonageClient.Builder {
// Workaround to prevent the default prefix being overriden
val applied = HttpConfig.builder().apply(init)
val customUa = applied.build().customUserAgent
val adjustedUa = if (customUa.isNullOrBlank()) SDK_USER_AGENT else "$SDK_USER_AGENT $customUa"
return httpConfig(applied.appendUserAgent(adjustedUa).build())
}
25 changes: 14 additions & 11 deletions src/test/kotlin/com/vonage/client/kt/VonageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ class VonageTest {
}

@Test
fun `test user agent is not overriden`() {
fun `Ensure that custom user agent does not override SDK's default prefix`() {
val expectedDefaultUa = "vonage-kotlin-sdk/$VONAGE_KOTLIN_SDK_VERSION"
var client = Vonage { }
assertEquals(expectedDefaultUa, client.getWrapper().httpConfig.customUserAgent)

val timeout = 36000
val customUa = "MyCustomUserAgent"
val client = Vonage { httpConfig { timeoutMillis(timeout); appendUserAgent(customUa) } }
val wrapper = FieldUtils.readField(
client::class.java.getDeclaredField("client").apply { isAccessible = true }.get(client),
val customUa = "My_Custom User-Agent"
client = Vonage { httpConfig { timeoutMillis(timeout); appendUserAgent(customUa) } }
assertEquals(timeout, client.getWrapper().httpConfig.timeoutMillis)
assertEquals("$expectedDefaultUa $customUa", client.getWrapper().httpConfig.customUserAgent)
}

private fun Vonage.getWrapper(): HttpWrapper {
return FieldUtils.readField(
this::class.java.getDeclaredField("client").apply { isAccessible = true }.get(this),
"httpWrapper", true
) as HttpWrapper

assertEquals(
"vonage-kotlin-sdk/$VONAGE_KOTLIN_SDK_VERSION",
wrapper.httpConfig.customUserAgent
)
assertEquals(timeout, wrapper.httpConfig.timeoutMillis)
}
}

0 comments on commit 1cdee62

Please sign in to comment.