feat: better anilist api failure info

This commit is contained in:
rebelonion 2024-02-04 00:29:41 -06:00
parent 462f82e3fb
commit 15abcd77d0
2 changed files with 19 additions and 1 deletions

View file

@ -10,6 +10,7 @@ import ani.dantotsu.currContext
import ani.dantotsu.openLinkInBrowser import ani.dantotsu.openLinkInBrowser
import ani.dantotsu.settings.saving.PrefManager import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.toast
import ani.dantotsu.tryWithSuspend import ani.dantotsu.tryWithSuspend
import java.io.File import java.io.File
import java.util.Calendar import java.util.Calendar
@ -30,6 +31,8 @@ object Anilist {
var genres: ArrayList<String>? = null var genres: ArrayList<String>? = null
var tags: Map<Boolean, List<String>>? = null var tags: Map<Boolean, List<String>>? = null
var rateLimitReset: Long = 0
val sortBy = listOf( val sortBy = listOf(
"SCORE_DESC", "SCORE_DESC",
"POPULARITY_DESC", "POPULARITY_DESC",
@ -122,6 +125,11 @@ object Anilist {
cache: Int? = null cache: Int? = null
): T? { ): T? {
return tryWithSuspend { return tryWithSuspend {
if (rateLimitReset > System.currentTimeMillis() / 1000) {
toast("Rate limited. Try after ${rateLimitReset - (System.currentTimeMillis() / 1000)} seconds")
throw Exception("Rate limited after ${rateLimitReset - (System.currentTimeMillis() / 1000)} seconds")
}
val data = mapOf( val data = mapOf(
"query" to query, "query" to query,
"variables" to variables "variables" to variables
@ -140,6 +148,16 @@ object Anilist {
data = data, data = data,
cacheTime = cache ?: 10 cacheTime = cache ?: 10
) )
if (json.code == 429) {
val retry = json.headers["Retry-After"]?.toIntOrNull() ?: -1
val passedLimitReset = json.headers["X-RateLimit-Reset"]?.toLongOrNull() ?: 0
if (retry > 0) {
rateLimitReset = passedLimitReset
}
toast("Rate limited. Try after $retry seconds")
throw Exception("Rate limited after $retry seconds")
}
if (!json.text.startsWith("{")) throw Exception(currContext()?.getString(R.string.anilist_down)) if (!json.text.startsWith("{")) throw Exception(currContext()?.getString(R.string.anilist_down))
if (show) println("Response : ${json.text}") if (show) println("Response : ${json.text}")
json.parsed() json.parsed()

View file

@ -700,7 +700,7 @@ query (${"$"}page: Int = 1, ${"$"}id: Int, ${"$"}type: MediaType, ${"$"}isAdult:
page = pageInfo.currentPage.toString().toIntOrNull() ?: 0, page = pageInfo.currentPage.toString().toIntOrNull() ?: 0,
hasNextPage = pageInfo.hasNextPage == true, hasNextPage = pageInfo.hasNextPage == true,
) )
} else snackString(currContext()?.getString(R.string.empty_response)) }
return null return null
} }