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