Darmaheev
09/03/2019, 6:32 AMval httpClient = HttpClient()
GlobalScope.launch(ApplicationDispatcher) {
try {
val url = servUrl
val json = httpClient.get<String>{
header("Authorization", "Bearer " + Token.getAccessToken())
url(url)
}
success(json)
} catch (ex: Exception){
failure(ex)
}
I would really appreciate any advice. I’m new at ktor and kotlinChristoph Schnabl
09/03/2019, 7:45 AMDarmaheev
09/03/2019, 8:10 AMcy
09/03/2019, 9:14 AMe5l
09/03/2019, 9:18 AMVlad
09/03/2019, 11:05 AMclass TokenManager(private val client: AuthClient) : CoroutineScope {
val tokenChannel = async { ConflatedBroadcastChannel(newToken()) }
private val tokenUpdateJob = launch {
with(tokenChannel.await()) {
while (true) {
delay(value.expiresIn)
send(
runCatching { updateToken(value) }.getOrElse { newToken() }
)
}
}
}
private suspend fun newToken() = client.clientToken()
private suspend fun updateToken(token: TokenResponse) = client.updateToken(token.refreshToken)
suspend fun token() = tokenChannel.await().value
}
Vlad
09/03/2019, 11:28 AMDarmaheev
09/04/2019, 3:47 AM