Bläddra i källkod

Remove `respondStateCode`, `respondDTO` as http response

ryoii 2 år sedan
förälder
incheckning
0f0a9f43e6

+ 2 - 2
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/GlobalExceptionHandler.kt

@@ -11,11 +11,11 @@ package net.mamoe.mirai.api.http.adapter.http.plugin
 
 import io.ktor.server.application.*
 import io.ktor.server.application.hooks.*
-import net.mamoe.mirai.api.http.adapter.http.router.respondStateCode
+import io.ktor.server.response.*
 import net.mamoe.mirai.api.http.adapter.internal.handler.toStateCode
 
 val GlobalExceptionHandler = createApplicationPlugin("GlobalExceptionHandler") {
     on(CallFailed) { call, cause ->
-        call.respondStateCode(cause.toStateCode())
+        call.respond(cause.toStateCode())
     }
 }

+ 5 - 4
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/about.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2020 Mamoe Technologies and contributors.
+ * Copyright 2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -10,6 +10,7 @@
 package net.mamoe.mirai.api.http.adapter.http.router
 
 import io.ktor.server.application.*
+import io.ktor.server.response.*
 import io.ktor.server.routing.*
 import net.mamoe.mirai.api.http.adapter.internal.action.onAbout
 import net.mamoe.mirai.api.http.adapter.internal.action.onGetBotsList
@@ -31,16 +32,16 @@ internal fun Application.aboutRouter() = routing {
      */
     get(Paths.about) {
         val data = onAbout()
-        call.respondDTO(StringMapRestfulResult(data = data))
+        call.respond(StringMapRestfulResult(data = data))
     }
 
     httpAuthedGet<EmptyAuthedDTO>(Paths.sessionInfo) {
         val data = onGetSessionInfo(it)
-        call.respondDTO(ElementResult(data = data.toJsonElement()))
+        call.respond(ElementResult(data = data.toJsonElement()))
     }
 
     get(Paths.botList) {
         val data = onGetBotsList()
-        call.respondDTO(LongListRestfulResult(data))
+        call.respond(LongListRestfulResult(data))
     }
 }

+ 2 - 25
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/dsl.kt

@@ -27,7 +27,6 @@ import net.mamoe.mirai.api.http.adapter.internal.dto.AuthedDTO
 import net.mamoe.mirai.api.http.adapter.internal.dto.BindDTO
 import net.mamoe.mirai.api.http.adapter.internal.dto.DTO
 import net.mamoe.mirai.api.http.adapter.internal.dto.VerifyDTO
-import net.mamoe.mirai.api.http.adapter.internal.serializer.toJson
 import net.mamoe.mirai.api.http.context.MahContext
 import net.mamoe.mirai.api.http.context.MahContextHolder
 import net.mamoe.mirai.api.http.context.session.Session
@@ -48,13 +47,13 @@ private fun <T> buildStrategy(block: Strategy<T>) = block
  * 处理策略: 返回状态码
  */
 internal inline fun <reified T> respondStateCodeStrategy(crossinline action: suspend (T) -> StateCode) =
-    buildStrategy<T> { call.respondStateCode(action(it)) }
+    buildStrategy<T> { call.respond(action(it)) }
 
 /**
  * 处理策略: 返回 DTO
  */
 internal inline fun <reified T, reified R : DTO> respondDTOStrategy(crossinline action: suspend (T) -> R) =
-    buildStrategy<T> { call.respondDTO(action(it)) }
+    buildStrategy<T> { call.respond(action(it)) }
 
 /***********************
  *    路由 DSL 定义
@@ -134,28 +133,6 @@ private fun PipelineContext<*, ApplicationCall>.getAuthedSession(sessionKey: Str
         ?: throw IllegalSessionException
 }
 
-/**
- * 响应 [StateCode]
- */
-internal suspend inline fun <reified T : StateCode> ApplicationCall.respondStateCode(
-    code: T,
-    status: HttpStatusCode = HttpStatusCode.OK
-) = respondJson(code.toJson(), status)
-
-/**
- * 响应 [DTO]
- */
-internal suspend inline fun <reified T : DTO> ApplicationCall.respondDTO(
-    dto: T,
-    status: HttpStatusCode = HttpStatusCode.OK
-) = respondJson(dto.toJson(), status)
-
-/**
- * 响应 Json 字符串
- */
-internal suspend fun ApplicationCall.respondJson(json: String, status: HttpStatusCode = HttpStatusCode.OK) =
-    respondText(json, defaultTextContentType(ContentType.Application.Json), status)
-
 /**
  * 接收 http multi part 值类型
  */

+ 4 - 3
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/file.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2020 Mamoe Technologies and contributors.
+ * Copyright 2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -11,6 +11,7 @@ package net.mamoe.mirai.api.http.adapter.http.router
 
 import io.ktor.server.application.*
 import io.ktor.http.content.*
+import io.ktor.server.response.*
 import io.ktor.server.routing.*
 import net.mamoe.mirai.api.http.adapter.common.IllegalParamException
 import net.mamoe.mirai.api.http.adapter.common.StateCode
@@ -37,7 +38,7 @@ internal fun Application.fileRouter() = routing {
         val contact = when (type) {
             "group" -> session.bot.getGroup(target)
             else -> {
-                call.respondStateCode(StateCode.NoOperateSupport)
+                call.respond(StateCode.NoOperateSupport)
                 return@httpAuthedMultiPart
             }
         }
@@ -46,7 +47,7 @@ internal fun Application.fileRouter() = routing {
             onUploadFile(streamProvider(), path, originalFileName, contact!!)
         } ?: throw IllegalParamException("缺少参数 file")
 
-        call.respondDTO(ret)
+        call.respond(ret)
     }
 
     httpAuthedPost(Paths.fileDelete, respondDTOStrategy(::onDeleteFile))

