mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2024-11-04 22:33:47 +00:00
fixes
This commit is contained in:
parent
3ecfb4298b
commit
0c2c7e9e50
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user