MV
05/03/2024, 11:25 PMobject API {
var defaultClient = HttpClient {}
fun <T : HttpClientEngineConfig> setDefaultClientConfig(block: HttpClientConfig<T>.() -> Unit) {
defaultClient = defaultClient.config(block as HttpClientConfig<*>.() -> Unit)
}
}
Is there a way to achieve this without doing an unchecked cast?MV
05/03/2024, 11:28 PMAPI.setDefaultClientConfig<OkHttpConfig> {
install(UserAgent) {
agent = "MyAgent"
}
engine {
preconfigured = okHttpClient()
addNetworkInterceptor(DownloadProgressInterceptor(...))
}
}
AdamW
05/04/2024, 12:10 PMHttpClient { }
defaults to here in case there are multiple engines in the classpath. To do this without an unchecked cast, the engine also needs to be provided by a consumer. But overall that’s a dangerous looking pattern in API
MV
05/04/2024, 5:28 PMfun API.setDefaultClientConfig(block: HttpClientConfig<OkHttpConfig>.() -> Unit){
@Suppress("UNCHECKED_CAST")
defaultClient = defaultClient.config(block as HttpClientConfig<*>.() -> Unit)
}
andylamax
05/06/2024, 3:54 PMHttpClient
instead of the config block