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

Ktor realisation use RequestLimiter

This commit is contained in:
InsanusMokrassar 2019-01-24 10:36:44 +08:00
parent e77478b13e
commit 5692bb321c
2 changed files with 29 additions and 23 deletions

View File

@ -50,3 +50,4 @@
* Replace `ProxySettings` data class in `settings` package, deprecate old link * Replace `ProxySettings` data class in `settings` package, deprecate old link
* `BaseRequestsExecutor` now have no it's own scope * `BaseRequestsExecutor` now have no it's own scope
* Add `RequestLimiter` and base realisations * Add `RequestLimiter` and base realisations
* Now `KtorRequestsExecutor` can receive as one of parameters `RequestLimiter` (by default - `EmptyLimiter`)

View File

@ -4,6 +4,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.BaseRequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base.MultipartRequestCallFactory import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base.MultipartRequestCallFactory
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base.SimpleRequestCallFactory import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base.SimpleRequestCallFactory
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestException import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestException
import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.limiters.EmptyLimiter
import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.limiters.RequestLimiter
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import com.github.insanusmokrassar.TelegramBotAPI.types.ResponseParameters import com.github.insanusmokrassar.TelegramBotAPI.types.ResponseParameters
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
@ -19,7 +21,8 @@ class KtorRequestsExecutor(
private val client: HttpClient = HttpClient(OkHttp), private val client: HttpClient = HttpClient(OkHttp),
hostUrl: String = "https://api.telegram.org", hostUrl: String = "https://api.telegram.org",
callsFactories: List<KtorCallFactory> = emptyList(), callsFactories: List<KtorCallFactory> = emptyList(),
excludeDefaultFactories: Boolean = false excludeDefaultFactories: Boolean = false,
private val requestsLimiter: RequestLimiter = EmptyLimiter
) : BaseRequestsExecutor(token, hostUrl) { ) : BaseRequestsExecutor(token, hostUrl) {
constructor( constructor(
token: String, token: String,
@ -40,6 +43,7 @@ class KtorRequestsExecutor(
} }
override suspend fun <T : Any> execute(request: Request<T>): T { override suspend fun <T : Any> execute(request: Request<T>): T {
return requestsLimiter.limit {
var call: HttpClientCall? = null var call: HttpClientCall? = null
for (factory in callsFactories) { for (factory in callsFactories) {
call = factory.prepareCall( call = factory.prepareCall(
@ -59,11 +63,12 @@ class KtorRequestsExecutor(
ResponseParameters.serializer(request.resultSerializer()), ResponseParameters.serializer(request.resultSerializer()),
content content
) )
return responseObject.result ?: call.let { responseObject.result ?: call.let {
throw RequestException( throw RequestException(
responseObject, responseObject,
"Can't get result object" "Can't get result object"
) )
} }
} }
}
} }