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`
|
* Ktor version `1.1.4` -> `1.2.2`
|
||||||
|
|
||||||
* `RequestsExecutor` now is `Closeable`
|
* `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
|
## 0.16.0 Bot API 4.3
|
||||||
|
|
||||||
|
@ -1,8 +1,17 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.bot
|
package com.github.insanusmokrassar.TelegramBotAPI.bot
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
|
||||||
|
|
||||||
abstract class BaseRequestsExecutor(
|
abstract class BaseRequestsExecutor(
|
||||||
token: String,
|
protected val telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper
|
||||||
hostUrl: String = "https://api.telegram.org"
|
|
||||||
) : RequestsExecutor {
|
) : 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.requests.abstracts.Request
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
|
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
|
import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.call.HttpClientCall
|
import io.ktor.client.call.HttpClientCall
|
||||||
import io.ktor.client.engine.HttpClientEngine
|
import io.ktor.client.engine.HttpClientEngine
|
||||||
@ -17,22 +18,33 @@ import kotlinx.coroutines.delay
|
|||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
class KtorRequestsExecutor(
|
class KtorRequestsExecutor(
|
||||||
token: String,
|
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
|
||||||
private val client: HttpClient = HttpClient(),
|
private val client: HttpClient = HttpClient(),
|
||||||
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,
|
private val requestsLimiter: RequestLimiter = EmptyLimiter,
|
||||||
private val jsonFormatter: Json = Json.nonstrict
|
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(
|
constructor(
|
||||||
token: String,
|
token: String,
|
||||||
engine: HttpClientEngine? = null,
|
engine: HttpClientEngine? = null,
|
||||||
hostUrl: String = "https://api.telegram.org"
|
hostUrl: String = "https://api.telegram.org"
|
||||||
) : this(
|
) : this(
|
||||||
token,
|
TelegramAPIUrlsKeeper(token, hostUrl),
|
||||||
engine ?.let { HttpClient(engine) } ?: HttpClient(),
|
engine ?.let { HttpClient(engine) } ?: HttpClient()
|
||||||
hostUrl
|
|
||||||
)
|
)
|
||||||
|
|
||||||
private val callsFactories: List<KtorCallFactory> = callsFactories.run {
|
private val callsFactories: List<KtorCallFactory> = callsFactories.run {
|
||||||
@ -49,7 +61,7 @@ class KtorRequestsExecutor(
|
|||||||
for (factory in callsFactories) {
|
for (factory in callsFactories) {
|
||||||
call = factory.prepareCall(
|
call = factory.prepareCall(
|
||||||
client,
|
client,
|
||||||
baseUrl,
|
telegramAPIUrlsKeeper.commonAPIUrl,
|
||||||
request
|
request
|
||||||
)
|
)
|
||||||
if (call != null) {
|
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.requests.abstracts.FileId
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@ -15,11 +16,16 @@ data class PathedFile(
|
|||||||
override val fileSize: Long? = null
|
override val fileSize: Long? = null
|
||||||
): TelegramMediaFile
|
): 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(
|
fun PathedFile.makeFileUrl(
|
||||||
botToken: String,
|
botToken: String,
|
||||||
apiHost: String = "https://api.telegram.org"
|
apiHost: String = "https://api.telegram.org"
|
||||||
) = "${downloadingFilesBaseUrl(botToken, apiHost)}/$filePath"
|
) = "${downloadingFilesBaseUrl(botToken, apiHost)}/$filePath"
|
||||||
|
|
||||||
|
@Deprecated("Deprecated due to old API", ReplaceWith("telegramApiUrlsKeeper.fileBaseUrl"))
|
||||||
fun downloadingFilesBaseUrl(
|
fun downloadingFilesBaseUrl(
|
||||||
botToken: String,
|
botToken: String,
|
||||||
apiHost: String
|
apiHost: String
|
||||||
|
@ -18,7 +18,7 @@ import io.ktor.client.engine.cio.CIO
|
|||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
fun KtorUpdatesPoller(
|
fun KtorUpdatesPoller(
|
||||||
token: String,
|
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
|
||||||
timeoutSeconds: Int? = null,
|
timeoutSeconds: Int? = null,
|
||||||
oneTimeUpdatesLimit: Int? = null,
|
oneTimeUpdatesLimit: Int? = null,
|
||||||
allowedUpdates: List<String> = ALL_UPDATES_LIST,
|
allowedUpdates: List<String> = ALL_UPDATES_LIST,
|
||||||
@ -26,7 +26,7 @@ fun KtorUpdatesPoller(
|
|||||||
updatesReceiver: UpdateReceiver<Update>
|
updatesReceiver: UpdateReceiver<Update>
|
||||||
): KtorUpdatesPoller {
|
): KtorUpdatesPoller {
|
||||||
val executor = KtorRequestsExecutor(
|
val executor = KtorRequestsExecutor(
|
||||||
token,
|
telegramAPIUrlsKeeper,
|
||||||
HttpClient(
|
HttpClient(
|
||||||
CIO.create {
|
CIO.create {
|
||||||
timeoutSeconds ?.let { _ ->
|
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(
|
class KtorUpdatesPoller(
|
||||||
private val executor: RequestsExecutor,
|
private val executor: RequestsExecutor,
|
||||||
private val timeoutSeconds: Int? = null,
|
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