Skip to content

Commit

Permalink
Merge pull request #30 from simple-robot/dev/support-jvm-bot-requests
Browse files Browse the repository at this point in the history
`TelegramBot` 中支持 `execute*` API 用于请求API实例
  • Loading branch information
ForteScarlet authored Jul 23, 2024
2 parents cb5b3ec + 4048038 commit b94443e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
requires simbot.common.core;
requires simbot.common.ktor.inputfile;
requires transitive simbot.component.telegram.type;
requires io.ktor.client.core;

exports love.forte.simbot.telegram.api;
exports love.forte.simbot.telegram.api.bot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic

// TODO JVM

/**
* Use [TelegramBot] to request the [TelegramApi] and get [HttpResponse].

Check warning on line 36 in simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/BotRequests.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'HttpResponse'
* @see TelegramApi.requestRawBy
* @see TelegramBot.executeRaw
*/
@JvmSynthetic
public suspend fun TelegramApi<*>.requestRawBy(bot: TelegramBot): HttpResponse =
Expand All @@ -45,6 +44,7 @@ public suspend fun TelegramApi<*>.requestRawBy(bot: TelegramBot): HttpResponse =
/**
* Use [TelegramBot] to request the [TelegramApi] and get [TelegramApiResult] with [R].
* @see TelegramApi.requestResult
* @see TelegramBot.executeResult
*/
@JvmSynthetic
public suspend fun <R : Any> TelegramApi<R>.requestResultBy(bot: TelegramBot): TelegramApiResult<R> =
Expand All @@ -53,6 +53,7 @@ public suspend fun <R : Any> TelegramApi<R>.requestResultBy(bot: TelegramBot): T
/**
* Use [TelegramBot] to request the [TelegramApi] and get [R].
* @see TelegramApi.requestData
* @see TelegramBot.execute
*/
@JvmSynthetic
public suspend fun <R : Any> TelegramApi<R>.requestDataBy(bot: TelegramBot): R =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package love.forte.simbot.component.telegram.core.bot

