abdl
11/19/2024, 3:25 AMclass MbakKasirHttpClientBuilder(
private val sessionHandler: SessionHandler
) {
private lateinit var protocol: URLProtocol
private lateinit var host: String
private var port: Int? = null
fun protocol(protocol: URLProtocol) = apply { this.protocol = protocol }
fun host(host: String) = apply { this.host = host }
fun port(port: Int) = apply { this.port = port }
fun build(engine: HttpClientEngine): HttpClient {
return HttpClient(engine) {
expectSuccess = true
defaultRequest {
url {
protocol = this@MbakKasirHttpClientBuilder.protocol
host = this@MbakKasirHttpClientBuilder.host
this@MbakKasirHttpClientBuilder.port?.let { port = it }
}
header("Content-Type", "application/json")
}
install(ContentNegotiation) {
json(
Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
}
)
}
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
println(message)
}
}
level = LogLevel.ALL
}
install(Auth) {
bearer {
loadTokens {
BearerTokens(
accessToken = sessionHandler.getToken().first(),
refreshToken = ""
)
}
}
}
}
}
}
I inject the HttpClient using Koin like this:
val provideHttpClientModule = module {
single {
MbakKasirHttpClientBuilder(sessionHandler = get())
.protocol(URLProtocol.HTTPS)
.host(BuildKonfig.BASE_URL)
.build(engine = get())
}
single { RequestHandler(get()) }
}
Does anyone know how to make the Ktor client dynamically observe and use the updated token after login? Should I recreate the client after login, or is there a better way to make the Auth plugin fetch the latest token automatically? Any advice or guidance would be greatly appreciated!Pamela Hill
11/19/2024, 5:08 AMabdl
11/19/2024, 5:21 AMwisha khn
11/19/2024, 8:58 AM