Retry to fix post login issues.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
aminecmi 2022-12-08 13:09:02 +01:00
parent d98b00533d
commit 797bf06a9c

View File

@ -4,7 +4,6 @@ import bou.amine.apps.readerforselfossv2.model.*
import bou.amine.apps.readerforselfossv2.service.AppSettingsService import bou.amine.apps.readerforselfossv2.service.AppSettingsService
import io.github.aakira.napier.Napier import io.github.aakira.napier.Napier
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.* import io.ktor.client.plugins.*
import io.ktor.client.plugins.cache.* import io.ktor.client.plugins.cache.*
import io.ktor.client.plugins.contentnegotiation.* import io.ktor.client.plugins.contentnegotiation.*
@ -15,6 +14,9 @@ import io.ktor.client.request.forms.*
import io.ktor.client.statement.* import io.ktor.client.statement.*
import io.ktor.http.* import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.* import io.ktor.serialization.kotlinx.json.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
class SelfossApi(private val appSettingsService: AppSettingsService) { class SelfossApi(private val appSettingsService: AppSettingsService) {
@ -43,22 +45,21 @@ class SelfossApi(private val appSettingsService: AppSettingsService) {
requestTimeoutMillis = appSettingsService.getApiTimeout() requestTimeoutMillis = appSettingsService.getApiTimeout()
} }
install(HttpCookies) install(HttpCookies)
expectSuccess = false install(HttpRequestRetry) {
} maxRetries = 2
retryIf { _, response ->
client.plugin(HttpSend).intercept { request -> response.status == HttpStatusCode.Forbidden && shouldHavePostLogin() && hasLoginInfo()
val originalCall = execute(request) }
if (originalCall.response.status == HttpStatusCode.Forbidden && shouldHavePostLogin() && hasLoginInfo()) { modifyRequest { _ ->
Napier.i("Forbidden action, will try to login and retry", tag = "HttpSend") Napier.i("Will modify", tag = "HttpSend")
CoroutineScope(Dispatchers.Main).launch {
if (login().isSuccess) { Napier.i("Will login", tag = "HttpSend")
Napier.i("Logged in worked", tag = "HttpSend") this@SelfossApi.login()
execute(request) Napier.i("Did login", tag = "HttpSend")
}
} }
originalCall
} else {
originalCall
} }
expectSuccess = false
} }
return client return client