Skip to content

Commit

Permalink
update based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dtayeh committed Nov 26, 2024
1 parent 2f270d8 commit c59a4d6
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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<SELF : Client.Builder<SELF>> : Client.Builder<SELF>() {
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<SELF : Client.BuilderWithHttpClient<SELF>> : Client.BuilderWithHttpClient<SELF>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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<SELF : Client.Builder<SELF>> : Client.Builder<SELF>() {
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<SELF : Client.BuilderWithHttpClient<SELF>> : Client.BuilderWithHttpClient<SELF>()
}
25 changes: 22 additions & 3 deletions core/src/main/kotlin/com/expediagroup/sdk/core/client/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -309,12 +310,30 @@ abstract class Client(

/** Create a [Client] object. */
abstract override fun build(): 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 <SELF> The type of the builder itself, used for method chaining.
*/
abstract class BuilderWithHttpClient<SELF : Builder<SELF>> : Builder<SELF>() {
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. */
fun <T : Client> T.finalize() = Hooks.execute(this)


Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -26,6 +27,7 @@ interface ClientConfiguration {
val socketTimeout: Long?
val maskedLoggingHeaders: Set<String>?
val maskedLoggingBodyFields: Set<String>?
val okHttpClient: OkHttpClient?

/** Build a [RuntimeConfigurationProvider] from a [ClientConfiguration]. */
fun toProvider(): RuntimeConfigurationProvider =
Expand All @@ -37,6 +39,7 @@ interface ClientConfiguration {
connectionTimeout = connectionTimeout,
socketTimeout = socketTimeout,
maskedLoggingHeaders = maskedLoggingHeaders,
maskedLoggingBodyFields = maskedLoggingBodyFields
maskedLoggingBodyFields = maskedLoggingBodyFields,
okHttpClient = okHttpClient
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -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,
Expand All @@ -40,6 +42,7 @@ data class ExpediaGroupClientConfiguration(
override val socketTimeout: Long? = null,
override val maskedLoggingHeaders: Set<String>? = null,
override val maskedLoggingBodyFields: Set<String>? = null,
override val okHttpClient: OkHttpClient? = null,
val authEndpoint: String? = null
) : ClientConfiguration {
/** Build a [RuntimeConfigurationProvider] from an [ExpediaGroupClientConfiguration]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ data class RapidClientConfiguration(
override val socketTimeout: Long? = null,
override val maskedLoggingHeaders: Set<String>? = null,
override val maskedLoggingBodyFields: Set<String>? = null,
val okHttpClient: OkHttpClient? = null
override val okHttpClient: OkHttpClient? = null
) : ClientConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ data class XapClientConfiguration(
override val socketTimeout: Long? = null,
override val maskedLoggingHeaders: Set<String>? = null,
override val maskedLoggingBodyFields: Set<String>? = null,
val okHttpClient: OkHttpClient? = null
override val okHttpClient: OkHttpClient? = null
) : ClientConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<String>? = providers.firstWith { it.maskedLoggingHeaders }.also { it?.log(MASKED_LOGGING_HEADERS) }?.retrieve()
override val maskedLoggingBodyFields: Set<String>? = providers.firstWith { it.maskedLoggingBodyFields }.also { it?.log(MASKED_LOGGING_BODY_FIELDS) }?.retrieve()
override val okHttpClient: OkHttpClient? = providers.firstWith { it.okHttpClient }?.retrieve()

private fun <T> ProvidedConfiguration<T>.log(configurationName: String) {
log.info(LoggingMessageProvider.getChosenProviderMessage(configurationName, providerName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -55,4 +56,8 @@ interface ConfigurationProvider {
/** The body fields to be masked in logging.*/
val maskedLoggingBodyFields: Set<String>?
get() = setOf()

/** The okhttp client to be used by the sdk.*/
val okHttpClient: OkHttpClient?
get() = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,5 +42,6 @@ data class RuntimeConfigurationProvider(
override val connectionTimeout: Long? = null,
override val socketTimeout: Long? = null,
override val maskedLoggingHeaders: Set<String>? = null,
override val maskedLoggingBodyFields: Set<String>? = null
override val maskedLoggingBodyFields: Set<String>? = null,
override val okHttpClient: OkHttpClient? = null
) : ConfigurationProvider
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ExpediaGroupClientConfigurationTest {
assertNull(it.socketTimeout)
assertNull(it.maskedLoggingHeaders)
assertNull(it.maskedLoggingBodyFields)
assertNull(it.okHttpClient)
}
}

Expand Down Expand Up @@ -72,6 +73,7 @@ class ExpediaGroupClientConfigurationTest {
assertNull(it.socketTimeout)
assertNull(it.maskedLoggingHeaders)
assertNull(it.maskedLoggingBodyFields)
assertNull(it.okHttpClient)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import kotlinx.coroutines.runBlocking
class {{clientClassname}}Client private constructor(clientConfiguration: ExpediaGroupClientConfiguration) : ExpediaGroupClient("{{namespace}}", clientConfiguration) {
class Builder : ExpediaGroupClient.Builder<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)
)
}

Expand Down

0 comments on commit c59a4d6

Please sign in to comment.