Skip to content

Commit e2ffba8

Browse files
authored
Merge pull request #208 from simple-robot/dev/api-methods
OneBotApi支持指定method与自定义的URL操作; 调整部分依赖配置
2 parents 92752e8 + ef3795e commit e2ffba8

File tree

8 files changed

+101
-28
lines changed

8 files changed

+101
-28
lines changed

simbot-component-onebot-common/build.gradle.kts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ kotlin {
5959
api(kotlin("test"))
6060
}
6161

62-
jvmMain {
63-
dependencies {
64-
compileOnly(libs.simbot.api)
65-
}
66-
}
67-
6862
jvmTest.dependencies {
6963
implementation(libs.log4j.api)
7064
implementation(libs.log4j.core)

simbot-component-onebot-v11/simbot-component-onebot-v11-common/build.gradle.kts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ kotlin {
5151

5252
sourceSets {
5353
commonMain.dependencies {
54-
implementation(project(":simbot-component-onebot-common"))
55-
implementation(libs.simbot.common.annotations)
54+
api(project(":simbot-component-onebot-common"))
55+
api(libs.simbot.common.annotations)
5656
api(libs.kotlinx.serialization.core)
5757
}
5858

@@ -62,10 +62,6 @@ kotlin {
6262
api(libs.kotlinx.coroutines.test)
6363
}
6464

65-
jvmMain.dependencies {
66-
compileOnly(libs.simbot.common.annotations)
67-
}
68-
6965
jvmTest.dependencies {
7066
compileOnly(libs.simbot.common.annotations)
7167
implementation(libs.log4j.api)

simbot-component-onebot-v11/simbot-component-onebot-v11-core/api/simbot-component-onebot-v11-core.api

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,10 @@ public abstract interface class love/forte/simbot/component/onebot/v11/core/api/
11041104
public abstract fun getAction ()Ljava/lang/String;
11051105
public abstract fun getApiResultDeserializer ()Lkotlinx/serialization/DeserializationStrategy;
11061106
public abstract fun getBody ()Ljava/lang/Object;
1107+
public fun getMethod ()Lio/ktor/http/HttpMethod;
11071108
public abstract fun getResultDeserializer ()Lkotlinx/serialization/DeserializationStrategy;
1109+
public fun resolveUrlAction (Lio/ktor/http/URLBuilder;Ljava/util/Collection;)V
1110+
public fun resolveUrlExtensions (Lio/ktor/http/URLBuilder;)V
11081111
}
11091112

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

1167+
public final class love/forte/simbot/component/onebot/v11/core/api/OneBotApiKt {
1168+
public static final fun resolveUrl (Llove/forte/simbot/component/onebot/v11/core/api/OneBotApi;Lio/ktor/http/URLBuilder;Ljava/util/Collection;)V
1169+
}
1170+
11641171
public final class love/forte/simbot/component/onebot/v11/core/api/OneBotApiRequests {
11651172
public static final fun getApiLogger ()Lorg/slf4j/Logger;
11661173
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;

simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/api/OneBotApi.kt

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

20+
import io.ktor.http.*
2021
import kotlinx.serialization.*
2122
import kotlinx.serialization.builtins.serializer
2223
import kotlinx.serialization.descriptors.SerialDescriptor
@@ -37,10 +38,48 @@ import love.forte.simbot.component.onebot.v11.core.api.OneBotApiResult.Companion
3738
*/
3839
public interface OneBotApi<T : Any> {
3940
/**
40-
* API 的 action(要进行的动作)
41+
* 此 API 的请求方式。
42+
* OneBot协议中的标准API通常均为 POST,
43+
* 但是一些额外的扩展或自定义API可能是 GET 或其他方式。
44+
* @since 1.8.0
45+
*/
46+
public val method: HttpMethod
47+
get() = HttpMethod.Post
48+
49+
/**
50+
* API 的 action(要进行的动作),会通过 [resolveUrlAction] 附加在 url 中。
51+
* 可以重写它来改变此逻辑。
4152
*/
4253
public val action: String
4354

55+
/**
56+
* 根据 [action] 和可能额外要求的 [actionSuffixes] 构建一个完整的请求地址。
57+
*
58+
* [urlBuilder] 中已经添加了基础的 `host` 等信息。
59+
*
60+
* @since 1.8.0
61+
*/
62+
public fun resolveUrlAction(urlBuilder: URLBuilder, actionSuffixes: Collection<String>?) {
63+
if (actionSuffixes?.isEmpty() != false) {
64+
urlBuilder.appendPathSegments(action)
65+
} else {
66+
urlBuilder.appendPathSegments(
67+
buildString(action.length) {
68+
append(action)
69+
actionSuffixes.forEach { sf -> append(sf) }
70+
}
71+
)
72+
}
73+
}
74+
75+
/**
76+
* 对 [urlBuilder] 进行一些额外的处理,例如当method为GET时为其添加查询参数。
77+
* 主要面向额外扩展的自定义实现来重写此方法。
78+
* @since 1.8.0
79+
*/
80+
public fun resolveUrlExtensions(urlBuilder: URLBuilder) {
81+
}
82+
4483
/**
4584
* API的参数。
4685
*/
@@ -76,6 +115,15 @@ public interface OneBotApi<T : Any> {
76115
}
77116
}
78117

118+
/**
119+
* 使用 [OneBotApi.resolveUrlAction] 和 [OneBotApi.resolveUrlExtensions] 。
120+
* @since 1.8.0
121+
*/
122+
public fun OneBotApi<*>.resolveUrl(urlBuilder: URLBuilder, actionSuffixes: Collection<String>?) {
123+
resolveUrlAction(urlBuilder, actionSuffixes)
124+
resolveUrlExtensions(urlBuilder)
125+
}
126+
79127
/**
80128
* [响应](https://github.com/botuniverse/onebot-11/blob/master/api/README.md#响应)
81129
*

simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/api/OneBotApiRequests.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,13 @@ public suspend fun OneBotApi<*>.request(
102102
accessToken: String? = null,
103103
actionSuffixes: Collection<String>? = null,
104104
): HttpResponse {
105-
return client.post {
105+
val api = this
106+
107+
return client.request {
108+
this.method = api.method
106109
url {
107110
takeFrom(host)
108-
if (actionSuffixes?.isEmpty() != false) {
109-
appendPathSegments(action)
110-
} else {
111-
appendPathSegments(
112-
buildString(action.length) {
113-
append(action)
114-
actionSuffixes.forEach { sf -> append(sf) }
115-
}
116-
)
117-
}
111+
api.resolveUrl(this, actionSuffixes)
118112
}
119113

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

125119
var jsonStr: String? = null
126120

127-
when (val b = this@request.body) {
121+
when (val b = api.body) {
128122
null -> {
129123
if (GlobalOneBotApiRequestConfiguration.emptyJsonStringIfBodyNull) {
130124
setBody(EMPTY_JSON_STR)
@@ -163,7 +157,7 @@ public suspend fun OneBotApi<*>.request(
163157
"API [{}] REQ ===> {}, body: {}, json: {}",
164158
action,
165159
url,
166-
this@request.body,
160+
api.body,
167161
jsonStr
168162
)
169163
}.also { res ->

simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonTest/kotlin/love/forte/simbot/component/onebot/v11/core/api/ApiRequestTests.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package love.forte.simbot.component.onebot.v11.core.api
22

33
import io.ktor.client.*
44
import io.ktor.client.engine.mock.*
5+
import io.ktor.client.statement.*
6+
import io.ktor.http.*
57
import kotlinx.coroutines.test.runTest
8+
import kotlinx.serialization.DeserializationStrategy
69
import kotlinx.serialization.KSerializer
10+
import kotlinx.serialization.Serializable
711
import love.forte.simbot.common.id.IntID.Companion.ID
812
import love.forte.simbot.common.id.literal
913
import love.forte.simbot.component.onebot.common.annotations.ApiResultConstructor
@@ -70,4 +74,34 @@ class ApiRequestTests {
7074
assertEquals("123", data.messageId.literal)
7175
}
7276

77+
@Test
78+
fun customGetApiTest() = runTest {
79+
@Serializable
80+
data class CustomResult(val name: String)
81+
class MyCustomApi : OneBotApi<CustomResult> {
82+
override val method: HttpMethod = HttpMethod.Get
83+
override val action: String = "custom_action"
84+
override val body: Any? = null
85+
override val resultDeserializer: DeserializationStrategy<CustomResult> = CustomResult.serializer()
86+
override val apiResultDeserializer: DeserializationStrategy<OneBotApiResult<CustomResult>> =
87+
OneBotApiResult.serializer(CustomResult.serializer())
88+
89+
override fun resolveUrlExtensions(urlBuilder: URLBuilder) {
90+
urlBuilder.parameters.append("name", "forte")
91+
}
92+
}
93+
94+
val client = createClient(
95+
"custom_action",
96+
respDataSer = { CustomResult.serializer() },
97+
respData = { CustomResult("forte") }
98+
)
99+
100+
val resp = MyCustomApi().request(client, "http://127.0.0.1:8080/")
101+
assertEquals(HttpMethod.Get, resp.request.method)
102+
assertEquals("forte", resp.request.url.parameters["name"])
103+
104+
val data = MyCustomApi().requestData(client, "http://127.0.0.1:8080/")
105+
assertEquals("forte", data.name)
106+
}
73107
}

simbot-component-onebot-v11/simbot-component-onebot-v11-event/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ kotlin {
5757
implementation(libs.simbot.api)
5858
api(libs.simbot.common.annotations)
5959
api(project(":simbot-component-onebot-common"))
60-
implementation(libs.kotlinx.serialization.json)
60+
api(libs.kotlinx.serialization.json)
6161

6262
api(project(":simbot-component-onebot-v11:simbot-component-onebot-v11-common"))
6363
api(project(":simbot-component-onebot-v11:simbot-component-onebot-v11-message"))

simbot-component-onebot-v11/simbot-component-onebot-v11-message/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ kotlin {
6363
api(libs.simbot.common.annotations)
6464

6565
api(libs.kotlinx.coroutines.core)
66+
api(libs.kotlinx.serialization.json)
6667
implementation(libs.kotlinx.io.core)
67-
implementation(libs.kotlinx.serialization.json)
6868
implementation(libs.jetbrains.annotations)
6969
}
7070

0 commit comments

Comments
 (0)