import io.ktor.client.statement.*
import love.forte.simbot.bot.Bot
import love.forte.simbot.bot.ContactRelation
import love.forte.simbot.bot.GroupRelation
Expand All @@ -27,6 +28,8 @@ import love.forte.simbot.component.telegram.core.bot.command.TelegramBotCommands
import love.forte.simbot.component.telegram.core.bot.command.TelegramBotCommandsUpdater
import love.forte.simbot.component.telegram.core.component.TelegramComponent
import love.forte.simbot.suspendrunner.ST
import love.forte.simbot.telegram.api.TelegramApi
import love.forte.simbot.telegram.api.TelegramApiResult
import love.forte.simbot.telegram.api.update.Update
import love.forte.simbot.telegram.type.BotCommandScope
import love.forte.simbot.telegram.type.User
Expand Down Expand Up @@ -124,6 +127,42 @@ public interface TelegramBot : Bot {
*/
public val commandsUpdater: TelegramBotCommandsUpdater

/**
* Execute [TelegramApi] by this bot.
*
* @throws Exception Any exception that may occur during the API request process

Check warning on line 133 in simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/TelegramBot.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'Exception'
*
* @see requestDataBy
*/
@ST
public suspend fun <R : Any> execute(api: TelegramApi<R>): R =
api.requestDataBy(this)

/**
* Execute [TelegramApi] by this bot.
*
* @throws Exception Any exception that may occur during the API request process

Check warning on line 144 in simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/TelegramBot.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'Exception'
*
* @return The raw [HttpResponse]

Check warning on line 146 in simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/TelegramBot.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'HttpResponse'
* @see requestRawBy
*/
@ST
public suspend fun executeRaw(api: TelegramApi<*>): HttpResponse =
api.requestRawBy(this)

/**
* Execute [TelegramApi] by this bot.
*
* @throws Exception Any exception that may occur during the API request process

Check warning on line 156 in simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/TelegramBot.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'Exception'
*
* @return The [TelegramApiResult]
* @see requestResultBy
*/
@ST
public suspend fun <R : Any> executeResult(api: TelegramApi<R>): TelegramApiResult<R> =
api.requestResultBy(this)


override val groupRelation: GroupRelation?
get() = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

requires static kotlinx.coroutines.reactive;
requires static org.reactivestreams;
requires io.ktor.client.core;

exports love.forte.simbot.telegram.stdlib.bot;
exports love.forte.simbot.telegram.stdlib.event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,65 +23,132 @@ package love.forte.simbot.telegram.stdlib.bot
import io.ktor.client.statement.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.future.future
import love.forte.simbot.annotations.InternalSimbotAPI
import love.forte.simbot.suspendrunner.reserve.SuspendReserve
import love.forte.simbot.suspendrunner.reserve.suspendReserve
import love.forte.simbot.suspendrunner.runInNoScopeBlocking
import love.forte.simbot.telegram.api.*
import java.util.concurrent.CompletableFuture
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

/**
* [requestRawBy] in async.
* @see TelegramApi.requestRaw
*/
@JvmSynthetic
@JvmOverloads
public fun TelegramApi<*>.requestByAsync(bot: Bot, scope: CoroutineScope = bot): CompletableFuture<HttpResponse> =
scope.future { requestRawBy(bot) }
public fun TelegramApi<*>.requestByAsync(
bot: Bot,
scope: CoroutineScope? = null,
context: CoroutineContext? = null,
): CompletableFuture<HttpResponse> =
(scope ?: bot).future(context = context ?: EmptyCoroutineContext) { requestRawBy(bot) }


/**
* [requestResultBy] in async.
* @see TelegramApi.requestResult
*/
@JvmSynthetic
@JvmOverloads
public fun <R : Any> TelegramApi<R>.requestResultByAsync(
bot: Bot,
scope: CoroutineScope = bot
scope: CoroutineScope? = null,
context: CoroutineContext? = null,
): CompletableFuture<TelegramApiResult<R>> =
scope.future { requestResultBy(bot) }
(scope ?: bot).future(context = context ?: EmptyCoroutineContext) { requestResultBy(bot) }


/**
* [requestDataBy] in async.
* @see TelegramApi.requestData
*/
@JvmSynthetic
@JvmOverloads
public fun <R : Any> TelegramApi<R>.requestDataByAsync(bot: Bot, scope: CoroutineScope = bot): CompletableFuture<R> =
scope.future { requestDataBy(bot) }
public fun <R : Any> TelegramApi<R>.requestDataByAsync(
bot: Bot,
scope: CoroutineScope? = null,
context: CoroutineContext? = null,
): CompletableFuture<R> =
(scope ?: bot).future(context = context ?: EmptyCoroutineContext) { requestDataBy(bot) }

/**
* [requestRawBy] in blocking.
* @see requestRawBy
*/
@JvmSynthetic
public fun TelegramApi<*>.requestByBlocking(bot: Bot): HttpResponse =
runInNoScopeBlocking { requestRawBy(bot) }
@JvmOverloads
public fun TelegramApi<*>.requestByBlocking(
bot: Bot,
context: CoroutineContext? = null,
): HttpResponse =
runInNoScopeBlocking(context = context ?: EmptyCoroutineContext) { requestRawBy(bot) }

/**
* [requestResultBy] in blocking.
* @see requestResultBy
*/
@JvmSynthetic
@JvmOverloads
public fun <R : Any> TelegramApi<R>.requestResultByBlocking(
bot: Bot
bot: Bot,
context: CoroutineContext? = null,
): TelegramApiResult<R> =
runInNoScopeBlocking { requestResultBy(bot) }
runInNoScopeBlocking(context = context ?: EmptyCoroutineContext) { requestResultBy(bot) }

/**
* [requestDataBy] in blocking.
* @see requestDataBy
*/
@JvmOverloads
public fun <R : Any> TelegramApi<R>.requestDataByBlocking(
bot: Bot,
context: CoroutineContext? = null,
): R =
runInNoScopeBlocking(context = context ?: EmptyCoroutineContext) { requestDataBy(bot) }


/**
* [requestRawBy] in blocking.
* @see requestRawBy
*/
@OptIn(InternalSimbotAPI::class)
@JvmOverloads
public fun TelegramApi<*>.requestByReserve(
bot: Bot,
scope: CoroutineScope? = null,
context: CoroutineContext? = null,
): SuspendReserve<HttpResponse> =
suspendReserve(
scope = scope ?: bot,
context = context ?: EmptyCoroutineContext,
) { requestRawBy(bot) }

/**
* [requestResultBy] in blocking.
* @see requestResultBy
*/
@OptIn(InternalSimbotAPI::class)
@JvmOverloads
public fun <R : Any> TelegramApi<R>.requestResultByReserve(
bot: Bot,
scope: CoroutineScope? = null,
context: CoroutineContext? = null,
): SuspendReserve<TelegramApiResult<R>> =
suspendReserve(
scope = scope ?: bot,
context = context ?: EmptyCoroutineContext,
) { requestResultBy(bot) }

/**
* [requestDataBy] in blocking.
* @see requestDataBy
*/
@JvmSynthetic
public fun <R : Any> TelegramApi<R>.requestDataByBlocking(bot: Bot): R =
runInNoScopeBlocking { requestDataBy(bot) }
@OptIn(InternalSimbotAPI::class)
@JvmOverloads
public fun <R : Any> TelegramApi<R>.requestDataByReserve(
bot: Bot,
scope: CoroutineScope? = null,
context: CoroutineContext? = null,
): SuspendReserve<R> =
suspendReserve(
scope = scope ?: bot,
context = context ?: EmptyCoroutineContext,
) { requestDataBy(bot) }

0 comments on commit b94443e

Please sign in to comment.