diff --git a/CHANGELOG.md b/CHANGELOG.md index 54301c46b3..3eec9b1df7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ * Ktor version `1.1.4` -> `1.2.2` * `RequestsExecutor` now is `Closeable` +* `TelegramAPIUrlsKeeper` was added to provide more comfortable work with file urls and other things +like this ## 0.16.0 Bot API 4.3 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/BaseRequestsExecutor.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/BaseRequestsExecutor.kt index abaf662aa4..3aef77e43a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/BaseRequestsExecutor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/BaseRequestsExecutor.kt @@ -1,8 +1,17 @@ package com.github.insanusmokrassar.TelegramBotAPI.bot +import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper + abstract class BaseRequestsExecutor( - token: String, - hostUrl: String = "https://api.telegram.org" + protected val telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper ) : RequestsExecutor { - protected val baseUrl: String = "$hostUrl/bot$token" + @Deprecated("Deprecated due to new TelegramAPIUrlKeeper API", ReplaceWith("telegramAPIUrlsKeeper.commonAPIUrl")) + protected val baseUrl: String + get() = telegramAPIUrlsKeeper.commonAPIUrl + + @Deprecated("Deprecated due to new TelegramAPIUrlKeeper API") + constructor( + token: String, + hostUrl: String = "https://api.telegram.org" + ) : this (TelegramAPIUrlsKeeper(token, hostUrl)) } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt index ff1b43eb56..abc0e49fd8 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt @@ -9,6 +9,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.limiters.RequestL import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.types.Response import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError +import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper import io.ktor.client.HttpClient import io.ktor.client.call.HttpClientCall import io.ktor.client.engine.HttpClientEngine @@ -17,22 +18,33 @@ import kotlinx.coroutines.delay import kotlinx.serialization.json.Json class KtorRequestsExecutor( - token: String, + telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, private val client: HttpClient = HttpClient(), - hostUrl: String = "https://api.telegram.org", callsFactories: List = emptyList(), excludeDefaultFactories: Boolean = false, private val requestsLimiter: RequestLimiter = EmptyLimiter, private val jsonFormatter: Json = Json.nonstrict -) : BaseRequestsExecutor(token, hostUrl) { +) : BaseRequestsExecutor(telegramAPIUrlsKeeper) { + + @Deprecated("Deprecated due to new TelegramAPIUrlKeeper API") + constructor( + token: String, + client: HttpClient = HttpClient(), + hostUrl: String = "https://api.telegram.org", + callsFactories: List = emptyList(), + excludeDefaultFactories: Boolean = false, + requestsLimiter: RequestLimiter = EmptyLimiter, + jsonFormatter: Json = Json.nonstrict + ) : this(TelegramAPIUrlsKeeper(token, hostUrl), client, callsFactories, excludeDefaultFactories, requestsLimiter, jsonFormatter) + + @Deprecated("Deprecated due to new TelegramAPIUrlKeeper API") constructor( token: String, engine: HttpClientEngine? = null, hostUrl: String = "https://api.telegram.org" ) : this( - token, - engine ?.let { HttpClient(engine) } ?: HttpClient(), - hostUrl + TelegramAPIUrlsKeeper(token, hostUrl), + engine ?.let { HttpClient(engine) } ?: HttpClient() ) private val callsFactories: List = callsFactories.run { @@ -49,7 +61,7 @@ class KtorRequestsExecutor( for (factory in callsFactories) { call = factory.prepareCall( client, - baseUrl, + telegramAPIUrlsKeeper.commonAPIUrl, request ) if (call != null) { diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt index e7d48b5823..7ab0ab5a11 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt @@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -15,11 +16,16 @@ data class PathedFile( override val fileSize: Long? = null ): TelegramMediaFile +fun TelegramAPIUrlsKeeper.resolveFileURL(file: PathedFile): String = "$fileBaseUrl/${file.filePath}" +inline fun PathedFile.fullUrl(keeper: TelegramAPIUrlsKeeper): String = keeper.resolveFileURL(this) + +@Deprecated("Deprecated due to old API", ReplaceWith("fullUrl(telegramApiUrlsKeeper)")) fun PathedFile.makeFileUrl( botToken: String, apiHost: String = "https://api.telegram.org" ) = "${downloadingFilesBaseUrl(botToken, apiHost)}/$filePath" +@Deprecated("Deprecated due to old API", ReplaceWith("telegramApiUrlsKeeper.fileBaseUrl")) fun downloadingFilesBaseUrl( botToken: String, apiHost: String diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesPoller.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesPoller.kt index 6c6482f549..2f670113d5 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesPoller.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesPoller.kt @@ -18,7 +18,7 @@ import io.ktor.client.engine.cio.CIO import kotlinx.coroutines.* fun KtorUpdatesPoller( - token: String, + telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, timeoutSeconds: Int? = null, oneTimeUpdatesLimit: Int? = null, allowedUpdates: List = ALL_UPDATES_LIST, @@ -26,7 +26,7 @@ fun KtorUpdatesPoller( updatesReceiver: UpdateReceiver ): KtorUpdatesPoller { val executor = KtorRequestsExecutor( - token, + telegramAPIUrlsKeeper, HttpClient( CIO.create { timeoutSeconds ?.let { _ -> @@ -50,6 +50,25 @@ fun KtorUpdatesPoller( ) } +@Deprecated("Deprecated due to new TelegramAPIUrlsKeeper") +fun KtorUpdatesPoller( + token: String, + timeoutSeconds: Int? = null, + oneTimeUpdatesLimit: Int? = null, + allowedUpdates: List = ALL_UPDATES_LIST, + exceptionsHandler: (Exception) -> Boolean = { true }, + updatesReceiver: UpdateReceiver +): KtorUpdatesPoller { + return KtorUpdatesPoller( + TelegramAPIUrlsKeeper(token), + timeoutSeconds, + oneTimeUpdatesLimit, + allowedUpdates, + exceptionsHandler, + updatesReceiver + ) +} + class KtorUpdatesPoller( private val executor: RequestsExecutor, private val timeoutSeconds: Int? = null, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/TelegramAPIUrlsKeeper.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/TelegramAPIUrlsKeeper.kt new file mode 100644 index 0000000000..2fd7790c31 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/TelegramAPIUrlsKeeper.kt @@ -0,0 +1,9 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +class TelegramAPIUrlsKeeper( + token: String, + hostUrl: String = "https://api.telegram.org" +) { + val commonAPIUrl = "$hostUrl/bot$token" + val fileBaseUrl = "$hostUrl/file/bot$token" +}