From 491d476cacb4b246fa306117248dffb8d6f5e366 Mon Sep 17 00:00:00 2001 From: MCAxiaz Date: Sun, 29 Dec 2019 14:45:58 -0800 Subject: [PATCH] auto attempt a login refresh once if MAL returns http 400 (#32) (#2403) --- .../data/track/myanimelist/MyAnimeList.kt | 14 +++++++----- .../myanimelist/MyAnimeListInterceptor.kt | 22 ++++++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt index fbfc1e019b..da40b5cfd5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt @@ -112,11 +112,7 @@ class Myanimelist(private val context: Context, id: Int) : TrackService(id) { .toCompletable() } - // Attempt to login again if cookies have been cleared but credentials are still filled - fun ensureLoggedIn() { - if (isAuthorized) return - if (!isLogged) throw Exception("MAL Login Credentials not found") - + fun refreshLogin() { val username = getUsername() val password = getPassword() logout() @@ -131,6 +127,14 @@ class Myanimelist(private val context: Context, id: Int) : TrackService(id) { } } + // Attempt to login again if cookies have been cleared but credentials are still filled + fun ensureLoggedIn() { + if (isAuthorized) return + if (!isLogged) throw Exception("MAL Login Credentials not found") + + refreshLogin() + } + override fun logout() { super.logout() preferences.trackToken(this).delete() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt index 0a032c6a5b..124182168f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.data.track.myanimelist import okhttp3.Interceptor +import okhttp3.Request import okhttp3.RequestBody import okhttp3.Response import okio.Buffer @@ -11,18 +12,27 @@ class MyAnimeListInterceptor(private val myanimelist: Myanimelist): Interceptor override fun intercept(chain: Interceptor.Chain): Response { myanimelist.ensureLoggedIn() - var request = chain.request() - request.body()?.let { + val request = chain.request() + var response = chain.proceed(updateRequest(request)) + + if (response.code() == 400){ + myanimelist.refreshLogin() + response = chain.proceed(updateRequest(request)) + } + + return response + } + + private fun updateRequest(request: Request): Request { + return request.body()?.let { val contentType = it.contentType().toString() val updatedBody = when { contentType.contains("x-www-form-urlencoded") -> updateFormBody(it) contentType.contains("json") -> updateJsonBody(it) else -> it } - request = request.newBuilder().post(updatedBody).build() - } - - return chain.proceed(request) + request.newBuilder().post(updatedBody).build() + } ?: request } private fun bodyToString(requestBody: RequestBody): String {