1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 00:03:48 +00:00

fix incosistences after updating of dependencies

This commit is contained in:
InsanusMokrassar 2022-05-01 11:21:01 +06:00
parent 9be2b899cb
commit bafb0c2459
17 changed files with 29 additions and 87 deletions

View File

@ -12,7 +12,7 @@ klock_version=2.7.0
uuid_version=0.4.0
ktor_version=2.0.0
micro_utils_version=0.10.0
micro_utils_version=0.10.1
javax_activation_version=1.1.1
@ -22,4 +22,4 @@ dokka_version=1.6.21
library_group=dev.inmo
library_version=1.0.0
github_release_plugin_version=2.3.12
github_release_plugin_version=2.3.7

View File

@ -11,7 +11,8 @@ import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.Response
import dev.inmo.tgbotapi.utils.*
import io.ktor.client.HttpClient
import io.ktor.client.features.*
import io.ktor.client.plugins.*
import io.ktor.client.statement.bodyAsText
import io.ktor.client.statement.readText
import kotlinx.serialization.json.Json
@ -41,7 +42,7 @@ class KtorRequestsExecutor(
}
private val client = client.config {
if (client.feature(HttpTimeout) == null) {
if (client.pluginOrNull(HttpTimeout) == null) {
install(HttpTimeout)
}
}
@ -53,7 +54,7 @@ class KtorRequestsExecutor(
pipelineStepsHolder.onRequestException(request, e) ?.let { return@safely it }
throw if (e is ClientRequestException) {
val content = e.response.readText()
val content = e.response.bodyAsText()
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
newRequestException(
responseObject,

View File

@ -8,10 +8,9 @@ import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.Response
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient
import io.ktor.client.call.receive
import io.ktor.client.features.timeout
import io.ktor.client.plugins.timeout
import io.ktor.client.request.*
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import kotlinx.serialization.json.Json
import kotlin.collections.set
@ -28,7 +27,7 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
): T? {
val preparedBody = prepareCallBody(client, urlsKeeper, request) ?: return null
client.post<HttpResponse> {
client.post {
url(
methodsCache[request.method()] ?: "${urlsKeeper.commonAPIUrl}/${request.method()}".also {
methodsCache[request.method()] = it
@ -37,7 +36,7 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
accept(ContentType.Application.Json)
if (request is GetUpdates) {
request.timeout?.times(1000L)?.let { customTimeoutMillis ->
request.timeout?.times(1000L) ?.let { customTimeoutMillis ->
if (customTimeoutMillis > 0) {
timeout {
requestTimeoutMillis = customTimeoutMillis
@ -52,9 +51,9 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
}
}
body = preparedBody
setBody(preparedBody)
}.let { response ->
val content = response.receive<String>()
val content = response.bodyAsText()
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
return safelyWithResult {

View File

@ -10,6 +10,7 @@ import io.ktor.client.HttpClient
import io.ktor.client.call.receive
import io.ktor.client.request.get
import io.ktor.client.statement.HttpStatement
import io.ktor.client.statement.bodyAsChannel
import io.ktor.utils.io.*
import kotlinx.coroutines.*
import kotlinx.serialization.json.Json
@ -28,10 +29,9 @@ object DownloadFileChannelRequestCallFactory : KtorCallFactory {
val outChannel = ByteChannel()
scope.launch {
runCatchingSafely {
client.get<HttpStatement>(fullUrl).execute { httpResponse ->
val channel: ByteReadChannel = httpResponse.receive()
channel.copyAndClose(outChannel)
}
val response = client.get(fullUrl)
val channel: ByteReadChannel = response.bodyAsChannel()
channel.copyAndClose(outChannel)
}
scope.cancel()
}

View File

@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.statement.readBytes
import kotlinx.serialization.json.Json
object DownloadFileRequestCallFactory : KtorCallFactory {
@ -20,7 +21,7 @@ object DownloadFileRequestCallFactory : KtorCallFactory {
safely {
@Suppress("UNCHECKED_CAST")
client.get<ByteArray>(fullUrl) as T // always ByteArray
client.get(fullUrl).readBytes() as T // always ByteArray
}
}
}

View File

@ -4,7 +4,7 @@ import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.tgbotapi.bot.exceptions.TooMuchRequestsException
import dev.inmo.tgbotapi.types.MilliSeconds
import dev.inmo.tgbotapi.types.RetryAfterError
import io.ktor.client.features.ClientRequestException
import io.ktor.client.plugins.ClientRequestException
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow

View File

@ -58,10 +58,6 @@ sealed interface ChatInviteLink : WithUser {
override val user: User
get() = creator
companion object {
fun serializer(): KSerializer<ChatInviteLink> = ChatInviteLinkSerializer
}
}
/**
@ -71,10 +67,6 @@ sealed interface ChatInviteLink : WithUser {
sealed interface SecondaryChatInviteLink : ChatInviteLink {
override val isPrimary: Boolean
get() = false
companion object {
fun serializer(): KSerializer<SecondaryChatInviteLink> = ChatInviteLinkSerializer as KSerializer<SecondaryChatInviteLink>
}
}
/**

View File

@ -49,10 +49,6 @@ sealed interface MenuButton {
override val type: String,
val rawJson: JsonElement
) : MenuButton
companion object {
fun serializer(): KSerializer<MenuButton> = MenuButtonSerializer
}
}
@Serializable

View File

@ -18,10 +18,6 @@ sealed interface TextSource {
val asText: String
get() = source
companion object {
fun serializer() = TextSourceSerializer
}
}
@Suppress("NOTHING_TO_INLINE")
@ -36,10 +32,6 @@ inline operator fun List<TextSource>.plus(text: String) = this + regular(text)
@Serializable(TextSourceSerializer::class)
sealed interface MultilevelTextSource : TextSource {
val subsources: List<TextSource>
companion object {
fun serializer() = TextSourceSerializer
}
}
fun List<TextSource>.separateForMessage(limit: IntRange, numberOfParts: Int? = null): List<List<TextSource>> {

View File

@ -36,10 +36,6 @@ sealed interface Sticker : TelegramMediaFile, SizedMediaFile, ThumbedMediaFile {
get() = this is AnimatedSticker
val isVideo
get() = this is VideoSticker
companion object {
fun serializer(): KSerializer<Sticker> = StickerSerializer
}
}
object StickerSerializer : KSerializer<Sticker> {

View File

@ -1,19 +0,0 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.micro_utils.crypto.SourceString
internal expect fun SourceString.hmacSha256(key: String): String
private val HEX_ARRAY = "0123456789abcdef".toCharArray()
internal fun SourceBytes.hex(): String {
val hexChars = CharArray(size * 2)
for (j in indices) {
val v: Int = this[j].toInt() and 0xFF
hexChars[j * 2] = HEX_ARRAY[v ushr 4]
hexChars[j * 2 + 1] = HEX_ARRAY[v and 0x0F]
}
return hexChars.concatToString()
}
internal fun SourceString.hex(): String = encodeToByteArray().hex()

View File

@ -1,5 +1,8 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.crypto.hex
import dev.inmo.micro_utils.crypto.hmacSha256
const val telegramBotAPIDefaultUrl = "https://api.telegram.org"
private inline val String.withoutLastSlash: String

View File

@ -1,6 +0,0 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.crypto.CryptoJS
import dev.inmo.micro_utils.crypto.SourceString
actual fun SourceString.hmacSha256(key: String) = CryptoJS.asDynamic().HmacSHA256(this, key).toString().unsafeCast<String>()

View File

@ -1,14 +0,0 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.crypto.SourceString
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
actual fun SourceString.hmacSha256(key: String): String {
val mac = Mac.getInstance("HmacSHA256")
val secretKey = SecretKeySpec(key.toByteArray(), "HmacSHA256")
mac.init(secretKey)
return mac.doFinal(toByteArray()).hex()
}

View File

@ -4,11 +4,12 @@ import dev.inmo.tgbotapi.types.files.PathedFile
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.statement.readBytes
suspend fun HttpClient.loadFile(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
filePath: String
) = get<ByteArray>("${telegramAPIUrlsKeeper.fileBaseUrl}/$filePath")
) = get("${telegramAPIUrlsKeeper.fileBaseUrl}/$filePath").readBytes()
suspend fun HttpClient.loadFile(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,

View File

@ -14,7 +14,7 @@ import dev.inmo.tgbotapi.types.update.MediaGroupUpdates.*
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.*
import dev.inmo.tgbotapi.utils.*
import io.ktor.client.features.HttpRequestTimeoutException
import io.ktor.client.plugins.HttpRequestTimeoutException
import io.ktor.utils.io.CancellationException
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

View File

@ -10,11 +10,11 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.types.update.abstracts.UpdateDeserializationStrategy
import dev.inmo.tgbotapi.updateshandlers.*
import dev.inmo.tgbotapi.updateshandlers.webhook.WebhookPrivateKeyConfig
import io.ktor.application.call
import io.ktor.request.receiveText
import io.ktor.response.respond
import io.ktor.routing.*
import io.ktor.server.application.call
import io.ktor.server.engine.*
import io.ktor.server.request.receiveText
import io.ktor.server.response.respond
import io.ktor.server.routing.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.Executors