From 6ec0fcadd2f8f3819edceca55124f0e90953afc0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 6 Nov 2020 12:29:51 +0600 Subject: [PATCH] rewrite telegramBot functions --- .../tgbotapi/extensions/api/BotExtensions.kt | 140 +++++++++++++----- 1 file changed, 99 insertions(+), 41 deletions(-) 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 2300eeaba3..f3f02d62a2 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 @@ -6,7 +6,7 @@ import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl import io.ktor.client.HttpClient import io.ktor.client.HttpClientConfig -import io.ktor.client.engine.HttpClientEngine +import io.ktor.client.engine.* /** * Allows to create bot using bot [urlsKeeper] and already prepared [client] @@ -19,14 +19,108 @@ fun telegramBot( client ) +/** + * Allows to create bot using bot [urlsKeeper] and specify [HttpClientEngineFactory] by passing [clientFactory] param and optionally + * configure it with [clientConfig] + */ +@Suppress("NOTHING_TO_INLINE") +inline fun telegramBot( + urlsKeeper: TelegramAPIUrlsKeeper, + clientFactory: HttpClientEngineFactory, + noinline clientConfig: HttpClientConfig.() -> Unit = {} +) = telegramBot( + urlsKeeper, + HttpClient(clientFactory, clientConfig) +) + /** * Allows to create bot using bot [urlsKeeper] and specify [HttpClientEngine] by passing [clientEngine] param and optionally * configure [HttpClient] using [clientConfig] */ +@Suppress("NOTHING_TO_INLINE") +inline fun telegramBot( + urlsKeeper: TelegramAPIUrlsKeeper, + clientEngine: HttpClientEngine, + noinline clientConfig: HttpClientConfig<*>.() -> Unit = {} +) = telegramBot( + urlsKeeper, + HttpClient(clientEngine, clientConfig) +) + +/** + * Allows to create bot using bot [urlsKeeper] and specify [HttpClientEngine] by configuring [HttpClient] using + * [clientConfig] + */ +@Suppress("NOTHING_TO_INLINE") +inline fun telegramBot( + urlsKeeper: TelegramAPIUrlsKeeper, + noinline clientConfig: HttpClientConfig<*>.() -> Unit +) = telegramBot( + urlsKeeper, + HttpClient(clientConfig) +) + +/** + * Allows to create bot using bot [token], [apiUrl] (for custom api servers) and already prepared [client] + */ +@Suppress("NOTHING_TO_INLINE") +inline fun telegramBot( + token: String, + apiUrl: String = telegramBotAPIDefaultUrl, + client: HttpClient = HttpClient() +): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, apiUrl), client) + +@Suppress("NOTHING_TO_INLINE") +inline fun telegramBot( + token: String, + clientFactory: HttpClientEngineFactory, + apiUrl: String = telegramBotAPIDefaultUrl, + noinline clientConfig: HttpClientConfig.() -> Unit = {} +) = telegramBot( + TelegramAPIUrlsKeeper(token, apiUrl), + clientFactory, + clientConfig +) + +/** + * Allows to create bot using bot [token] and specify [HttpClientEngine] by passing [clientEngine] param and optionally + * configure [HttpClient] using [clientConfig] + */ +@Suppress("NOTHING_TO_INLINE") +inline fun telegramBot( + token: String, + clientEngine: HttpClientEngine, + apiUrl: String = telegramBotAPIDefaultUrl, + noinline clientConfig: HttpClientConfig<*>.() -> Unit = {} +) = telegramBot( + TelegramAPIUrlsKeeper(token, apiUrl), + clientEngine, + clientConfig +) + +/** + * Allows to create bot using bot [token] and [apiUrl] and specify [HttpClientEngine] by configuring [HttpClient] using + * [clientConfig] + */ +@Suppress("NOTHING_TO_INLINE") +inline fun telegramBot( + token: String, + apiUrl: String = telegramBotAPIDefaultUrl, + noinline clientConfig: HttpClientConfig<*>.() -> Unit = {} +) = telegramBot( + TelegramAPIUrlsKeeper(token, apiUrl), + clientConfig +) + +/** + * Allows to create bot using bot [urlsKeeper] and specify [HttpClientEngine] by passing [clientEngine] param and optionally + * configure [HttpClient] using [clientConfig] + */ +@Deprecated("Will be removed in next releases", ReplaceWith("telegramBot", "dev.inmo.tgbotapi.extensions.api.telegramBot")) fun telegramBotWithCustomClientConfig( urlsKeeper: TelegramAPIUrlsKeeper, clientEngine: HttpClientEngine, - clientConfig: HttpClientConfig<*>.() -> Unit = {} + clientConfig: HttpClientConfig<*>.() -> Unit ): TelegramBot = telegramBot( urlsKeeper, HttpClient(clientEngine, clientConfig) @@ -35,55 +129,19 @@ fun telegramBotWithCustomClientConfig( /** * Allows to create bot using bot [urlsKeeper] and optionally configure [HttpClient] using [clientConfig] */ +@Deprecated("Will be removed in next releases", ReplaceWith("telegramBot", "dev.inmo.tgbotapi.extensions.api.telegramBot")) fun telegramBotWithCustomClientConfig( urlsKeeper: TelegramAPIUrlsKeeper, - clientConfig: HttpClientConfig<*>.() -> Unit = {} + clientConfig: HttpClientConfig<*>.() -> Unit ): TelegramBot = telegramBot( urlsKeeper, HttpClient(clientConfig) ) -/** - * Allows to create bot using bot [token] - */ -fun telegramBot( - token: String, - apiUrl: String = telegramBotAPIDefaultUrl -): TelegramBot = telegramBotWithCustomClientConfig(TelegramAPIUrlsKeeper(token, apiUrl)) - -/** - * Allows to create bot using bot [token] and already prepared [client] - */ -fun telegramBot( - token: String, - client: HttpClient, - apiUrl: String = telegramBotAPIDefaultUrl -): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, apiUrl), client) - -/** - * Allows to create bot using bot [token] and configure [HttpClient] using [clientConfig] - */ -fun telegramBot( - token: String, - apiUrl: String = telegramBotAPIDefaultUrl, - clientConfig: HttpClientConfig<*>.() -> Unit -): TelegramBot = telegramBotWithCustomClientConfig(TelegramAPIUrlsKeeper(token, apiUrl), clientConfig) - @Suppress("NOTHING_TO_INLINE") @Deprecated("Renamed", ReplaceWith("telegramBot", "dev.inmo.tgbotapi.extensions.api.telegramBot")) inline fun telegramBotWithCustomClientConfig( token: String, apiUrl: String = telegramBotAPIDefaultUrl, noinline clientConfig: HttpClientConfig<*>.() -> Unit -) = telegramBot(token, apiUrl, clientConfig) - -/** - * Allows to create bot using bot [token] and specify [HttpClientEngine] by passing [clientEngine] param and optionally - * configure [HttpClient] using [clientConfig] - */ -fun telegramBot( - token: String, - clientEngine: HttpClientEngine, - apiUrl: String = telegramBotAPIDefaultUrl, - clientConfig: HttpClientConfig<*>.() -> Unit = {} -): TelegramBot = telegramBotWithCustomClientConfig(TelegramAPIUrlsKeeper(token, apiUrl), clientEngine, clientConfig) +) = telegramBot(token, apiUrl = apiUrl, clientConfig = clientConfig)