|
| 1 | +/* |
| 2 | + * Copyright (c) 2025. ForteScarlet. |
| 3 | + * |
| 4 | + * This file is part of simbot-component-kook. |
| 5 | + * |
| 6 | + * simbot-component-kook is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * simbot-component-kook is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with simbot-component-kook, |
| 18 | + * If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +@file:JvmName("KookBotRequests") |
| 22 | +@file:JvmMultifileClass |
| 23 | + |
| 24 | +package love.forte.simbot.component.kook.util |
| 25 | + |
| 26 | +import io.ktor.client.statement.* |
| 27 | +import love.forte.simbot.annotations.Api4J |
| 28 | +import love.forte.simbot.annotations.InternalSimbotAPI |
| 29 | +import love.forte.simbot.component.kook.bot.KookBot |
| 30 | +import love.forte.simbot.kook.api.ApiResult |
| 31 | +import love.forte.simbot.kook.api.KookApi |
| 32 | +import love.forte.simbot.suspendrunner.asReserve |
| 33 | +import love.forte.simbot.suspendrunner.reserve.SuspendReserve |
| 34 | +import love.forte.simbot.suspendrunner.runInAsync |
| 35 | +import love.forte.simbot.suspendrunner.runInNoScopeBlocking |
| 36 | +import java.util.concurrent.CompletableFuture |
| 37 | + |
| 38 | +//region request |
| 39 | +/** |
| 40 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 41 | + * @see KookBot.request |
| 42 | + * @since 4.1.1 |
| 43 | + */ |
| 44 | +@Api4J |
| 45 | +public fun KookBot.requestBlocking(api: KookApi<*>): HttpResponse = |
| 46 | + runInNoScopeBlocking { request(api) } |
| 47 | + |
| 48 | +/** |
| 49 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 50 | + * @see KookBot.request |
| 51 | + * @since 4.1.1 |
| 52 | + */ |
| 53 | +@Api4J |
| 54 | +@OptIn(InternalSimbotAPI::class) |
| 55 | +public fun KookBot.requestAsync(api: KookApi<*>): CompletableFuture<out HttpResponse> = |
| 56 | + runInAsync(scope = this) { request(api) } |
| 57 | + |
| 58 | +/** |
| 59 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 60 | + * @see KookBot.request |
| 61 | + * @since 4.1.1 |
| 62 | + */ |
| 63 | +@Api4J |
| 64 | +@OptIn(InternalSimbotAPI::class) |
| 65 | +public fun KookBot.requestReserve(api: KookApi<*>): SuspendReserve<HttpResponse> = |
| 66 | + asReserve(scope = this) { request(api) } |
| 67 | + |
| 68 | +/** |
| 69 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 70 | + * @see KookBot.requestData |
| 71 | + * @since 4.1.1 |
| 72 | + */ |
| 73 | +@Api4J |
| 74 | +public fun <T : Any> KookBot.requestDataBlocking(api: KookApi<T>): T = |
| 75 | + runInNoScopeBlocking { requestData(api) } |
| 76 | + |
| 77 | +/** |
| 78 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 79 | + * @see KookBot.requestData |
| 80 | + * @since 4.1.1 |
| 81 | + */ |
| 82 | +@Api4J |
| 83 | +@OptIn(InternalSimbotAPI::class) |
| 84 | +public fun <T : Any> KookBot.requestDataAsync(api: KookApi<T>): CompletableFuture<out T> = |
| 85 | + runInAsync(scope = this) { requestData(api) } |
| 86 | + |
| 87 | +/** |
| 88 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 89 | + * @see KookBot.requestData |
| 90 | + * @since 4.1.1 |
| 91 | + */ |
| 92 | +@Api4J |
| 93 | +@OptIn(InternalSimbotAPI::class) |
| 94 | +public fun <T : Any> KookBot.requestDataReserve(api: KookApi<T>): SuspendReserve<T> = |
| 95 | + asReserve(scope = this) { requestData(api) } |
| 96 | + |
| 97 | +/** |
| 98 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 99 | + * @see KookBot.requestText |
| 100 | + * @since 4.1.1 |
| 101 | + */ |
| 102 | +@Api4J |
| 103 | +public fun KookBot.requestTextBlocking(api: KookApi<*>): String = |
| 104 | + runInNoScopeBlocking { requestText(api) } |
| 105 | + |
| 106 | +/** |
| 107 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 108 | + * @see KookBot.requestText |
| 109 | + * @since 4.1.1 |
| 110 | + */ |
| 111 | +@Api4J |
| 112 | +@OptIn(InternalSimbotAPI::class) |
| 113 | +public fun KookBot.requestTextAsync(api: KookApi<*>): CompletableFuture<out String> = |
| 114 | + runInAsync(scope = this) { requestText(api) } |
| 115 | + |
| 116 | +/** |
| 117 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 118 | + * @see KookBot.requestText |
| 119 | + * @since 4.1.1 |
| 120 | + */ |
| 121 | +@Api4J |
| 122 | +@OptIn(InternalSimbotAPI::class) |
| 123 | +public fun KookBot.requestTextReserve(api: KookApi<*>): SuspendReserve<String> = |
| 124 | + asReserve(scope = this) { requestText(api) } |
| 125 | + |
| 126 | +/** |
| 127 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 128 | + * @see KookBot.requestResult |
| 129 | + * @since 4.1.1 |
| 130 | + */ |
| 131 | +@Api4J |
| 132 | +public fun KookBot.requestResultBlocking(api: KookApi<*>): ApiResult = |
| 133 | + runInNoScopeBlocking { requestResult(api) } |
| 134 | + |
| 135 | +/** |
| 136 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 137 | + * @see KookBot.requestResult |
| 138 | + * @since 4.1.1 |
| 139 | + */ |
| 140 | +@Api4J |
| 141 | +@OptIn(InternalSimbotAPI::class) |
| 142 | +public fun KookBot.requestResultAsync(api: KookApi<*>): CompletableFuture<out ApiResult> = |
| 143 | + runInAsync(scope = this) { requestResult(api) } |
| 144 | + |
| 145 | +/** |
| 146 | + * 使用 [KookBot] 对 [api] 发起请求。 |
| 147 | + * @see KookBot.requestResult |
| 148 | + * @since 4.1.1 |
| 149 | + */ |
| 150 | +@Api4J |
| 151 | +@OptIn(InternalSimbotAPI::class) |
| 152 | +public fun KookBot.requestResultReserve(api: KookApi<*>): SuspendReserve<ApiResult> = |
| 153 | + asReserve(scope = this) { requestResult(api) } |
| 154 | +//endregion |
| 155 | + |
| 156 | +//region requestBy |
| 157 | +/** |
| 158 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 159 | + * @see KookApi.requestBy |
| 160 | + * @since 4.1.1 |
| 161 | + */ |
| 162 | +@Api4J |
| 163 | +public fun KookApi<*>.requestByBlocking(bot: KookBot): HttpResponse = |
| 164 | + runInNoScopeBlocking { requestBy(bot) } |
| 165 | + |
| 166 | +/** |
| 167 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 168 | + * @see KookApi.requestBy |
| 169 | + * @since 4.1.1 |
| 170 | + */ |
| 171 | +@Api4J |
| 172 | +@OptIn(InternalSimbotAPI::class) |
| 173 | +public fun KookApi<*>.requestByAsync(bot: KookBot): CompletableFuture<out HttpResponse> = |
| 174 | + runInAsync(scope = bot) { requestBy(bot) } |
| 175 | + |
| 176 | +/** |
| 177 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 178 | + * @see KookApi.requestBy |
| 179 | + * @since 4.1.1 |
| 180 | + */ |
| 181 | +@Api4J |
| 182 | +@OptIn(InternalSimbotAPI::class) |
| 183 | +public fun KookApi<*>.requestByReserve(bot: KookBot): SuspendReserve<HttpResponse> = |
| 184 | + asReserve(scope = bot) { requestBy(bot) } |
| 185 | + |
| 186 | +/** |
| 187 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 188 | + * @see KookApi.requestDataBy |
| 189 | + * @since 4.1.1 |
| 190 | + */ |
| 191 | +@Api4J |
| 192 | +public fun <T : Any> KookApi<T>.requestDataByBlocking(bot: KookBot): T = |
| 193 | + runInNoScopeBlocking { requestDataBy(bot) } |
| 194 | + |
| 195 | +/** |
| 196 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 197 | + * @see KookApi.requestDataBy |
| 198 | + * @since 4.1.1 |
| 199 | + */ |
| 200 | +@Api4J |
| 201 | +@OptIn(InternalSimbotAPI::class) |
| 202 | +public fun <T : Any> KookApi<T>.requestDataByAsync(bot: KookBot): CompletableFuture<out T> = |
| 203 | + runInAsync(scope = bot) { requestDataBy(bot) } |
| 204 | + |
| 205 | +/** |
| 206 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 207 | + * @see KookApi.requestDataBy |
| 208 | + * @since 4.1.1 |
| 209 | + */ |
| 210 | +@Api4J |
| 211 | +@OptIn(InternalSimbotAPI::class) |
| 212 | +public fun <T : Any> KookApi<T>.requestDataByReserve(bot: KookBot): SuspendReserve<T> = |
| 213 | + asReserve(scope = bot) { requestDataBy(bot) } |
| 214 | + |
| 215 | +/** |
| 216 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 217 | + * @see KookApi.requestTextBy |
| 218 | + * @since 4.1.1 |
| 219 | + */ |
| 220 | +@Api4J |
| 221 | +public fun KookApi<*>.requestTextByBlocking(bot: KookBot): String = |
| 222 | + runInNoScopeBlocking { requestTextBy(bot) } |
| 223 | + |
| 224 | +/** |
| 225 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 226 | + * @see KookApi.requestTextBy |
| 227 | + * @since 4.1.1 |
| 228 | + */ |
| 229 | +@Api4J |
| 230 | +@OptIn(InternalSimbotAPI::class) |
| 231 | +public fun KookApi<*>.requestTextByAsync(bot: KookBot): CompletableFuture<out String> = |
| 232 | + runInAsync(scope = bot) { requestTextBy(bot) } |
| 233 | + |
| 234 | +/** |
| 235 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 236 | + * @see KookApi.requestTextBy |
| 237 | + * @since 4.1.1 |
| 238 | + */ |
| 239 | +@Api4J |
| 240 | +@OptIn(InternalSimbotAPI::class) |
| 241 | +public fun KookApi<*>.requestTextByReserve(bot: KookBot): SuspendReserve<String> = |
| 242 | + asReserve(scope = bot) { requestTextBy(bot) } |
| 243 | + |
| 244 | +/** |
| 245 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 246 | + * @see KookApi.requestResultBy |
| 247 | + * @since 4.1.1 |
| 248 | + */ |
| 249 | +@Api4J |
| 250 | +public fun KookApi<*>.requestResultByBlocking(bot: KookBot): ApiResult = |
| 251 | + runInNoScopeBlocking { requestResultBy(bot) } |
| 252 | + |
| 253 | +/** |
| 254 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 255 | + * @see KookApi.requestResultBy |
| 256 | + * @since 4.1.1 |
| 257 | + */ |
| 258 | +@Api4J |
| 259 | +@OptIn(InternalSimbotAPI::class) |
| 260 | +public fun KookApi<*>.requestResultByAsync(bot: KookBot): CompletableFuture<out ApiResult> = |
| 261 | + runInAsync(scope = bot) { requestResultBy(bot) } |
| 262 | + |
| 263 | +/** |
| 264 | + * 使用 [KookApi] 通过 [bot] 发起请求。 |
| 265 | + * @see KookApi.requestResultBy |
| 266 | + * @since 4.1.1 |
| 267 | + */ |
| 268 | +@Api4J |
| 269 | +@OptIn(InternalSimbotAPI::class) |
| 270 | +public fun KookApi<*>.requestResultByReserve(bot: KookBot): SuspendReserve<ApiResult> = |
| 271 | + asReserve(scope = bot) { requestResultBy(bot) } |
| 272 | +//endregion |
0 commit comments