Alper Tekin
09/01/2022, 1:24 PMbindSingleton() { HttpClient(CIO) }
(Auth module) then I set authentication token to the header using the singleton instance
val client: HttpClient by closestDI().instance()
client.config {
defaultRequest {
headers.append(HttpHeaders.Authorization, "$schemes $token")
}
(User module) Also, I have http clients that talk to each other in microservices. But it looks like I can’t set auth token to the same instance, no token is set in the header for the client below.
private val client: HttpClient by instance()
Kodein: 7.11.0
WDYT? Any suggestion?romainbsl
09/01/2022, 3:09 PMclient.config { }
return a new instance of HttpClient
, so you will need to manage a new instance in your Auth module, or you could replace your singleton by a factory/multiton depending on a generic config that could carry your auth.Alper Tekin
09/01/2022, 3:19 PMAlper Tekin
09/08/2022, 9:36 PMfun Application.service(test: Boolean) = DI.Module("service") {
bindMultiton<String, HttpClient> { token -> createHttpClient(!test, token) }
...
}
fun createHttpClient(consul: Boolean, token: String) = HttpClient(CIO) {
...
defaultRequest {
headers.append(HttpHeaders.Authorization, "Bearer $token")
}
and I set token in my auth module as
closestDI().instance<String, HttpClient>(arg = token)
here is my userClient module
val userClient
get() = DI.Module("userClient") {
bind<UserClient>() with singleton { UserClientImpl(di) }
}
and this is how I call the instance:
class UserClientImpl(
override val di: DI
) : DIAware, UserClient {
private val client: HttpClient by instance()
...
}
romainbsl
09/09/2022, 6:33 AMromainbsl
09/09/2022, 6:34 AMromainbsl
09/09/2022, 6:35 AM