package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request 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 RequestsExecutor.executeAsync( request: Request, onFail: (suspend (Response) -> Unit)? = null, scope: CoroutineScope = GlobalScope, onSuccess: (suspend (T) -> Unit)? = null ): Job { return scope.launch { try { val result = execute(request) onSuccess ?.invoke(result) } catch (e: RequestException) { onFail ?.invoke(e.response) } } } @Deprecated("Replaced and modified inside of TelegramBotAPI-extensions-utils") fun RequestsExecutor.executeAsync( request: Request, scope: CoroutineScope = GlobalScope ): Deferred { return scope.async { execute(request) } } @Deprecated("Replaced and modified inside of TelegramBotAPI-extensions-utils") suspend fun RequestsExecutor.executeUnsafe( request: Request, retries: Int = 0, retriesDelay: Long = 1000L, onAllFailed: (suspend (exceptions: Array) -> Unit)? = null ): T? { var leftRetries = retries val exceptions = onAllFailed ?.let { mutableListOf() } do { handleSafely( { leftRetries-- delay(retriesDelay) exceptions ?.add(it) null } ) { execute(request) } ?.let { return it } } while(leftRetries >= 0) onAllFailed ?.invoke(exceptions ?.toTypedArray() ?: emptyArray()) return null }