This commit is contained in:
InsanusMokrassar 2019-12-14 00:21:12 +06:00
parent 3ecfb4298b
commit 0c2c7e9e50
3 changed files with 33 additions and 9 deletions

View File

@ -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()
}
}

View File

@ -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
}

View File

@ -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,