diff --git a/http/client/build.gradle.kts b/http/client/build.gradle.kts index c6e9cfa..0d8d9e9 100644 --- a/http/client/build.gradle.kts +++ b/http/client/build.gradle.kts @@ -23,21 +23,10 @@ kotlin { api(packageSearchApiModelsVersions.kotlinx.serialization.json) } } - jsMain { - dependencies { - api(packageSearchApiModelsVersions.ktor.client.js) - } - } jvmMain { dependencies { - api(packageSearchApiModelsVersions.ktor.client.cio) api(packageSearchApiModelsVersions.logback.classic) } } - appleMain { - dependencies { - api(packageSearchApiModelsVersions.ktor.client.darwin) - } - } } } \ No newline at end of file diff --git a/http/client/src/appleMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.apple.kt b/http/client/src/appleMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.apple.kt deleted file mode 100644 index be006ce..0000000 --- a/http/client/src/appleMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.apple.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.jetbrains.packagesearch.api.v3.http - -import io.ktor.client.engine.HttpClientEngine -import io.ktor.client.engine.HttpClientEngineConfig -import io.ktor.client.engine.HttpClientEngineFactory -import io.ktor.client.engine.darwin.Darwin - -public actual val DefaultEngine: HttpClientEngineFactory - get() = Darwin \ No newline at end of file diff --git a/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.kt b/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.kt index 7534503..1e4d202 100644 --- a/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.kt +++ b/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.kt @@ -24,8 +24,6 @@ import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds -public expect val DefaultEngine: HttpClientEngineFactory - public interface PackageSearchApi { public suspend fun getKnownRepositories(): List public suspend fun getPackageInfoByIds(ids: Set): Map @@ -56,31 +54,52 @@ public class PackageSearchApiClient( public companion object { + private fun HttpClientConfig<*>.defaultEngineConfig(protobuf: Boolean = true) { + install(ContentNegotiation) { + if (protobuf) protobuf(ProtoBuf { encodeDefaults = false }) + json() + } + install(ContentEncoding) { + gzip() + } + install(HttpRequestRetry) { + maxRetries = 3 + constantDelay( + delay = 500.milliseconds, + randomization = 100.milliseconds, + respectRetryAfterHeader = false + ) + } + install(HttpTimeout) { + requestTimeout = 10.seconds + } + } + public fun defaultHttpClient( protobuf: Boolean = true, additionalConfig: HttpClientConfig<*>.() -> Unit = {}, - ): HttpClient = - HttpClient(DefaultEngine) { - install(ContentNegotiation) { - if (protobuf) protobuf(ProtoBuf { encodeDefaults = false }) - json() - } - install(ContentEncoding) { - gzip() - } - install(HttpRequestRetry) { - maxRetries = 3 - constantDelay( - delay = 500.milliseconds, - randomization = 100.milliseconds, - respectRetryAfterHeader = false - ) - } - install(HttpTimeout) { - requestTimeout = 10.seconds - } - additionalConfig() - } + ): HttpClient = HttpClient { + defaultEngineConfig(protobuf) + additionalConfig() + } + + public fun defaultHttpClient( + engine: HttpClientEngineFactory, + protobuf: Boolean = true, + additionalConfig: HttpClientConfig.() -> Unit = {}, + ): HttpClient = HttpClient(engine) { + defaultEngineConfig(protobuf) + additionalConfig() + } + + public fun defaultHttpClient( + engine: HttpClientEngine, + protobuf: Boolean = true, + additionalConfig: HttpClientConfig<*>.() -> Unit = {}, + ): HttpClient = HttpClient(engine) { + defaultEngineConfig(protobuf) + additionalConfig() + } } diff --git a/http/client/src/jsMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.js.kt b/http/client/src/jsMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.js.kt deleted file mode 100644 index 1db0bfa..0000000 --- a/http/client/src/jsMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.js.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.jetbrains.packagesearch.api.v3.http - -import io.ktor.client.engine.HttpClientEngine -import io.ktor.client.engine.HttpClientEngineConfig -import io.ktor.client.engine.HttpClientEngineFactory -import io.ktor.client.engine.js.Js - -public actual val DefaultEngine: HttpClientEngineFactory - get() = Js \ No newline at end of file diff --git a/http/client/src/jvmMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.jvm.kt b/http/client/src/jvmMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.jvm.kt deleted file mode 100644 index 22ee8be..0000000 --- a/http/client/src/jvmMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.jvm.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.jetbrains.packagesearch.api.v3.http - -import io.ktor.client.engine.HttpClientEngine -import io.ktor.client.engine.HttpClientEngineConfig -import io.ktor.client.engine.HttpClientEngineFactory -import io.ktor.client.engine.cio.CIO - -public actual val DefaultEngine: HttpClientEngineFactory - get() = CIO \ No newline at end of file