1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

RetryAfterError handling in KtorRequestsExecutor

This commit is contained in:
InsanusMokrassar 2019-02-05 12:55:46 +08:00
parent c4f0ed4b48
commit 58f73fac97
3 changed files with 15 additions and 1 deletions

View File

@ -29,6 +29,8 @@ must be regular text
* Add `RequestError` sealed class and described in documentation known errors
* Add `ResponseParametersRaw` which can create error based on input parameters
* Add `parameters` field in `Response` and remove useless fields from `Response`
* Add `leftToRetry` parameter in `RetryAfterError`
* Add handling of `RetryAfterError` in `KtorRequestsExecutor`
### 0.9.3

View File

@ -8,11 +8,13 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.limiters.EmptyLim
import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.limiters.RequestLimiter
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
import io.ktor.client.HttpClient
import io.ktor.client.call.HttpClientCall
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.util.cio.toByteArray
import kotlinx.coroutines.delay
import kotlinx.io.charsets.Charset
import kotlinx.serialization.json.JSON
@ -64,7 +66,15 @@ class KtorRequestsExecutor(
Response.serializer(request.resultSerializer()),
content
)
responseObject.result ?: call.let {
responseObject.result ?: responseObject.parameters ?.let {
val error = it.error
if (error is RetryAfterError) {
delay(error.leftToRetry)
execute(request)
} else {
null
}
} ?: call.let {
throw RequestException(
responseObject,
"Can't get result object"

View File

@ -9,6 +9,8 @@ data class RetryAfterError(
val startCountingMillis: Long
) : RequestError() {
val canContinue = TimeUnit.SECONDS.toMillis(seconds) + startCountingMillis
val leftToRetry: Long
get() = canContinue - System.currentTimeMillis()
}
data class MigrateChatId(