From 0c2c7e9e5053a30f0e76447fdac4ee2a7853d61e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 14 Dec 2019 00:21:12 +0600 Subject: [PATCH] fixes --- .../SauceNaoAPI/SauceNaoAPI.kt | 5 +-- .../exceptions/TooManyRequestsException.kt | 31 ++++++++++++++++--- .../SauceNaoAPI/utils/RequestQuotaManager.kt | 6 ++-- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt index d5e2979..4859827 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt @@ -58,7 +58,8 @@ data class SauceNaoAPI( quotaManager.updateQuota(answer.header, timeManager) } catch (e: TooManyRequestsException) { - quotaManager.happenTooManyRequests(timeManager) + logger.warning("Exceed time limit. Answer was:\n${e.answerContent}") + quotaManager.happenTooManyRequests(timeManager, e) requestsChannel.send(callback to requestBuilder) } catch (e: Exception) { try { @@ -141,7 +142,7 @@ data class SauceNaoAPI( answerText ) } catch (e: ClientRequestException) { - throw e.sauceNaoAPIException + throw e.sauceNaoAPIException() } } diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt index e177104..4e213a6 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/exceptions/TooManyRequestsException.kt @@ -1,13 +1,34 @@ package com.github.insanusmokrassar.SauceNaoAPI.exceptions +import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS +import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS +import com.soywiz.klock.TimeSpan import io.ktor.client.features.ClientRequestException +import io.ktor.client.response.readText import io.ktor.http.HttpStatusCode.Companion.TooManyRequests import kotlinx.io.IOException -val ClientRequestException.sauceNaoAPIException: Exception - get() = when (response.status) { - TooManyRequests -> TooManyRequestsException() - else -> this +internal suspend fun ClientRequestException.sauceNaoAPIException(): Exception { + return when (response.status) { + TooManyRequests -> { + val answerContent = response.readText() + when { + answerContent.contains("daily limit") -> TooManyRequestsLongException(answerContent) + else -> TooManyRequestsShortException(answerContent) + } + } + else -> this + } } -class TooManyRequestsException : IOException() +sealed class TooManyRequestsException : IOException() { + abstract val answerContent: String + abstract val waitTime: TimeSpan +} + +class TooManyRequestsShortException(override val answerContent: String) : TooManyRequestsException() { + override val waitTime: TimeSpan = SHORT_TIME_RECALCULATING_MILLIS +} +class TooManyRequestsLongException(override val answerContent: String) : TooManyRequestsException() { + override val waitTime: TimeSpan = LONG_TIME_RECALCULATING_MILLIS +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt index 40641ed..0ae09a7 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/utils/RequestQuotaManager.kt @@ -2,6 +2,8 @@ package com.github.insanusmokrassar.SauceNaoAPI.utils import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS +import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException +import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsLongException import com.github.insanusmokrassar.SauceNaoAPI.models.Header import com.soywiz.klock.DateTime import kotlinx.coroutines.* @@ -64,8 +66,8 @@ class RequestQuotaManager ( timeManager ) - suspend fun happenTooManyRequests(timeManager: TimeManager) = updateQuota( - 1, + suspend fun happenTooManyRequests(timeManager: TimeManager, e: TooManyRequestsException) = updateQuota( + if (e is TooManyRequestsLongException) 0 else 1, 0, null, null,