1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-02 16:05:28 +00:00
tgbotapi/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesCIOPoller.kt

77 lines
2.3 KiB
Kotlin
Raw Normal View History

2019-12-03 05:07:25 +00:00
package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
2019-12-06 10:46:38 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
2019-12-03 05:07:25 +00:00
import io.ktor.client.HttpClient
2020-02-06 06:07:23 +00:00
import io.ktor.client.engine.HttpClientEngine
2019-12-03 05:07:25 +00:00
import io.ktor.client.engine.cio.CIO
import io.ktor.client.engine.cio.endpoint
import io.ktor.util.KtorExperimentalAPI
@KtorExperimentalAPI
fun KtorUpdatesPoller(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
timeoutSeconds: Int? = null,
oneTimeUpdatesLimit: Int? = null,
allowedUpdates: List<String> = ALL_UPDATES_LIST,
exceptionsHandler: (Exception) -> Boolean = { true },
updatesReceiver: UpdateReceiver<Update>
): KtorUpdatesPoller {
val executor = KtorRequestsExecutor(
telegramAPIUrlsKeeper,
HttpClient(
CIO.create {
endpoint {
timeoutSeconds ?.times(1000) ?.also { timeOutMillis ->
keepAliveTime = timeOutMillis.toLong()
}
connectTimeout = 1000
}
}
)
)
return KtorUpdatesPoller(
executor,
timeoutSeconds,
oneTimeUpdatesLimit,
allowedUpdates,
exceptionsHandler,
updatesReceiver
)
}
2020-02-06 06:07:23 +00:00
@KtorExperimentalAPI
fun KtorUpdatesPoller(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
timeoutSeconds: Int? = null,
oneTimeUpdatesLimit: Int? = null,
allowedUpdates: List<String> = ALL_UPDATES_LIST,
exceptionsHandler: (Exception) -> Boolean = { true },
clientEngine: HttpClientEngine = CIO.create {
endpoint {
timeoutSeconds ?.times(1000) ?.also { timeOutMillis ->
keepAliveTime = timeOutMillis.toLong()
}
connectTimeout = 1000
}
},
updatesReceiver: UpdateReceiver<Update>
): KtorUpdatesPoller {
val executor = KtorRequestsExecutor(
telegramAPIUrlsKeeper,
HttpClient(clientEngine)
)
return KtorUpdatesPoller(
executor,
timeoutSeconds,
oneTimeUpdatesLimit,
allowedUpdates,
exceptionsHandler,
updatesReceiver
)
}