@@ -18,7 +18,14 @@ package com.vonage.client.kt
18
18
import com.vonage.client.HttpConfig
19
19
import com.vonage.client.VonageClient
20
20
21
+ /* *
22
+ * Denotes the version of the Vonage Kotlin SDK being used, in SemVer format.
23
+ */
21
24
const val VONAGE_KOTLIN_SDK_VERSION = " 1.1.2"
25
+
26
+ /* *
27
+ * The non-overridable user agent string used by the SDK.
28
+ */
22
29
private const val SDK_USER_AGENT = " vonage-kotlin-sdk/$VONAGE_KOTLIN_SDK_VERSION "
23
30
24
31
/* *
@@ -173,27 +180,34 @@ class Vonage(config: VonageClient.Builder.() -> Unit) {
173
180
* - **VONAGE_PRIVATE_KEY_PATH**: Path to the private key file of the application.
174
181
*/
175
182
fun VonageClient.Builder.authFromEnv (): VonageClient .Builder {
176
- val apiKey = System .getenv(" VONAGE_API_KEY" )
177
- val apiSecret = System .getenv(" VONAGE_API_SECRET" )
178
- val signatureSecret = System .getenv(" VONAGE_SIGNATURE_SECRET" )
179
- val applicationId = System .getenv(" VONAGE_APPLICATION_ID" )
180
- val privateKeyPath = System .getenv(" VONAGE_PRIVATE_KEY_PATH" )
181
- if (apiKey != null ) apiKey(apiKey)
182
- if (apiSecret != null ) apiSecret(apiSecret)
183
- if (signatureSecret != null ) signatureSecret(signatureSecret)
184
- if (applicationId != null ) applicationId(applicationId)
185
- if (privateKeyPath != null ) privateKeyPath(privateKeyPath)
183
+ setFromEnvIfNotNull(" VONAGE_API_KEY" ) { apiKey(it) }
184
+ setFromEnvIfNotNull(" VONAGE_API_SECRET" ) { apiSecret(it) }
185
+ setFromEnvIfNotNull(" VONAGE_SIGNATURE_SECRET" ) { signatureSecret(it) }
186
+ setFromEnvIfNotNull(" VONAGE_APPLICATION_ID" ) { applicationId(it) }
187
+ setFromEnvIfNotNull(" VONAGE_PRIVATE_KEY_PATH" ) { privateKeyPath(it) }
188
+ setFromEnvIfNotNull(" VONAGE_PRIVATE_KEY_CONTENTS" ) { privateKeyContents(it) }
186
189
return this
187
190
}
188
191
192
+ /* *
193
+ * Calls the setter function with the value of the environment variable if it is not null.
194
+ *
195
+ * @param envVar The name of the environment variable.
196
+ * @param setter The setter function to call with the value of the environment variable.
197
+ */
198
+ private fun setFromEnvIfNotNull (envVar : String , setter : (String ) -> Unit ) {
199
+ val value = System .getenv(envVar)
200
+ if (value != null ) setter(value)
201
+ }
202
+
189
203
/* *
190
204
* Optional HTTP configuration options for the client.
191
205
*
192
206
* @param init The config options lambda.
193
207
* @return The builder.
194
208
*/
195
209
fun VonageClient.Builder.httpConfig (init : HttpConfig .Builder .() -> Unit ): VonageClient .Builder {
196
- // Workaround to prevent the default prefix being overriden
210
+ // Workaround to prevent the default prefix being overridden
197
211
val applied = HttpConfig .builder().apply (init )
198
212
val customUa = applied.build().customUserAgent
199
213
val adjustedUa = if (customUa.isNullOrBlank()) SDK_USER_AGENT else " $SDK_USER_AGENT $customUa "
0 commit comments