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) quotaManager.updateQuota(answer.header, timeManager)
} catch (e: TooManyRequestsException) { } 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) requestsChannel.send(callback to requestBuilder)
} catch (e: Exception) { } catch (e: Exception) {
try { try {
@ -141,7 +142,7 @@ data class SauceNaoAPI(
answerText answerText
) )
} catch (e: ClientRequestException) { } catch (e: ClientRequestException) {
throw e.sauceNaoAPIException throw e.sauceNaoAPIException()
} }
} }

View File

@ -1,13 +1,34 @@
package com.github.insanusmokrassar.SauceNaoAPI.exceptions 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.features.ClientRequestException
import io.ktor.client.response.readText
import io.ktor.http.HttpStatusCode.Companion.TooManyRequests import io.ktor.http.HttpStatusCode.Companion.TooManyRequests
import kotlinx.io.IOException import kotlinx.io.IOException
val ClientRequestException.sauceNaoAPIException: Exception internal suspend fun ClientRequestException.sauceNaoAPIException(): Exception {
get() = when (response.status) { return when (response.status) {
TooManyRequests -> TooManyRequestsException() TooManyRequests -> {
else -> this 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.LONG_TIME_RECALCULATING_MILLIS
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_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.github.insanusmokrassar.SauceNaoAPI.models.Header
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import kotlinx.coroutines.* import kotlinx.coroutines.*
@ -64,8 +66,8 @@ class RequestQuotaManager (
timeManager timeManager
) )
suspend fun happenTooManyRequests(timeManager: TimeManager) = updateQuota( suspend fun happenTooManyRequests(timeManager: TimeManager, e: TooManyRequestsException) = updateQuota(
1, if (e is TooManyRequestsLongException) 0 else 1,
0, 0,
null, null,
null, null,