2020-10-04 11:06:30 +00:00
|
|
|
package dev.inmo.tgbotapi.bot.Ktor
|
2018-12-26 08:07:24 +00:00
|
|
|
|
2020-10-04 11:06:30 +00:00
|
|
|
import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
|
|
|
|
import dev.inmo.tgbotapi.bot.Ktor.base.*
|
|
|
|
import dev.inmo.tgbotapi.bot.exceptions.newRequestException
|
|
|
|
import dev.inmo.tgbotapi.bot.settings.limiters.EmptyLimiter
|
|
|
|
import dev.inmo.tgbotapi.bot.settings.limiters.RequestLimiter
|
|
|
|
import dev.inmo.tgbotapi.requests.abstracts.Request
|
|
|
|
import dev.inmo.tgbotapi.types.Response
|
|
|
|
import dev.inmo.tgbotapi.utils.*
|
2018-12-26 08:07:24 +00:00
|
|
|
import io.ktor.client.HttpClient
|
2020-03-19 05:10:18 +00:00
|
|
|
import io.ktor.client.features.*
|
2020-01-15 06:28:06 +00:00
|
|
|
import io.ktor.client.statement.HttpStatement
|
|
|
|
import io.ktor.client.statement.readText
|
2019-02-21 06:21:33 +00:00
|
|
|
import kotlinx.serialization.json.Json
|
2018-12-26 08:07:24 +00:00
|
|
|
|
|
|
|
class KtorRequestsExecutor(
|
2019-07-25 08:25:18 +00:00
|
|
|
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
|
2020-03-17 15:24:04 +00:00
|
|
|
client: HttpClient = HttpClient(),
|
2018-12-26 08:07:24 +00:00
|
|
|
callsFactories: List<KtorCallFactory> = emptyList(),
|
2019-01-24 02:36:44 +00:00
|
|
|
excludeDefaultFactories: Boolean = false,
|
2019-02-05 04:30:09 +00:00
|
|
|
private val requestsLimiter: RequestLimiter = EmptyLimiter,
|
2020-03-22 07:37:01 +00:00
|
|
|
private val jsonFormatter: Json = nonstrictJsonFormat
|
2019-07-25 08:25:18 +00:00
|
|
|
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
2018-12-26 08:07:24 +00:00
|
|
|
private val callsFactories: List<KtorCallFactory> = callsFactories.run {
|
|
|
|
if (!excludeDefaultFactories) {
|
2020-08-23 15:43:58 +00:00
|
|
|
this + listOf(SimpleRequestCallFactory, MultipartRequestCallFactory, DownloadFileRequestCallFactory)
|
2018-12-26 08:07:24 +00:00
|
|
|
} else {
|
|
|
|
this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 15:24:04 +00:00
|
|
|
private val client = client.config {
|
2020-03-19 05:10:18 +00:00
|
|
|
if (client.feature(HttpTimeout) == null) {
|
|
|
|
install(HttpTimeout)
|
|
|
|
}
|
2020-03-17 15:24:04 +00:00
|
|
|
}
|
2019-08-17 05:50:46 +00:00
|
|
|
|
2020-03-17 15:24:04 +00:00
|
|
|
override suspend fun <T : Any> execute(request: Request<T>): T {
|
2020-04-13 06:40:14 +00:00
|
|
|
return handleSafely(
|
|
|
|
{ e ->
|
|
|
|
throw if (e is ClientRequestException) {
|
2020-08-18 18:28:16 +00:00
|
|
|
val content = e.response ?.readText() ?: throw e
|
2020-08-18 06:50:11 +00:00
|
|
|
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
|
2020-04-13 06:40:14 +00:00
|
|
|
newRequestException(
|
|
|
|
responseObject,
|
|
|
|
content,
|
|
|
|
"Can't get result object from $content"
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
requestsLimiter.limit {
|
2020-08-23 15:43:58 +00:00
|
|
|
var result: T? = null
|
|
|
|
for (potentialFactory in callsFactories) {
|
|
|
|
result = potentialFactory.makeCall(
|
2020-04-13 06:40:14 +00:00
|
|
|
client,
|
2020-08-23 15:43:58 +00:00
|
|
|
telegramAPIUrlsKeeper,
|
|
|
|
request,
|
|
|
|
jsonFormatter
|
2020-04-13 06:40:14 +00:00
|
|
|
)
|
2020-08-23 15:43:58 +00:00
|
|
|
if (result != null) {
|
2020-04-13 06:40:14 +00:00
|
|
|
break
|
2019-12-13 17:54:02 +00:00
|
|
|
}
|
2020-04-13 06:40:14 +00:00
|
|
|
}
|
2020-03-17 15:24:04 +00:00
|
|
|
|
2020-08-23 15:43:58 +00:00
|
|
|
result ?: error("Can't execute request: $request")
|
2019-12-13 17:54:02 +00:00
|
|
|
}
|
2018-12-26 08:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-22 23:46:19 +00:00
|
|
|
|
|
|
|
override fun close() {
|
|
|
|
client.close()
|
|
|
|
}
|
2018-12-26 08:07:24 +00:00
|
|
|
}
|