Skip to content

Commit 88ebab5

Browse files
authored
Merge pull request #60 from simple-robot/dev/api-empty-body
API请求时,如果 body 为 `null` 则默认填充空JSON字符串
2 parents e8603f6 + bd983c2 commit 88ebab5

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ import io.ktor.http.content.*
3030
import io.ktor.utils.io.charsets.*
3131
import kotlinx.serialization.json.Json
3232
import love.forte.simbot.common.serialization.guessSerializer
33+
import love.forte.simbot.component.onebot.common.annotations.ExperimentalOneBotAPI
3334
import love.forte.simbot.component.onebot.common.annotations.InternalOneBotAPI
3435
import love.forte.simbot.component.onebot.v11.core.OneBot11
3536
import love.forte.simbot.logger.Logger
3637
import love.forte.simbot.logger.LoggerFactory
38+
import kotlin.concurrent.Volatile
3739
import kotlin.jvm.JvmMultifileClass
3840
import kotlin.jvm.JvmName
3941
import kotlin.jvm.JvmSynthetic
@@ -44,6 +46,36 @@ import kotlin.jvm.JvmSynthetic
4446
@InternalOneBotAPI
4547
public val ApiLogger: Logger = LoggerFactory.getLogger("love.forte.simbot.component.onebot.v11.core.api.API")
4648

49+
private const val EMPTY_JSON_STR: String = "{}"
50+
51+
/**
52+
* 全局性的配置属性。
53+
*/
54+
@ExperimentalOneBotAPI
55+
public object GlobalOneBotApiRequestConfiguration {
56+
private const val EMPTY_JSON_STRING_IF_BODY_NULL_KEY: String =
57+
"simbot.onebot11.api.request.emptyJsonStringIfBodyNull"
58+
59+
/**
60+
* 如果 API 的 body 为 null, 则使用一个空的JSON字符串作为 Body。
61+
*
62+
* 在 JVM 平台中,可以通JVM属性
63+
* `simbot.onebot11.api.request.emptyJsonStringIfBodyNull`
64+
* 来设置初始值:
65+
* ```
66+
* -Dsimbot.onebot11.api.request.emptyJsonStringIfBodyNull=false
67+
* ```
68+
*
69+
* 默认为 `true`。
70+
*/
71+
@Volatile
72+
public var emptyJsonStringIfBodyNull: Boolean =
73+
initConfig(EMPTY_JSON_STRING_IF_BODY_NULL_KEY, "true")
74+
.toBoolean()
75+
}
76+
77+
internal expect fun initConfig(key: String, default: String?): String?
78+
4779
/**
4880
* 对 [this] 发起一次请求,并得到相应的 [HttpResponse] 响应。
4981
*
@@ -91,7 +123,12 @@ public suspend fun OneBotApi<*>.request(
91123
}
92124

93125
when (val b = this@request.body) {
94-
null -> {}
126+
null -> {
127+
if (GlobalOneBotApiRequestConfiguration.emptyJsonStringIfBodyNull) {
128+
setBody(EMPTY_JSON_STR)
129+
}
130+
}
131+
95132
is OutgoingContent, is String -> {
96133
setBody(b)
97134
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ package love.forte.simbot.component.onebot.v11.core.api
2222
*/
2323
@Suppress("TopLevelPropertyNaming")
2424
internal actual const val isContentNegotiationRuntimeAvailable: Boolean = true
25+
internal actual fun initConfig(key: String, default: String?): String? = default

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919

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

22-
import io.ktor.client.HttpClient
23-
import io.ktor.client.statement.HttpResponse
24-
import io.ktor.client.statement.request
25-
import io.ktor.http.Url
22+
import io.ktor.client.*
23+
import io.ktor.client.statement.*
24+
import io.ktor.http.*
2625
import io.ktor.utils.io.charsets.Charset
2726
import kotlinx.coroutines.CoroutineScope
2827
import kotlinx.serialization.json.Json
@@ -34,6 +33,7 @@ import love.forte.simbot.suspendrunner.runInAsync
3433
import love.forte.simbot.suspendrunner.runInNoScopeBlocking
3534
import java.util.concurrent.CompletableFuture
3635
import kotlin.coroutines.EmptyCoroutineContext
36+
import kotlin.text.Charsets
3737

3838
/**
3939
* 校验当前运行时是否存在
@@ -554,3 +554,7 @@ public fun <T : Any> OneBotApi<T>.requestDataReserve(
554554
)
555555
}
556556
//endregion
557+
558+
559+
internal actual fun initConfig(key: String, default: String?): String? =
560+
System.getProperty(key)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ package love.forte.simbot.component.onebot.v11.core.api
2222
*/
2323
@Suppress("TopLevelPropertyNaming")
2424
internal actual const val isContentNegotiationRuntimeAvailable: Boolean = true
25+
internal actual fun initConfig(key: String, default: String?): String? = default

0 commit comments

Comments
 (0)