Skip to content

Commit

Permalink
add ext function to response
Browse files Browse the repository at this point in the history
  • Loading branch information
vendelieu committed Nov 27, 2023
1 parent f79bcc7 commit a8afb3d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response<T>>.foldResponse()` function to handle async responses.

### 3.3.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<String, Invocation>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -39,3 +40,12 @@ fun <T> Response<T>.getOrNull(): T? = when (this) {
is Response.Success<T> -> result
else -> null
}

@Suppress("UNCHECKED_CAST")
suspend inline fun <T, R> Deferred<Response<out T>>.foldResponse(
success: Response.Success<T>.() -> R,
failure: Response.Failure.() -> R,
): R = when (val response = await()) {
is Response.Success<*> -> success.invoke(response as Response.Success<T>)
is Response.Failure -> failure.invoke(response)
}
20 changes: 20 additions & 0 deletions telegram-bot/src/test/kotlin/eu/vendeli/TelegramBotTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a8afb3d

Please sign in to comment.