Skip to content

OneBotApi支持指定method与自定义的URL操作; 调整部分依赖配置 #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions simbot-component-onebot-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ kotlin {
api(kotlin("test"))
}

jvmMain {
dependencies {
compileOnly(libs.simbot.api)
}
}

jvmTest.dependencies {
implementation(libs.log4j.api)
implementation(libs.log4j.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ kotlin {

sourceSets {
commonMain.dependencies {
implementation(project(":simbot-component-onebot-common"))
implementation(libs.simbot.common.annotations)
api(project(":simbot-component-onebot-common"))
api(libs.simbot.common.annotations)
api(libs.kotlinx.serialization.core)
}

Expand All @@ -62,10 +62,6 @@ kotlin {
api(libs.kotlinx.coroutines.test)
}

jvmMain.dependencies {
compileOnly(libs.simbot.common.annotations)
}

jvmTest.dependencies {
compileOnly(libs.simbot.common.annotations)
implementation(libs.log4j.api)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,10 @@ public abstract interface class love/forte/simbot/component/onebot/v11/core/api/
public abstract fun getAction ()Ljava/lang/String;
public abstract fun getApiResultDeserializer ()Lkotlinx/serialization/DeserializationStrategy;
public abstract fun getBody ()Ljava/lang/Object;
public fun getMethod ()Lio/ktor/http/HttpMethod;
public abstract fun getResultDeserializer ()Lkotlinx/serialization/DeserializationStrategy;
public fun resolveUrlAction (Lio/ktor/http/URLBuilder;Ljava/util/Collection;)V
public fun resolveUrlExtensions (Lio/ktor/http/URLBuilder;)V
}

public final class love/forte/simbot/component/onebot/v11/core/api/OneBotApi$Actions {
Expand Down Expand Up @@ -1161,6 +1164,10 @@ public final class love/forte/simbot/component/onebot/v11/core/api/OneBotApiExec
public final synthetic fun unbox-impl ()Llove/forte/simbot/component/onebot/v11/core/api/OneBotApiExecutable;
}

public final class love/forte/simbot/component/onebot/v11/core/api/OneBotApiKt {
public static final fun resolveUrl (Llove/forte/simbot/component/onebot/v11/core/api/OneBotApi;Lio/ktor/http/URLBuilder;Ljava/util/Collection;)V
}

public final class love/forte/simbot/component/onebot/v11/core/api/OneBotApiRequests {
public static final fun getApiLogger ()Lorg/slf4j/Logger;
public static final synthetic fun request (Llove/forte/simbot/component/onebot/v11/core/api/OneBotApi;Lio/ktor/client/HttpClient;Lio/ktor/http/Url;Ljava/lang/String;Ljava/util/Collection;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package love.forte.simbot.component.onebot.v11.core.api

import io.ktor.http.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
Expand All @@ -37,10 +38,48 @@ import love.forte.simbot.component.onebot.v11.core.api.OneBotApiResult.Companion
*/
public interface OneBotApi<T : Any> {
/**
* API 的 action(要进行的动作)
* 此 API 的请求方式。
* OneBot协议中的标准API通常均为 POST,
* 但是一些额外的扩展或自定义API可能是 GET 或其他方式。
* @since 1.8.0
*/
public val method: HttpMethod
get() = HttpMethod.Post

/**
* API 的 action(要进行的动作),会通过 [resolveUrlAction] 附加在 url 中。
* 可以重写它来改变此逻辑。
*/
public val action: String

/**
* 根据 [action] 和可能额外要求的 [actionSuffixes] 构建一个完整的请求地址。
*
* [urlBuilder] 中已经添加了基础的 `host` 等信息。
*
* @since 1.8.0
*/
public fun resolveUrlAction(urlBuilder: URLBuilder, actionSuffixes: Collection<String>?) {
if (actionSuffixes?.isEmpty() != false) {
urlBuilder.appendPathSegments(action)
} else {
urlBuilder.appendPathSegments(
buildString(action.length) {
append(action)
actionSuffixes.forEach { sf -> append(sf) }
}
)
}
}

/**
* 对 [urlBuilder] 进行一些额外的处理,例如当method为GET时为其添加查询参数。
* 主要面向额外扩展的自定义实现来重写此方法。
* @since 1.8.0
*/
public fun resolveUrlExtensions(urlBuilder: URLBuilder) {
}

/**
* API的参数。
*/
Expand Down Expand Up @@ -76,6 +115,15 @@ public interface OneBotApi<T : Any> {
}
}

/**
* 使用 [OneBotApi.resolveUrlAction] 和 [OneBotApi.resolveUrlExtensions] 。
* @since 1.8.0
*/
public fun OneBotApi<*>.resolveUrl(urlBuilder: URLBuilder, actionSuffixes: Collection<String>?) {
resolveUrlAction(urlBuilder, actionSuffixes)
resolveUrlExtensions(urlBuilder)
}

/**
* [响应](https://github.com/botuniverse/onebot-11/blob/master/api/README.md#响应)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,13 @@ public suspend fun OneBotApi<*>.request(
accessToken: String? = null,
actionSuffixes: Collection<String>? = null,
): HttpResponse {
return client.post {
val api = this

return client.request {
this.method = api.method
url {
takeFrom(host)
if (actionSuffixes?.isEmpty() != false) {
appendPathSegments(action)
} else {
appendPathSegments(
buildString(action.length) {
append(action)
actionSuffixes.forEach { sf -> append(sf) }
}
)
}
api.resolveUrl(this, actionSuffixes)
}

headers {
Expand All @@ -124,7 +118,7 @@ public suspend fun OneBotApi<*>.request(

var jsonStr: String? = null

when (val b = this@request.body) {
when (val b = api.body) {
null -> {
if (GlobalOneBotApiRequestConfiguration.emptyJsonStringIfBodyNull) {
setBody(EMPTY_JSON_STR)
Expand Down Expand Up @@ -163,7 +157,7 @@ public suspend fun OneBotApi<*>.request(
"API [{}] REQ ===> {}, body: {}, json: {}",
action,
url,
this@request.body,
api.body,
jsonStr
)
}.also { res ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package love.forte.simbot.component.onebot.v11.core.api

import io.ktor.client.*
import io.ktor.client.engine.mock.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import love.forte.simbot.common.id.IntID.Companion.ID
import love.forte.simbot.common.id.literal
import love.forte.simbot.component.onebot.common.annotations.ApiResultConstructor
Expand Down Expand Up @@ -70,4 +74,34 @@ class ApiRequestTests {
assertEquals("123", data.messageId.literal)
}

@Test
fun customGetApiTest() = runTest {
@Serializable
data class CustomResult(val name: String)
class MyCustomApi : OneBotApi<CustomResult> {
override val method: HttpMethod = HttpMethod.Get
override val action: String = "custom_action"
override val body: Any? = null
override val resultDeserializer: DeserializationStrategy<CustomResult> = CustomResult.serializer()
override val apiResultDeserializer: DeserializationStrategy<OneBotApiResult<CustomResult>> =
OneBotApiResult.serializer(CustomResult.serializer())

override fun resolveUrlExtensions(urlBuilder: URLBuilder) {
urlBuilder.parameters.append("name", "forte")
}
}

val client = createClient(
"custom_action",
respDataSer = { CustomResult.serializer() },
respData = { CustomResult("forte") }
)

val resp = MyCustomApi().request(client, "http://127.0.0.1:8080/")
assertEquals(HttpMethod.Get, resp.request.method)
assertEquals("forte", resp.request.url.parameters["name"])

val data = MyCustomApi().requestData(client, "http://127.0.0.1:8080/")
assertEquals("forte", data.name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ kotlin {
implementation(libs.simbot.api)
api(libs.simbot.common.annotations)
api(project(":simbot-component-onebot-common"))
implementation(libs.kotlinx.serialization.json)
api(libs.kotlinx.serialization.json)

api(project(":simbot-component-onebot-v11:simbot-component-onebot-v11-common"))
api(project(":simbot-component-onebot-v11:simbot-component-onebot-v11-message"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ kotlin {
api(libs.simbot.common.annotations)

api(libs.kotlinx.coroutines.core)
api(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.io.core)
implementation(libs.kotlinx.serialization.json)
implementation(libs.jetbrains.annotations)
}

Expand Down
Loading