Hi all! At our company we're trying to migrate fr...
# ktor
p
Hi all! At our company we're trying to migrate from Retrofit to Ktor (KMP) but we'd like to modularize the configuration and optimally inject already configured plugins into the HttpClient, like this:
Copy code
val client = HttpClient {
    plugins.forEach {
        install(it)
    }
}
We started with the Logger. Our idea was to create a custom plugin that installs the Logger and itself and configures it but we found out that you can't do that with Plugins (or can you?). The official documentation asks you to just install Logger explicitly in the HttpClientConfig but we want to do this outside of HttpClient. Is there a common approach on how to do this? Here's one idea I had:
Copy code
interface KtorInstallable {
    fun HttpClientConfig<*>.install()
}

interface PlatformLogger {
    fun logKtor(message: String)
}

class CustomLoggerPlugin(
    private val platformLogger: PlatformLogger,
) : KtorInstallable {
    override fun HttpClientConfig<*>.install() {
        install(Logging) {
            logger = object : Logger {
                override fun log(message: String) {
                    platformLogger.logKtor(message)
                }
            }
        }
    }
}

class CustomHttpClient(plugins: List<KtorInstallable>) {
    val client = HttpClient {
        plugins.forEach { plugin ->
            plugin.run { install() }
        }
    }
}
We are quite new to Ktor. Thank you!!
a
I think the above approach should work. Can you tell me what the purpose of the modularization is?
p
We'd like to inject custom implementations depending on debug/release/qa build and such. We currently have a highly modularized setup with Retrofit and we'd like to achieve something equivalent with ktor.
We'd like to have a logging module, auth module, bot protection etc which are responsible for different things.