image.png
# ktor
u
image.png
🧵 1
m
You can do it if you cast the HttpClientConfig to
HttpClientConfig<OkHttpConfig>
u
you mean
this
on the
config
lambda?
m
More something like
Copy code
fun API.setDefaultClientConfig(block: HttpClientConfig<OkHttpConfig>.() -> Unit) {
    @Suppress("UNCHECKED_CAST")
    defaultClient = defaultClient.config(block as HttpClientConfig<*>.() -> Unit)
}
u
I see, thanks!
regardless .. seems engine is not supposed to be changed?
m
Possibly, could also just be a limitation in the API
u
btw do you do KMP?
m
yes
u
do you use okhttp engine when on android?
m
yes on JVM
u
can I know how does your setup of the
HttpClient
look like? do you keep it in common and expect/actual the engine somehow? or expect actual the whole
createHttpClient
with distinct platform setups? (I assume you have more setup on the instance other than passin in okhttp)
.. maybe a better first question would be - do you pass in the
okhttp
instance yourself?
m
All in common no expect actual, it can pick the engine from the classpath. I just have some overrides like the above on each platform. I set the engines on my side but probably better to let the client choose
u
hmm what do you mean from classpath?
u
not sure what you mean just this
val client = HttpClient()
?
m
yes
u
I mean..yea..when there's no okhttp
preconfigured
passing in but for my interop story I need to pass that instance in and now not sure how should I set up my HttpClient instance
m
I just use the above method to set it, (setDefaultClientConfig)
u
since I need this
Copy code
private val ktor = HttpClient(OkHttp) {
    engine {
        preconfigured = httpClient
    }
}
but this doesn't compose at all, because ktor people really love their dsl-ish stuff
a
You can try creating a default config separately to instantiate two clients with the default config and the overridden one. Here is an example:
Copy code
val defaultConfig = HttpClientConfig<OkHttpConfig>().apply {
    engine {
        preconfigured = httpClient
    }
    
    install(DefaultRequest) {
        url("")
    }

    install(ContentNegotiation) {
        json()
    }
}

val ktor1 = HttpClient(OkHttp.create(), defaultConfig)

val newConfig = HttpClientConfig<OkHttpConfig>().apply {
    plusAssign(defaultConfig)
    engine {
        preconfigured = httpClient
    }
}
val ktor2 = HttpClient(OkHttp.create(), newConfig)
u
plusAssign .. neat!