From bafb0c2459c4182ad57d2837b9ebbdc1b775daa3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 1 May 2022 11:21:01 +0600 Subject: [PATCH] fix incosistences after updating of dependencies --- gradle.properties | 4 ++-- .../tgbotapi/bot/ktor/KtorRequestsExecutor.kt | 7 ++++--- .../ktor/base/AbstractRequestCallFactory.kt | 13 ++++++------- .../DownloadFileChannelRequestCallFactory.kt | 8 ++++---- .../base/DownloadFileRequestCallFactory.kt | 3 ++- .../limiters/ExceptionsOnlyLimiter.kt | 2 +- .../dev/inmo/tgbotapi/types/ChatInviteLink.kt | 8 -------- .../dev/inmo/tgbotapi/types/MenuButton.kt | 4 ---- .../MessageEntity/textsources/TextSource.kt | 8 -------- .../dev/inmo/tgbotapi/types/files/Sticker.kt | 4 ---- .../kotlin/dev/inmo/tgbotapi/utils/Crypto.kt | 19 ------------------- .../tgbotapi/utils/TelegramAPIUrlsKeeper.kt | 3 +++ .../dev/inmo/tgbotapi/utils/CryptoActual.kt | 6 ------ .../dev/inmo/tgbotapi/utils/CryptoActual.kt | 14 -------------- .../utils/extensions/FilesDownloading.kt | 3 ++- .../utils/updates/retrieving/LongPolling.kt | 2 +- .../utils/updates/retrieving/Webhook.kt | 8 ++++---- 17 files changed, 29 insertions(+), 87 deletions(-) delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/Crypto.kt delete mode 100644 tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt delete mode 100644 tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt diff --git a/gradle.properties b/gradle.properties index b3044ffee1..9642c6c806 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt index 2d76e55982..3ae200c3a1 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt @@ -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, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory.kt index 5078042d5a..f4e049703e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory.kt @@ -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 { + 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() + val content = response.bodyAsText() val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content) return safelyWithResult { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory.kt index 083dcb571b..d1b9458173 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory.kt @@ -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(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() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileRequestCallFactory.kt index 4edcfda6c8..323b7b12fe 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileRequestCallFactory.kt @@ -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(fullUrl) as T // always ByteArray + client.get(fullUrl).readBytes() as T // always ByteArray } } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/ExceptionsOnlyLimiter.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/ExceptionsOnlyLimiter.kt index b3193160f0..723e88fd2e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/ExceptionsOnlyLimiter.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/ExceptionsOnlyLimiter.kt @@ -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 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt index 8b82677c24..4eb0d588b0 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt @@ -58,10 +58,6 @@ sealed interface ChatInviteLink : WithUser { override val user: User get() = creator - - companion object { - fun serializer(): KSerializer = ChatInviteLinkSerializer - } } /** @@ -71,10 +67,6 @@ sealed interface ChatInviteLink : WithUser { sealed interface SecondaryChatInviteLink : ChatInviteLink { override val isPrimary: Boolean get() = false - - companion object { - fun serializer(): KSerializer = ChatInviteLinkSerializer as KSerializer - } } /** diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MenuButton.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MenuButton.kt index 3ce057ce7c..25b9f71159 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MenuButton.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MenuButton.kt @@ -49,10 +49,6 @@ sealed interface MenuButton { override val type: String, val rawJson: JsonElement ) : MenuButton - - companion object { - fun serializer(): KSerializer = MenuButtonSerializer - } } @Serializable diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextSource.kt index 539410980d..2297b047ff 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextSource.kt @@ -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.plus(text: String) = this + regular(text) @Serializable(TextSourceSerializer::class) sealed interface MultilevelTextSource : TextSource { val subsources: List - - companion object { - fun serializer() = TextSourceSerializer - } } fun List.separateForMessage(limit: IntRange, numberOfParts: Int? = null): List> { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/Sticker.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/Sticker.kt index f8135965dc..e9832c00c2 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/Sticker.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/Sticker.kt @@ -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 = StickerSerializer - } } object StickerSerializer : KSerializer { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/Crypto.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/Crypto.kt deleted file mode 100644 index af150b34fa..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/Crypto.kt +++ /dev/null @@ -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() diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt index 649a8ccc2b..c9ffc38286 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt @@ -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 diff --git a/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt b/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt deleted file mode 100644 index a8b23eae8a..0000000000 --- a/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt +++ /dev/null @@ -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() diff --git a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt deleted file mode 100644 index 65075f0119..0000000000 --- a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt +++ /dev/null @@ -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() -} diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/FilesDownloading.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/FilesDownloading.kt index 5713ae8109..34cee8856c 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/FilesDownloading.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/FilesDownloading.kt @@ -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("${telegramAPIUrlsKeeper.fileBaseUrl}/$filePath") +) = get("${telegramAPIUrlsKeeper.fileBaseUrl}/$filePath").readBytes() suspend fun HttpClient.loadFile( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt index b58806440a..5caa189dd2 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt @@ -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.* diff --git a/tgbotapi.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt b/tgbotapi.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt index 5111c46980..04d39ed179 100644 --- a/tgbotapi.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt +++ b/tgbotapi.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt @@ -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