Skip to content

Commit

Permalink
Refactor HttpClientEngine to be platform-agnostic
Browse files Browse the repository at this point in the history
Removed platform-specific implementations of HttpClientEngine (specifically JS, JVM, and Apple), allowing more flexibility for the caller in terms of choosing HttpClientEngine configurations. Associated platform-specific dependencies have also been removed from the build.gradle.kts file.
  • Loading branch information
Lamberto Basti committed Jan 25, 2024
1 parent b875305 commit 6caef0f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 62 deletions.
11 changes: 0 additions & 11 deletions http/client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpClientEngineConfig>

public interface PackageSearchApi {
public suspend fun getKnownRepositories(): List<ApiRepository>
public suspend fun getPackageInfoByIds(ids: Set<String>): Map<String, ApiPackage>
Expand Down Expand Up @@ -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 <T : HttpClientEngineConfig> defaultHttpClient(
engine: HttpClientEngineFactory<T>,
protobuf: Boolean = true,
additionalConfig: HttpClientConfig<T>.() -> Unit = {},
): HttpClient = HttpClient(engine) {
defaultEngineConfig(protobuf)
additionalConfig()
}

public fun defaultHttpClient(
engine: HttpClientEngine,
protobuf: Boolean = true,
additionalConfig: HttpClientConfig<*>.() -> Unit = {},
): HttpClient = HttpClient(engine) {
defaultEngineConfig(protobuf)
additionalConfig()
}

}

Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 6caef0f

Please sign in to comment.