tgbotapi/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotExtensions.kt

113 lines
3.4 KiB
Kotlin
Raw Normal View History

package dev.inmo.tgbotapi.extensions.api
2020-05-12 12:52:37 +00:00
2021-01-09 12:25:11 +00:00
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
2021-01-09 14:59:37 +00:00
import dev.inmo.tgbotapi.bot.TelegramBot
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
2020-11-06 06:07:46 +00:00
import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl
2020-05-12 12:52:37 +00:00
import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
2020-11-06 06:29:51 +00:00
import io.ktor.client.engine.*
2020-05-12 12:52:37 +00:00
/**
* Allows to create bot using bot [urlsKeeper] and already prepared [client]
*/
fun telegramBot(
urlsKeeper: TelegramAPIUrlsKeeper,
2020-11-06 12:52:59 +00:00
client: HttpClient
2021-01-09 12:25:11 +00:00
): TelegramBot = telegramBot(urlsKeeper) {
2020-11-17 10:06:25 +00:00
this.client = client
}
2020-05-12 12:52:37 +00:00
2020-11-06 06:29:51 +00:00
/**
* 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 <T: HttpClientEngineConfig> telegramBot(
urlsKeeper: TelegramAPIUrlsKeeper,
clientFactory: HttpClientEngineFactory<T>,
noinline clientConfig: HttpClientConfig<T>.() -> Unit = {}
) = telegramBot(
urlsKeeper,
HttpClient(clientFactory, clientConfig)
)
2020-05-12 12:52:37 +00:00
/**
* Allows to create bot using bot [urlsKeeper] and specify [HttpClientEngine] by passing [clientEngine] param and optionally
* configure [HttpClient] using [clientConfig]
*/
2020-11-06 06:29:51 +00:00
@Suppress("NOTHING_TO_INLINE")
inline fun telegramBot(
2020-05-12 12:52:37 +00:00
urlsKeeper: TelegramAPIUrlsKeeper,
clientEngine: HttpClientEngine,
2020-11-06 06:29:51 +00:00
noinline clientConfig: HttpClientConfig<*>.() -> Unit = {}
) = telegramBot(
2020-05-12 12:52:37 +00:00
urlsKeeper,
HttpClient(clientEngine, clientConfig)
)
/**
2020-11-06 06:29:51 +00:00
* Allows to create bot using bot [urlsKeeper] and specify [HttpClientEngine] by configuring [HttpClient] using
* [clientConfig]
2020-05-12 12:52:37 +00:00
*/
2020-11-06 06:29:51 +00:00
@Suppress("NOTHING_TO_INLINE")
inline fun telegramBot(
2020-05-12 12:52:37 +00:00
urlsKeeper: TelegramAPIUrlsKeeper,
2020-11-06 06:29:51 +00:00
noinline clientConfig: HttpClientConfig<*>.() -> Unit
) = telegramBot(
2020-05-12 12:52:37 +00:00
urlsKeeper,
HttpClient(clientConfig)
)
/**
2020-11-06 06:29:51 +00:00
* Allows to create bot using bot [token], [apiUrl] (for custom api servers) and already prepared [client]
2020-05-12 12:52:37 +00:00
*/
2020-11-06 06:29:51 +00:00
@Suppress("NOTHING_TO_INLINE")
inline fun telegramBot(
2020-11-06 06:07:46 +00:00
token: String,
2020-11-06 06:29:51 +00:00
apiUrl: String = telegramBotAPIDefaultUrl,
2020-11-06 12:52:59 +00:00
client: HttpClient
2020-11-06 06:29:51 +00:00
): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, apiUrl), client)
2020-05-12 12:52:37 +00:00
2020-11-06 06:29:51 +00:00
@Suppress("NOTHING_TO_INLINE")
inline fun <T: HttpClientEngineConfig> telegramBot(
2020-05-12 12:52:37 +00:00
token: String,
2020-11-06 06:29:51 +00:00
clientFactory: HttpClientEngineFactory<T>,
apiUrl: String = telegramBotAPIDefaultUrl,
noinline clientConfig: HttpClientConfig<T>.() -> Unit = {}
) = telegramBot(
TelegramAPIUrlsKeeper(token, apiUrl),
clientFactory,
clientConfig
)
2020-05-12 12:52:37 +00:00
/**
2020-11-06 06:29:51 +00:00
* Allows to create bot using bot [token] and specify [HttpClientEngine] by passing [clientEngine] param and optionally
* configure [HttpClient] using [clientConfig]
2020-05-12 12:52:37 +00:00
*/
2020-11-06 06:29:51 +00:00
@Suppress("NOTHING_TO_INLINE")
inline fun telegramBot(
2020-05-12 12:52:37 +00:00
token: String,
2020-11-06 06:29:51 +00:00
clientEngine: HttpClientEngine,
2020-11-06 06:07:46 +00:00
apiUrl: String = telegramBotAPIDefaultUrl,
2020-11-06 06:29:51 +00:00
noinline clientConfig: HttpClientConfig<*>.() -> Unit = {}
) = telegramBot(
TelegramAPIUrlsKeeper(token, apiUrl),
clientEngine,
clientConfig
)
2020-11-06 06:07:46 +00:00
2020-11-06 06:29:51 +00:00
/**
* Allows to create bot using bot [token] and [apiUrl] and specify [HttpClientEngine] by configuring [HttpClient] using
* [clientConfig]
*/
2020-11-06 06:07:46 +00:00
@Suppress("NOTHING_TO_INLINE")
2020-11-06 06:29:51 +00:00
inline fun telegramBot(
2020-11-06 06:07:46 +00:00
token: String,
apiUrl: String = telegramBotAPIDefaultUrl,
2020-11-06 12:52:59 +00:00
noinline clientConfig: HttpClientConfig<*>.() -> Unit
2020-11-06 06:29:51 +00:00
) = telegramBot(
TelegramAPIUrlsKeeper(token, apiUrl),
clientConfig
)