2019-08-19 08:13:43 +00:00
|
|
|
package com.github.insanusmokrassar.SauceNaoAPI.exceptions
|
|
|
|
|
2019-12-13 18:21:12 +00:00
|
|
|
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
|
2019-08-19 08:13:43 +00:00
|
|
|
import io.ktor.client.features.ClientRequestException
|
2019-12-13 18:21:12 +00:00
|
|
|
import io.ktor.client.response.readText
|
2019-08-19 08:13:43 +00:00
|
|
|
import io.ktor.http.HttpStatusCode.Companion.TooManyRequests
|
|
|
|
import kotlinx.io.IOException
|
|
|
|
|
2019-12-13 18:21:12 +00:00
|
|
|
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
|
|
|
|
}
|
2019-08-19 08:13:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 18:21:12 +00:00
|
|
|
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
|
|
|
|
}
|