mirror of
https://github.com/geode-sdk/android-launcher.git
synced 2026-10-21 16:32:39 +00:00
show message for api ratelimits
This commit is contained in:
1 file changed
+34
@@ -1,6 +1,10 @@
|
|||||||
package com.geode.launcher.updater
|
package com.geode.launcher.updater
|
||||||
|
|
||||||
import com.geode.launcher.utils.DownloadUtils.executeCoroutine
|
import com.geode.launcher.utils.DownloadUtils.executeCoroutine
|
||||||
|
import kotlinx.datetime.Clock
|
||||||
|
import kotlinx.datetime.DateTimeUnit
|
||||||
|
import kotlinx.datetime.Instant
|
||||||
|
import kotlinx.datetime.until
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.JsonNamingStrategy
|
import kotlinx.serialization.json.JsonNamingStrategy
|
||||||
@@ -15,6 +19,9 @@ class ReleaseRepository(private val httpClient: OkHttpClient) {
|
|||||||
private const val GITHUB_API_BASE = "https://api.github.com"
|
private const val GITHUB_API_BASE = "https://api.github.com"
|
||||||
private const val GITHUB_API_HEADER = "X-GitHub-Api-Version"
|
private const val GITHUB_API_HEADER = "X-GitHub-Api-Version"
|
||||||
private const val GITHUB_API_VERSION = "2022-11-28"
|
private const val GITHUB_API_VERSION = "2022-11-28"
|
||||||
|
|
||||||
|
private const val GITHUB_RATELIMIT_REMAINING = "x-ratelimit-remaining"
|
||||||
|
private const val GITHUB_RATELIMIT_RESET = "x-ratelimit-reset"
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getLatestLauncherRelease(): Release? {
|
suspend fun getLatestLauncherRelease(): Release? {
|
||||||
@@ -35,6 +42,15 @@ class ReleaseRepository(private val httpClient: OkHttpClient) {
|
|||||||
return getReleaseByUrl(url)
|
return getReleaseByUrl(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun generateRateLimitMessage(resetTime: Instant): String {
|
||||||
|
val currentTime = Clock.System.now()
|
||||||
|
val resetDelay = currentTime.until(resetTime, DateTimeUnit.SECOND)
|
||||||
|
|
||||||
|
val formattedWait = "${resetDelay / 60}m"
|
||||||
|
|
||||||
|
return "api ratelimit reached, try again in ${formattedWait}"
|
||||||
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalSerializationApi::class)
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
private suspend fun getReleaseByUrl(url: URL): Release? {
|
private suspend fun getReleaseByUrl(url: URL): Release? {
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
@@ -59,6 +75,24 @@ class ReleaseRepository(private val httpClient: OkHttpClient) {
|
|||||||
|
|
||||||
release
|
release
|
||||||
}
|
}
|
||||||
|
403 -> {
|
||||||
|
// determine if the error code is a ratelimit
|
||||||
|
// (github docs say it sends 429 too, but haven't seen that)
|
||||||
|
|
||||||
|
val limitRemaining = response.headers.get(GITHUB_RATELIMIT_REMAINING)?.toInt()
|
||||||
|
val limitReset = response.headers.get(GITHUB_RATELIMIT_RESET)?.toLong()
|
||||||
|
|
||||||
|
if (limitRemaining == 0 && limitReset != null) {
|
||||||
|
// handle ratelimit with a custom error
|
||||||
|
// there's also a retry-after header but again, haven't seen
|
||||||
|
val resetTime = Instant.fromEpochSeconds(limitReset, 0L)
|
||||||
|
val msg = generateRateLimitMessage(resetTime)
|
||||||
|
|
||||||
|
throw IOException(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
throw IOException("response 403: ${response.body!!.string()}")
|
||||||
|
}
|
||||||
404 -> {
|
404 -> {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user