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:
parent
e77478b13e
commit
5692bb321c
@ -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`)
|
||||||
|
@ -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,30 +43,32 @@ class KtorRequestsExecutor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun <T : Any> execute(request: Request<T>): T {
|
override suspend fun <T : Any> execute(request: Request<T>): T {
|
||||||
var call: HttpClientCall? = null
|
return requestsLimiter.limit {
|
||||||
for (factory in callsFactories) {
|
var call: HttpClientCall? = null
|
||||||
call = factory.prepareCall(
|
for (factory in callsFactories) {
|
||||||
client,
|
call = factory.prepareCall(
|
||||||
baseUrl,
|
client,
|
||||||
request
|
baseUrl,
|
||||||
)
|
request
|
||||||
if (call != null) {
|
)
|
||||||
break
|
if (call != null) {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (call == null) {
|
||||||
if (call == null) {
|
throw IllegalArgumentException("Can't execute request: $request")
|
||||||
throw IllegalArgumentException("Can't execute request: $request")
|
}
|
||||||
}
|
val content = call.response.content.toByteArray().toString(Charset.defaultCharset())
|
||||||
val content = call.response.content.toByteArray().toString(Charset.defaultCharset())
|
val responseObject = JSON.parse(
|
||||||
val responseObject = JSON.parse(
|
ResponseParameters.serializer(request.resultSerializer()),
|
||||||
ResponseParameters.serializer(request.resultSerializer()),
|
content
|
||||||
content
|
|
||||||
)
|
|
||||||
return responseObject.result ?: call.let {
|
|
||||||
throw RequestException(
|
|
||||||
responseObject,
|
|
||||||
"Can't get result object"
|
|
||||||
)
|
)
|
||||||
|
responseObject.result ?: call.let {
|
||||||
|
throw RequestException(
|
||||||
|
responseObject,
|
||||||
|
"Can't get result object"
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user