Trey
05/04/2022, 8:31 PMAleksei Tirman [JB]
05/07/2022, 11:03 AMrefreshTokens
lambda to modify it before sending retried request. Here is an example:
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.plugins.auth.*
import io.ktor.client.plugins.auth.providers.*
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
suspend fun main(): Unit = coroutineScope {
val requestChannel = Channel<HttpRequestBuilder>()
val client = HttpClient(Apache) {
install(Auth) {
bearer {
loadTokens {
BearerTokens("", "")
}
refreshTokens {
val context = requestChannel.receive()
// Here goes a code for getting a new token
val newToken = "123"
context.parameter("auth", newToken)
BearerTokens(newToken, "")
}
}
}
}
client.get {
url {
host = "127.0.0.1"
port = 3030
path("/")
}
launch {
requestChannel.send(this@get)
}
}
}
This solution isn’t optimal but should work even for Websockets requests.