diff --git a/CHANGELOG.md b/CHANGELOG.md index 67f57c994b..8891104657 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Added `InputChaining` in annotation mode as experimental feature. * Added `additionalHeaders` that will be applied to requests to `HttpConfiguration` * (maybe useful for authorization via socks proxy) +* Added `Deffered>.foldResponse()` function to handle async responses. ### 3.3.1 diff --git a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/core/TelegramActionsCollector.kt b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/core/TelegramActionsCollector.kt index 16c7858e3f..efef949e91 100644 --- a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/core/TelegramActionsCollector.kt +++ b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/core/TelegramActionsCollector.kt @@ -14,7 +14,6 @@ import eu.vendeli.tgbot.types.internal.configuration.RateLimits import mu.KotlinLogging import org.reflections.Reflections import org.reflections.scanners.Scanners -import org.reflections.util.ConfigurationBuilder import java.lang.reflect.Parameter import kotlin.reflect.jvm.kotlinFunction @@ -38,7 +37,8 @@ internal object TelegramActionsCollector { @Suppress("LongMethod") fun collect(packageName: String): Actions = with( Reflections( - ConfigurationBuilder().forPackages(packageName).addScanners(Scanners.MethodsAnnotated), + packageName, + Scanners.MethodsAnnotated, ), ) { val commands = mutableMapOf() diff --git a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/types/internal/Response.kt b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/types/internal/Response.kt index ea628fc65c..b22aec60e4 100644 --- a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/types/internal/Response.kt +++ b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/types/internal/Response.kt @@ -3,6 +3,7 @@ package eu.vendeli.tgbot.types.internal import com.fasterxml.jackson.annotation.JsonSubTypes import com.fasterxml.jackson.annotation.JsonTypeInfo import eu.vendeli.tgbot.types.ResponseParameters +import kotlinx.coroutines.Deferred @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, @@ -39,3 +40,12 @@ fun Response.getOrNull(): T? = when (this) { is Response.Success -> result else -> null } + +@Suppress("UNCHECKED_CAST") +suspend inline fun Deferred>.foldResponse( + success: Response.Success.() -> R, + failure: Response.Failure.() -> R, +): R = when (val response = await()) { + is Response.Success<*> -> success.invoke(response as Response.Success) + is Response.Failure -> failure.invoke(response) +} diff --git a/telegram-bot/src/test/kotlin/eu/vendeli/TelegramBotTest.kt b/telegram-bot/src/test/kotlin/eu/vendeli/TelegramBotTest.kt index 9b713787c8..e34ef8ccc5 100644 --- a/telegram-bot/src/test/kotlin/eu/vendeli/TelegramBotTest.kt +++ b/telegram-bot/src/test/kotlin/eu/vendeli/TelegramBotTest.kt @@ -3,6 +3,7 @@ package eu.vendeli import BotTestContext import ch.qos.logback.classic.Level import eu.vendeli.tgbot.TelegramBot +import eu.vendeli.tgbot.api.botactions.getMe import eu.vendeli.tgbot.api.getFile import eu.vendeli.tgbot.api.media.photo import eu.vendeli.tgbot.core.EnvConfigLoader @@ -18,6 +19,7 @@ import eu.vendeli.tgbot.types.internal.HttpLogLevel import eu.vendeli.tgbot.types.internal.InputFile import eu.vendeli.tgbot.types.internal.MessageUpdate import eu.vendeli.tgbot.types.internal.TgMethod +import eu.vendeli.tgbot.types.internal.foldResponse import eu.vendeli.tgbot.types.internal.getOrNull import eu.vendeli.tgbot.types.internal.isSuccess import eu.vendeli.tgbot.types.internal.onFailure @@ -89,6 +91,24 @@ class TelegramBotTest : BotTestContext() { } } + @Test + suspend fun `fold response handling`() { + val req = getMe().sendAsync(bot) + + var isFailure: Boolean? = null + req.foldResponse( + { + isFailure = false + result.isBot + }, + { + isFailure = true + }, + ) shouldBe true + + isFailure shouldBe false + } + @Test fun `input listener setting`() { bot.inputListener shouldNotBeSameInstanceAs inputListenerImpl // check default impl