Skip to content

Commit

Permalink
Merge pull request #60 from simple-robot/dev/api-empty-body
Browse files Browse the repository at this point in the history
API请求时,如果 body 为 `null` 则默认填充空JSON字符串
  • Loading branch information
ForteScarlet authored Jun 21, 2024
2 parents e8603f6 + bd983c2 commit 88ebab5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ import io.ktor.http.content.*
import io.ktor.utils.io.charsets.*
import kotlinx.serialization.json.Json
import love.forte.simbot.common.serialization.guessSerializer
import love.forte.simbot.component.onebot.common.annotations.ExperimentalOneBotAPI
import love.forte.simbot.component.onebot.common.annotations.InternalOneBotAPI
import love.forte.simbot.component.onebot.v11.core.OneBot11
import love.forte.simbot.logger.Logger
import love.forte.simbot.logger.LoggerFactory
import kotlin.concurrent.Volatile
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic
Expand All @@ -44,6 +46,36 @@ import kotlin.jvm.JvmSynthetic
@InternalOneBotAPI
public val ApiLogger: Logger = LoggerFactory.getLogger("love.forte.simbot.component.onebot.v11.core.api.API")

private const val EMPTY_JSON_STR: String = "{}"

/**
* 全局性的配置属性。
*/
@ExperimentalOneBotAPI
public object GlobalOneBotApiRequestConfiguration {
private const val EMPTY_JSON_STRING_IF_BODY_NULL_KEY: String =
"simbot.onebot11.api.request.emptyJsonStringIfBodyNull"

/**
* 如果 API 的 body 为 null, 则使用一个空的JSON字符串作为 Body。
*
* 在 JVM 平台中,可以通JVM属性
* `simbot.onebot11.api.request.emptyJsonStringIfBodyNull`
* 来设置初始值:
* ```
* -Dsimbot.onebot11.api.request.emptyJsonStringIfBodyNull=false
* ```
*
* 默认为 `true`。
*/
@Volatile
public var emptyJsonStringIfBodyNull: Boolean =
initConfig(EMPTY_JSON_STRING_IF_BODY_NULL_KEY, "true")
.toBoolean()
}

internal expect fun initConfig(key: String, default: String?): String?

/**
* 对 [this] 发起一次请求,并得到相应的 [HttpResponse] 响应。

Check warning on line 80 in simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/api/OneBotApiRequests.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'HttpResponse'
*
Expand Down Expand Up @@ -91,7 +123,12 @@ public suspend fun OneBotApi<*>.request(
}

when (val b = this@request.body) {
null -> {}
null -> {
if (GlobalOneBotApiRequestConfiguration.emptyJsonStringIfBodyNull) {
setBody(EMPTY_JSON_STR)
}
}

is OutgoingContent, is String -> {
setBody(b)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ package love.forte.simbot.component.onebot.v11.core.api
*/
@Suppress("TopLevelPropertyNaming")
internal actual const val isContentNegotiationRuntimeAvailable: Boolean = true
internal actual fun initConfig(key: String, default: String?): String? = default
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

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

import io.ktor.client.HttpClient
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.request
import io.ktor.http.Url
import io.ktor.client.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.utils.io.charsets.Charset
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.json.Json
Expand All @@ -34,6 +33,7 @@ import love.forte.simbot.suspendrunner.runInAsync
import love.forte.simbot.suspendrunner.runInNoScopeBlocking
import java.util.concurrent.CompletableFuture
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.text.Charsets

/**
* 校验当前运行时是否存在
Expand Down Expand Up @@ -554,3 +554,7 @@ public fun <T : Any> OneBotApi<T>.requestDataReserve(
)
}
//endregion


internal actual fun initConfig(key: String, default: String?): String? =
System.getProperty(key)
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ package love.forte.simbot.component.onebot.v11.core.api
*/
@Suppress("TopLevelPropertyNaming")
internal actual const val isContentNegotiationRuntimeAvailable: Boolean = true
internal actual fun initConfig(key: String, default: String?): String? = default

0 comments on commit 88ebab5

Please sign in to comment.