mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-03 23:29:33 +00:00
migration
This commit is contained in:
@@ -12,8 +12,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.HttpClientCall
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import io.ktor.util.cio.toByteArray
|
||||
import io.ktor.client.call.receive
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
@@ -49,9 +48,7 @@ class KtorRequestsExecutor(
|
||||
if (call == null) {
|
||||
throw IllegalArgumentException("Can't execute request: $request")
|
||||
}
|
||||
val content = call.response.use {
|
||||
it.content.toByteArray().toString(Charsets.UTF_8)
|
||||
}
|
||||
val content = call.response.receive<String>()
|
||||
val responseObject = jsonFormatter.parse(Response.serializer(), content)
|
||||
|
||||
(responseObject.result ?.let {
|
@@ -1,10 +1,11 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.bot
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
||||
import kotlinx.io.core.Closeable
|
||||
|
||||
interface RequestsExecutor : Closeable {
|
||||
@Throws(RequestException::class)
|
||||
/**
|
||||
* @throws com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException
|
||||
*/
|
||||
suspend fun <T : Any> execute(request: Request<T>): T
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
|
||||
import java.io.IOException
|
||||
import kotlinx.io.errors.IOException
|
||||
|
||||
fun newRequestException(
|
||||
response: Response,
|
||||
@@ -20,7 +20,7 @@ sealed class RequestException constructor(
|
||||
message: String? = null,
|
||||
cause: Throwable? = null
|
||||
) : IOException(
|
||||
message,
|
||||
message ?: "Something went wrong",
|
||||
cause
|
||||
)
|
||||
|
@@ -1,9 +1,10 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.bot.settings.limiters
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
|
||||
private fun now(): Long = System.currentTimeMillis()
|
||||
private fun now(): Long = DateTime.nowUnixLong()
|
||||
|
||||
class CommonLimiter(
|
||||
private val lockCount: Int = 10,
|
@@ -4,8 +4,8 @@ import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
import java.util.concurrent.Executors
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.math.pow
|
||||
|
||||
private sealed class RequestEvent
|
||||
private class AddRequest(
|
||||
@@ -21,9 +21,7 @@ data class PowLimiter(
|
||||
private val powK: Double = 0.0016
|
||||
) : RequestLimiter {
|
||||
@Transient
|
||||
private val scope = CoroutineScope(
|
||||
Executors.newFixedThreadPool(3).asCoroutineDispatcher()
|
||||
)
|
||||
private val scope = CoroutineScope(Dispatchers.Default)
|
||||
@Transient
|
||||
private val eventsChannel = Channel<RequestEvent>(Channel.UNLIMITED)
|
||||
@Transient
|
||||
@@ -35,7 +33,7 @@ data class PowLimiter(
|
||||
for (event in eventsChannel) {
|
||||
when (event) {
|
||||
is AddRequest -> {
|
||||
val awaitTime = ((Math.pow(requestsInWork, powValue) * powK) * 1000L).toLong()
|
||||
val awaitTime = (((requestsInWork.pow(powValue) * powK) * 1000L).toLong())
|
||||
requestsInWork++
|
||||
|
||||
event.continuation.resume(
|
@@ -3,7 +3,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.StringDescriptor
|
||||
import java.io.File
|
||||
|
||||
@Serializable(InputFileSerializer::class)
|
||||
sealed class InputFile {
|
||||
@@ -40,7 +39,3 @@ data class MultipartFile (
|
||||
) : InputFile() {
|
||||
override val fileId: String = file.generateCustomName()
|
||||
}
|
||||
|
||||
fun File.toInputFile(): InputFile = MultipartFile(
|
||||
StorageFile(this)
|
||||
)
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user