From 657e9aa770c86f23b3996911fd85562fede1455f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 17 Nov 2020 16:06:25 +0600 Subject: [PATCH] add KtorRequestsExecutorBuilder --- CHANGELOG.md | 5 +++++ .../tgbotapi/bot/Ktor/KtorRequestsExecutor.kt | 18 ++++++++++++++++++ .../tgbotapi/extensions/api/BotExtensions.kt | 14 ++++++-------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f82db78a7c..2202ea5103 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ * `Core`: * `TelegramAPIUrlsKeeper` will fix ending of host url since this version * New mechanisms in`PowLimiter` and `CommonLimiter` has been added + * New builder `KtorRequestsExecutorBuilder` + * New function `telegramBot` +* `Utils`: + * Simple function `telegramBot(TelegramAPIUrlsKeeper)` has been deprecated with replacement by almost the same + function in `Core` ## 0.30.6 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/KtorRequestsExecutor.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/KtorRequestsExecutor.kt index 4c5ffe6922..cdaf835e84 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/KtorRequestsExecutor.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/KtorRequestsExecutor.kt @@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.bot.Ktor import dev.inmo.micro_utils.coroutines.safely import dev.inmo.tgbotapi.bot.BaseRequestsExecutor import dev.inmo.tgbotapi.bot.Ktor.base.* +import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.exceptions.newRequestException import dev.inmo.tgbotapi.bot.settings.limiters.* import dev.inmo.tgbotapi.requests.abstracts.Request @@ -13,6 +14,23 @@ import io.ktor.client.features.* import io.ktor.client.statement.readText import kotlinx.serialization.json.Json +class KtorRequestsExecutorBuilder( + var telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper +) { + var client: HttpClient = HttpClient() + var callsFactories: List = emptyList() + var excludeDefaultFactories: Boolean = false + var requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter() + var jsonFormatter: Json = nonstrictJsonFormat + + fun build() = KtorRequestsExecutor(telegramAPIUrlsKeeper, client, callsFactories, excludeDefaultFactories, requestsLimiter, jsonFormatter) +} + +inline fun telegramBot( + telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, + crossinline builder: KtorRequestsExecutorBuilder.() -> Unit = {} +): TelegramBot = KtorRequestsExecutorBuilder(telegramAPIUrlsKeeper).apply(builder).build() + class KtorRequestsExecutor( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, client: HttpClient = HttpClient(), diff --git a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotExtensions.kt b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotExtensions.kt index c931181091..606018acbb 100644 --- a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotExtensions.kt +++ b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotExtensions.kt @@ -1,6 +1,5 @@ package dev.inmo.tgbotapi.extensions.api -import dev.inmo.tgbotapi.bot.Ktor.KtorRequestsExecutor import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl @@ -11,11 +10,11 @@ import io.ktor.client.engine.* /** * Allows to create bot using bot [urlsKeeper] */ +@Deprecated("Replaced in core", ReplaceWith("telegramBot", "dev.inmo.tgbotapi.bot.Ktor.telegramBot")) fun telegramBot( urlsKeeper: TelegramAPIUrlsKeeper -): TelegramBot = KtorRequestsExecutor( - urlsKeeper, - HttpClient() +): TelegramBot = dev.inmo.tgbotapi.bot.Ktor.telegramBot( + urlsKeeper ) /** @@ -24,10 +23,9 @@ fun telegramBot( fun telegramBot( urlsKeeper: TelegramAPIUrlsKeeper, client: HttpClient -): TelegramBot = KtorRequestsExecutor( - urlsKeeper, - client -) +): TelegramBot = dev.inmo.tgbotapi.bot.Ktor.telegramBot(urlsKeeper) { + this.client = client +} /** * Allows to create bot using bot [urlsKeeper] and specify [HttpClientEngineFactory] by passing [clientFactory] param and optionally