From b0fbff53186f53ba4ad2793ebb4d43869db3bb02 Mon Sep 17 00:00:00 2001 From: Dana AlTayeh Date: Tue, 26 Nov 2024 17:13:22 +0300 Subject: [PATCH] update based on comments --- .../sdk/core/client/BaseRapidClient.kt | 17 ++----------- .../sdk/core/client/BaseXapClient.kt | 17 ++----------- .../expediagroup/sdk/core/client/Client.kt | 24 +++++++++++++++++++ .../core/configuration/ClientConfiguration.kt | 5 +++- .../ExpediaGroupClientConfiguration.kt | 3 +++ .../configuration/RapidClientConfiguration.kt | 2 +- .../configuration/XapClientConfiguration.kt | 2 +- .../collector/ConfigurationCollector.kt | 2 ++ .../provider/ConfigurationProvider.kt | 5 ++++ .../provider/RuntimeConfigurationProvider.kt | 4 +++- .../ExpediaGroupClientConfigurationTest.kt | 2 ++ .../expediagroup-sdk/client.mustache | 2 +- 12 files changed, 50 insertions(+), 35 deletions(-) diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/client/BaseRapidClient.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/client/BaseRapidClient.kt index 2de1f9b73..52d20daa3 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/client/BaseRapidClient.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/client/BaseRapidClient.kt @@ -23,7 +23,6 @@ import com.expediagroup.sdk.core.plugin.authentication.strategy.AuthenticationSt import io.ktor.client.HttpClient import io.ktor.client.engine.HttpClientEngine import io.ktor.client.engine.okhttp.OkHttp -import okhttp3.OkHttpClient /** * The integration point between the SDK Core and the product SDKs. @@ -42,7 +41,7 @@ abstract class BaseRapidClient( RapidConfigurationProvider ) - private val engine: HttpClientEngine = clientConfiguration.okHttpClient?.let { + private val engine: HttpClientEngine = _configurationProvider.okHttpClient?.let { OkHttp.create { preconfigured = it } @@ -66,17 +65,5 @@ abstract class BaseRapidClient( /** A [BaseRapidClient] builder with ability to pass a custom okhttp client. */ @Suppress("unused", "UnnecessaryAbstractClass") // This is used by the generated SDK clients. - abstract class BuilderWithHttpClient> : Client.Builder() { - protected var okHttpClient: OkHttpClient? = null - - @Suppress("UNCHECKED_CAST") - override fun self(): SELF = this as SELF - - /** Sets the [OkHttpClient] to use for the [BaseRapidClient]. */ - fun okHttpClient(okHttpClient: OkHttpClient): SELF { - this.okHttpClient = okHttpClient - return self() - } - - } + abstract class BuilderWithHttpClient> : Client.BuilderWithHttpClient() } diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/client/BaseXapClient.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/client/BaseXapClient.kt index 607dae8f3..19ef01d16 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/client/BaseXapClient.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/client/BaseXapClient.kt @@ -23,7 +23,6 @@ import com.expediagroup.sdk.core.plugin.authentication.strategy.AuthenticationSt import io.ktor.client.HttpClient import io.ktor.client.engine.HttpClientEngine import io.ktor.client.engine.okhttp.OkHttp -import okhttp3.OkHttpClient /** * The integration point between the SDK Core and the product SDKs. @@ -42,7 +41,7 @@ abstract class BaseXapClient( XapConfigurationProvider ) - private val engine: HttpClientEngine = clientConfiguration.okHttpClient?.let { + private val engine: HttpClientEngine = _configurationProvider.okHttpClient?.let { OkHttp.create { preconfigured = it } @@ -66,17 +65,5 @@ abstract class BaseXapClient( /** A [BaseXapClient] builder with ability to pass a custom okhttp client. */ @Suppress("unused", "UnnecessaryAbstractClass") // This is used by the generated SDK clients. - abstract class BuilderWithHttpClient> : Client.Builder() { - protected var okHttpClient: OkHttpClient? = null - - @Suppress("UNCHECKED_CAST") - override fun self(): SELF = this as SELF - - /** Sets the [OkHttpClient] to use for the [BaseXapClient]. */ - fun okHttpClient(okHttpClient: OkHttpClient): SELF { - this.okHttpClient = okHttpClient - return self() - } - - } + abstract class BuilderWithHttpClient> : Client.BuilderWithHttpClient() } diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/client/Client.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/client/Client.kt index d7ef4c7b2..b2249d44d 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/client/Client.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/client/Client.kt @@ -51,6 +51,7 @@ import io.ktor.client.engine.okhttp.OkHttp import io.ktor.client.statement.HttpResponse import io.ktor.client.statement.request import okhttp3.Dispatcher +import okhttp3.OkHttpClient // Create a Dispatcher with limits val dispatcher = Dispatcher().apply { @@ -312,6 +313,29 @@ abstract class Client( } + + /** + * A builder class for configuring HTTP-related settings for a [Client] with the ability to pass a custom [OkHttpClient]. + * + * This builder class extends the base [Client.Builder] class and provides additional methods + * for setting a configured okhttp client. + * + * @param The type of the builder itself, used for method chaining. + */ + abstract class BuilderWithHttpClient> : Builder() { + protected var okHttpClient: OkHttpClient? = null + + @Suppress("UNCHECKED_CAST") + override fun self(): SELF = this as SELF + + /** Sets the [OkHttpClient] to use for the [Client]. */ + fun okHttpClient(okHttpClient: OkHttpClient): SELF { + this.okHttpClient = okHttpClient + return self() + } + + } + } /** Executes the hooks for the client. */ diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/ClientConfiguration.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/ClientConfiguration.kt index d8dfdedd9..557db5ed6 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/ClientConfiguration.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/ClientConfiguration.kt @@ -16,6 +16,7 @@ package com.expediagroup.sdk.core.configuration import com.expediagroup.sdk.core.configuration.provider.RuntimeConfigurationProvider +import okhttp3.OkHttpClient interface ClientConfiguration { val key: String? @@ -26,6 +27,7 @@ interface ClientConfiguration { val socketTimeout: Long? val maskedLoggingHeaders: Set? val maskedLoggingBodyFields: Set? + val okHttpClient: OkHttpClient? /** Build a [RuntimeConfigurationProvider] from a [ClientConfiguration]. */ fun toProvider(): RuntimeConfigurationProvider = @@ -37,6 +39,7 @@ interface ClientConfiguration { connectionTimeout = connectionTimeout, socketTimeout = socketTimeout, maskedLoggingHeaders = maskedLoggingHeaders, - maskedLoggingBodyFields = maskedLoggingBodyFields + maskedLoggingBodyFields = maskedLoggingBodyFields, + okHttpClient = okHttpClient ) } diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/ExpediaGroupClientConfiguration.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/ExpediaGroupClientConfiguration.kt index e5289d9e9..917604d76 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/ExpediaGroupClientConfiguration.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/ExpediaGroupClientConfiguration.kt @@ -17,6 +17,7 @@ package com.expediagroup.sdk.core.configuration import com.expediagroup.sdk.core.client.ExpediaGroupClient import com.expediagroup.sdk.core.configuration.provider.RuntimeConfigurationProvider +import okhttp3.OkHttpClient /** * Configuration for the [ExpediaGroupClient]. @@ -30,6 +31,7 @@ import com.expediagroup.sdk.core.configuration.provider.RuntimeConfigurationProv * @property maskedLoggingHeaders The headers to be masked in logging. * @property maskedLoggingBodyFields The body fields to be masked in logging. * @property authEndpoint The API endpoint to use for authentication. + * @property okHttpClient The okhttp client to be used by the sdk. */ data class ExpediaGroupClientConfiguration( override val key: String? = null, @@ -40,6 +42,7 @@ data class ExpediaGroupClientConfiguration( override val socketTimeout: Long? = null, override val maskedLoggingHeaders: Set? = null, override val maskedLoggingBodyFields: Set? = null, + override val okHttpClient: OkHttpClient? = null, val authEndpoint: String? = null ) : ClientConfiguration { /** Build a [RuntimeConfigurationProvider] from an [ExpediaGroupClientConfiguration]. */ diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/RapidClientConfiguration.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/RapidClientConfiguration.kt index 523d116f2..48a1cef94 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/RapidClientConfiguration.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/RapidClientConfiguration.kt @@ -39,5 +39,5 @@ data class RapidClientConfiguration( override val socketTimeout: Long? = null, override val maskedLoggingHeaders: Set? = null, override val maskedLoggingBodyFields: Set? = null, - val okHttpClient: OkHttpClient? = null + override val okHttpClient: OkHttpClient? = null ) : ClientConfiguration diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/XapClientConfiguration.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/XapClientConfiguration.kt index f3ca5ad8f..ec72f80d7 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/XapClientConfiguration.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/XapClientConfiguration.kt @@ -39,5 +39,5 @@ data class XapClientConfiguration( override val socketTimeout: Long? = null, override val maskedLoggingHeaders: Set? = null, override val maskedLoggingBodyFields: Set? = null, - val okHttpClient: OkHttpClient? = null + override val okHttpClient: OkHttpClient? = null ) : ClientConfiguration diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/collector/ConfigurationCollector.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/collector/ConfigurationCollector.kt index a83d40fbe..49b674182 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/collector/ConfigurationCollector.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/collector/ConfigurationCollector.kt @@ -28,6 +28,7 @@ import com.expediagroup.sdk.core.constant.ConfigurationName.SECRET import com.expediagroup.sdk.core.constant.ConfigurationName.SOCKET_TIMEOUT_MILLIS import com.expediagroup.sdk.core.constant.provider.LoggingMessageProvider import com.expediagroup.sdk.core.plugin.logging.ExpediaGroupLoggerFactory +import okhttp3.OkHttpClient /** * Configuration collector that collects configuration from all available providers. @@ -66,6 +67,7 @@ internal class ConfigurationCollector private constructor(providers: Configurati override val socketTimeout: Long? = providers.firstWith { it.socketTimeout }.also { it?.log(SOCKET_TIMEOUT_MILLIS) }?.retrieve() override val maskedLoggingHeaders: Set? = providers.firstWith { it.maskedLoggingHeaders }.also { it?.log(MASKED_LOGGING_HEADERS) }?.retrieve() override val maskedLoggingBodyFields: Set? = providers.firstWith { it.maskedLoggingBodyFields }.also { it?.log(MASKED_LOGGING_BODY_FIELDS) }?.retrieve() + override val okHttpClient: OkHttpClient? = providers.firstWith { it.okHttpClient }?.retrieve() private fun ProvidedConfiguration.log(configurationName: String) { log.info(LoggingMessageProvider.getChosenProviderMessage(configurationName, providerName)) diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/provider/ConfigurationProvider.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/provider/ConfigurationProvider.kt index ac7f7ea43..0a14edd19 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/provider/ConfigurationProvider.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/provider/ConfigurationProvider.kt @@ -16,6 +16,7 @@ package com.expediagroup.sdk.core.configuration.provider import com.expediagroup.sdk.core.constant.Constant +import okhttp3.OkHttpClient /** * A configuration provider that can be used to provide configuration values. @@ -55,4 +56,8 @@ interface ConfigurationProvider { /** The body fields to be masked in logging.*/ val maskedLoggingBodyFields: Set? get() = setOf() + + /** The okhttp client to be used by the sdk.*/ + val okHttpClient: OkHttpClient? + get() = null } diff --git a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/provider/RuntimeConfigurationProvider.kt b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/provider/RuntimeConfigurationProvider.kt index fd72dc58e..d6e607e09 100644 --- a/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/provider/RuntimeConfigurationProvider.kt +++ b/core/src/main/kotlin/com/expediagroup/sdk/core/configuration/provider/RuntimeConfigurationProvider.kt @@ -16,6 +16,7 @@ package com.expediagroup.sdk.core.configuration.provider import com.expediagroup.sdk.core.constant.ConfigurationName.RUNTIME_CONFIGURATION_PROVIDER +import okhttp3.OkHttpClient /** * A runtime-built configuration provider. @@ -41,5 +42,6 @@ data class RuntimeConfigurationProvider( override val connectionTimeout: Long? = null, override val socketTimeout: Long? = null, override val maskedLoggingHeaders: Set? = null, - override val maskedLoggingBodyFields: Set? = null + override val maskedLoggingBodyFields: Set? = null, + override val okHttpClient: OkHttpClient? = null ) : ConfigurationProvider diff --git a/core/src/test/kotlin/com/expediagroup/sdk/core/configuration/ExpediaGroupClientConfigurationTest.kt b/core/src/test/kotlin/com/expediagroup/sdk/core/configuration/ExpediaGroupClientConfigurationTest.kt index 3310560e8..a01f7d5bb 100644 --- a/core/src/test/kotlin/com/expediagroup/sdk/core/configuration/ExpediaGroupClientConfigurationTest.kt +++ b/core/src/test/kotlin/com/expediagroup/sdk/core/configuration/ExpediaGroupClientConfigurationTest.kt @@ -32,6 +32,7 @@ class ExpediaGroupClientConfigurationTest { assertNull(it.socketTimeout) assertNull(it.maskedLoggingHeaders) assertNull(it.maskedLoggingBodyFields) + assertNull(it.okHttpClient) } } @@ -72,6 +73,7 @@ class ExpediaGroupClientConfigurationTest { assertNull(it.socketTimeout) assertNull(it.maskedLoggingHeaders) assertNull(it.maskedLoggingBodyFields) + assertNull(it.okHttpClient) } } diff --git a/generator/openapi/src/main/resources/templates/expediagroup-sdk/client.mustache b/generator/openapi/src/main/resources/templates/expediagroup-sdk/client.mustache index 88b5a95e5..5ddbbc8fc 100644 --- a/generator/openapi/src/main/resources/templates/expediagroup-sdk/client.mustache +++ b/generator/openapi/src/main/resources/templates/expediagroup-sdk/client.mustache @@ -22,7 +22,7 @@ import kotlinx.coroutines.runBlocking class {{clientClassname}}Client private constructor(clientConfiguration: ExpediaGroupClientConfiguration) : ExpediaGroupClient("{{namespace}}", clientConfiguration) { class Builder : ExpediaGroupClient.Builder() { override fun build() = {{clientClassname}}Client( - ExpediaGroupClientConfiguration(key, secret, endpoint, requestTimeout, connectionTimeout, socketTimeout, maskedLoggingHeaders, maskedLoggingBodyFields, authEndpoint) + ExpediaGroupClientConfiguration(key, secret, endpoint, requestTimeout, connectionTimeout, socketTimeout, maskedLoggingHeaders, maskedLoggingBodyFields, null, authEndpoint) ) }