+ 10 - 9
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/message.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2020 Mamoe Technologies and contributors.
+ * Copyright 2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -11,6 +11,7 @@ package net.mamoe.mirai.api.http.adapter.http.router
 
 import io.ktor.http.content.*
 import io.ktor.server.application.*
+import io.ktor.server.response.*
 import io.ktor.server.routing.*
 import net.mamoe.mirai.api.http.adapter.common.IllegalParamException
 import net.mamoe.mirai.api.http.adapter.http.dto.CountDTO
@@ -29,7 +30,7 @@ internal fun Application.messageRouter() = routing {
      */
     httpAuthedGet<CountDTO>("/countMessage") {
         val count = it.unreadQueue.size
-        call.respondDTO(IntRestfulResult(data = count))
+        call.respond(IntRestfulResult(data = count))
     }
 
     /**
@@ -37,7 +38,7 @@ internal fun Application.messageRouter() = routing {
      */
     httpAuthedGet<CountDTO>("/fetchMessage") {
         val data = it.unreadQueue.fetch(it.count)
-        call.respondDTO(EventListRestfulResult(data = data))
+        call.respond(EventListRestfulResult(data = data))
     }
 
     /**
@@ -45,7 +46,7 @@ internal fun Application.messageRouter() = routing {
      */
     httpAuthedGet<CountDTO>("/fetchLatestMessage") {
         val data = it.unreadQueue.fetchLatest(it.count)
-        call.respondDTO(EventListRestfulResult(data = data))
+        call.respond(EventListRestfulResult(data = data))
     }
 
     /**
@@ -53,13 +54,13 @@ internal fun Application.messageRouter() = routing {
      */
     httpAuthedGet<CountDTO>("/peekMessage") {
         val data = it.unreadQueue.peek(it.count)
-        call.respondDTO(EventListRestfulResult(data = data))
+        call.respond(EventListRestfulResult(data = data))
     }
 
     /*兼容旧接口*/
     httpAuthedGet<CountDTO>("/peakMessage") {
         val data = it.unreadQueue.peek(it.count)
-        call.respondDTO(EventListRestfulResult(data = data))
+        call.respond(EventListRestfulResult(data = data))
     }
     
     /**
@@ -67,7 +68,7 @@ internal fun Application.messageRouter() = routing {
      */
     httpAuthedGet<CountDTO>("/peekLatestMessage") {
         val data = it.unreadQueue.peekLatest(it.count)
-        call.respondDTO(EventListRestfulResult(data = data))
+        call.respond(EventListRestfulResult(data = data))
     }
 
     /**
@@ -114,7 +115,7 @@ internal fun Application.messageRouter() = routing {
         }
 
         val ret = onUploadImage(session, stream, type)
-        call.respondDTO(ret)
+        call.respond(ret)
     }
 
     /**
@@ -130,7 +131,7 @@ internal fun Application.messageRouter() = routing {
             f.streamProvider()
         }
         val ret = onUploadVoice(session, stream, type)
-        call.respondDTO(ret)
+        call.respond(ret)
     }
 
     /**

+ 8 - 7
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/verify.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2020 Mamoe Technologies and contributors.
+ * Copyright 2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -10,6 +10,7 @@
 package net.mamoe.mirai.api.http.adapter.http.router
 
 import io.ktor.server.application.*
+import io.ktor.server.response.*
 import io.ktor.server.routing.*
 import net.mamoe.mirai.api.http.adapter.common.IllegalSessionException
 import net.mamoe.mirai.api.http.adapter.common.StateCode
@@ -39,11 +40,11 @@ internal fun Application.authRouter(setting: HttpAdapterSetting) = routing {
                 MahContextHolder.sessionManager.createTempSession()
             }
 
-            call.respondDTO(VerifyRetDTO(0, session.key))
+            call.respond(VerifyRetDTO(0, session.key))
             return@httpVerify
         }
 
-        call.respondStateCode(StateCode.AuthKeyFail)
+        call.respond(StateCode.AuthKeyFail)
     }
 
     /**
@@ -51,11 +52,11 @@ internal fun Application.authRouter(setting: HttpAdapterSetting) = routing {
      */
     httpBind("/bind") {
         if (MahContextHolder.singleMode) {
-            call.respondStateCode(StateCode.NoOperateSupport)
+            call.respond(StateCode.NoOperateSupport)
             return@httpBind
         }
         val session = MahContextHolder[it.sessionKey] ?: kotlin.run {
-            call.respondStateCode(StateCode.IllegalSession)
+            call.respond(StateCode.IllegalSession)
             return@httpBind
         }
 
@@ -64,7 +65,7 @@ internal fun Application.authRouter(setting: HttpAdapterSetting) = routing {
             MahContextHolder.sessionManager.authSession(bot, it.sessionKey)
                 .asHttpSession(setting.unreadQueueMaxSize)
         }
-        call.respondStateCode(StateCode.Success)
+        call.respond(StateCode.Success)
     }
 
     /**
@@ -78,7 +79,7 @@ internal fun Application.authRouter(setting: HttpAdapterSetting) = routing {
                 unloadHttpSession()
                 close()
             }
-            call.respondStateCode(StateCode.Success)
+            call.respond(StateCode.Success)
         } else {
             throw NoSuchElementException()
         }