mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2024-12-23 13:17:11 +00:00
fixes
This commit is contained in:
parent
3ecfb4298b
commit
0c2c7e9e50
@ -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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 -> {
|
||||||
|
val answerContent = response.readText()
|
||||||
|
when {
|
||||||
|
answerContent.contains("daily limit") -> TooManyRequestsLongException(answerContent)
|
||||||
|
else -> TooManyRequestsShortException(answerContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
else -> this
|
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.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,
|
||||||
|
Loading…
Reference in New Issue
Block a user