mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
TelegramAPIUrlsKeeper
This commit is contained in:
parent
d8887bb7ff
commit
84ad751792
@ -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
|
||||
|
||||
|
@ -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))
|
||||
}
|
@ -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<KtorCallFactory> = 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<KtorCallFactory> = 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<KtorCallFactory> = callsFactories.run {
|
||||
@ -49,7 +61,7 @@ class KtorRequestsExecutor(
|
||||
for (factory in callsFactories) {
|
||||
call = factory.prepareCall(
|
||||
client,
|
||||
baseUrl,
|
||||
telegramAPIUrlsKeeper.commonAPIUrl,
|
||||
request
|
||||
)
|
||||
if (call != null) {
|
||||
|
@ -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
|
||||
|
@ -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<String> = ALL_UPDATES_LIST,
|
||||
@ -26,7 +26,7 @@ fun KtorUpdatesPoller(
|
||||
updatesReceiver: UpdateReceiver<Update>
|
||||
): 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<String> = ALL_UPDATES_LIST,
|
||||
exceptionsHandler: (Exception) -> Boolean = { true },
|
||||
updatesReceiver: UpdateReceiver<Update>
|
||||
): KtorUpdatesPoller {
|
||||
return KtorUpdatesPoller(
|
||||
TelegramAPIUrlsKeeper(token),
|
||||
timeoutSeconds,
|
||||
oneTimeUpdatesLimit,
|
||||
allowedUpdates,
|
||||
exceptionsHandler,
|
||||
updatesReceiver
|
||||
)
|
||||
}
|
||||
|
||||
class KtorUpdatesPoller(
|
||||
private val executor: RequestsExecutor,
|
||||
private val timeoutSeconds: Int? = null,
|
||||
|
@ -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"
|
||||
}
|
Loading…
Reference in New Issue
Block a user