mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 16:47:13 +00:00
executes was replaced
This commit is contained in:
parent
6073d914d5
commit
1fb2ecf15f
@ -62,6 +62,7 @@
|
||||
* All `String` formatting public extensions and functions
|
||||
* All extensions like `CaptionedInput#toHtmlCaptions`
|
||||
* All helper extensions for `List<BaseMessageUpdate>`
|
||||
* All `RequestsExecutor#executeAsync` and `RequestsExecutor#executeUnsafe`
|
||||
* `TelegramBotAPI-extensions-utils`:
|
||||
* `safely` function was introduced. It is in `PreviewFeature` state currently
|
||||
* `makeLinkToMessage` extensions has been added
|
||||
@ -76,6 +77,7 @@
|
||||
* `SentMediaGroupUpdate#chat`
|
||||
* `SentMediaGroupUpdate#mediaGroupId`
|
||||
* Several `List<MediaGroupMessage>.createResend` extensions were added
|
||||
* `RequestsExecutor#executeAsync` and `RequestsExecutor#executeUnsafe`
|
||||
|
||||
### 0.27.4
|
||||
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
fun <T: Any> RequestsExecutor.executeAsync(
|
||||
request: Request<T>,
|
||||
scope: CoroutineScope
|
||||
): Deferred<T> = scope.async {
|
||||
handleSafely {
|
||||
execute(request)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun <T: Any> RequestsExecutor.executeAsync(
|
||||
request: Request<T>
|
||||
): Deferred<T> = coroutineScope {
|
||||
executeAsync(request, this)
|
||||
}
|
||||
|
||||
suspend fun <T: Any> RequestsExecutor.executeUnsafe(
|
||||
request: Request<T>,
|
||||
retries: Int = 0,
|
||||
retriesDelay: Long = 1000L,
|
||||
onAllFailed: (suspend (exceptions: Array<Exception>) -> Unit)? = null
|
||||
): T? {
|
||||
var leftRetries = retries
|
||||
val exceptions = onAllFailed ?.let { mutableListOf<Exception>() }
|
||||
do {
|
||||
return handleSafely(
|
||||
{
|
||||
leftRetries--
|
||||
delay(retriesDelay)
|
||||
exceptions ?.add(it)
|
||||
null
|
||||
}
|
||||
) {
|
||||
execute(request)
|
||||
} ?: continue
|
||||
} while(leftRetries >= 0)
|
||||
onAllFailed ?.invoke(exceptions ?.toTypedArray() ?: emptyArray())
|
||||
return null
|
||||
}
|
@ -13,8 +13,8 @@ import io.ktor.utils.io.core.Closeable
|
||||
interface RequestsExecutor : Closeable {
|
||||
/**
|
||||
* Unsafe execution of incoming [request]. Can throw almost any exception. So, it is better to use
|
||||
* something like [com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeAsync] or
|
||||
* [com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeUnsafe]
|
||||
* something like [com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts.executeAsync] or
|
||||
* [com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts.executeUnsafe]
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
|
@ -7,7 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.Response
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
|
||||
@Deprecated("Will be removed in next major update")
|
||||
fun <T: Any> RequestsExecutor.executeAsync(
|
||||
request: Request<T>,
|
||||
onFail: (suspend (Response) -> Unit)? = null,
|
||||
@ -24,6 +24,7 @@ fun <T: Any> RequestsExecutor.executeAsync(
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated("Replaced and modified inside of TelegramBotAPI-extensions-utils")
|
||||
fun <T: Any> RequestsExecutor.executeAsync(
|
||||
request: Request<T>,
|
||||
scope: CoroutineScope = GlobalScope
|
||||
@ -31,6 +32,7 @@ fun <T: Any> RequestsExecutor.executeAsync(
|
||||
return scope.async { execute(request) }
|
||||
}
|
||||
|
||||
@Deprecated("Replaced and modified inside of TelegramBotAPI-extensions-utils")
|
||||
suspend fun <T: Any> RequestsExecutor.executeUnsafe(
|
||||
request: Request<T>,
|
||||
retries: Int = 0,
|
||||
|
Loading…
Reference in New Issue
Block a user