From 62038b6a7152391efe99a7ad041c7e7312f0e101 Mon Sep 17 00:00:00 2001 From: DevSrSouza Date: Wed, 15 Nov 2023 14:19:39 -0300 Subject: [PATCH] feat: adding ktor engine config to support Kotlin Scripting --- .../kotlin/com.aallam.openai.client/OpenAIConfig.kt | 3 +++ .../com.aallam.openai.client/internal/HttpClient.kt | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/openai-client/src/commonMain/kotlin/com.aallam.openai.client/OpenAIConfig.kt b/openai-client/src/commonMain/kotlin/com.aallam.openai.client/OpenAIConfig.kt index 3c4a39c7..82dae6c5 100644 --- a/openai-client/src/commonMain/kotlin/com.aallam.openai.client/OpenAIConfig.kt +++ b/openai-client/src/commonMain/kotlin/com.aallam.openai.client/OpenAIConfig.kt @@ -4,6 +4,7 @@ import com.aallam.openai.api.http.Timeout import com.aallam.openai.api.logging.LogLevel import com.aallam.openai.api.logging.Logger import io.ktor.client.HttpClientConfig +import io.ktor.client.engine.HttpClientEngine import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @@ -19,6 +20,7 @@ import kotlin.time.Duration.Companion.seconds * @param proxy HTTP proxy url * @param host OpenAI host configuration. * @param retry rate limit retry configuration + * @param engine explicit ktor engine for http requests. * @param httpClientConfig additional custom client configuration */ public class OpenAIConfig( @@ -30,6 +32,7 @@ public class OpenAIConfig( public val host: OpenAIHost = OpenAIHost.OpenAI, public val proxy: ProxyConfig? = null, public val retry: RetryStrategy = RetryStrategy(), + public val engine: HttpClientEngine? = null, public val httpClientConfig: HttpClientConfig<*>.() -> Unit = {} ) { diff --git a/openai-client/src/commonMain/kotlin/com.aallam.openai.client/internal/HttpClient.kt b/openai-client/src/commonMain/kotlin/com.aallam.openai.client/internal/HttpClient.kt index 29d25936..85280f74 100644 --- a/openai-client/src/commonMain/kotlin/com.aallam.openai.client/internal/HttpClient.kt +++ b/openai-client/src/commonMain/kotlin/com.aallam.openai.client/internal/HttpClient.kt @@ -21,7 +21,7 @@ import kotlin.time.DurationUnit * Default Http Client. */ internal fun createHttpClient(config: OpenAIConfig): HttpClient { - return HttpClient { + val configuration: HttpClientConfig<*>.() -> Unit = { engine { config.proxy?.let { proxyConfig -> proxy = when (proxyConfig) { @@ -82,6 +82,12 @@ internal fun createHttpClient(config: OpenAIConfig): HttpClient { config.httpClientConfig(this) } + + return if(config.engine != null) { + HttpClient(config.engine, configuration) + } else { + HttpClient(configuration) + } } /**