From aa7cc503f24309cfe325969ddce38de5aeadcdb9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 25 Apr 2023 13:14:12 +0600 Subject: [PATCH] improvements in query parameters getting --- CHANGELOG.md | 4 ++++ .../ktor/server/QueryParameters.kt | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0340b7a3372..e195ed81c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.18.0 +* `Ktor`: + * `Server`: + * Now it is possible to take query parameters as list + ## 0.17.8 * `Versions`: diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/QueryParameters.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/QueryParameters.kt index 053d316c631..241adfb6290 100644 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/QueryParameters.kt +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/QueryParameters.kt @@ -12,10 +12,22 @@ suspend fun ApplicationCall.getParameterOrSendError( } } +suspend fun ApplicationCall.getParametersOrSendError( + field: String +) = parameters.getAll(field).also { + if (it == null) { + respond(HttpStatusCode.BadRequest, "Request must contains $field") + } +} + fun ApplicationCall.getQueryParameter( field: String ) = request.queryParameters[field] +fun ApplicationCall.getQueryParameters( + field: String +) = request.queryParameters.getAll(field) + suspend fun ApplicationCall.getQueryParameterOrSendError( field: String ) = getQueryParameter(field).also { @@ -23,3 +35,11 @@ suspend fun ApplicationCall.getQueryParameterOrSendError( respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field") } } + +suspend fun ApplicationCall.getQueryParametersOrSendError( + field: String +) = getQueryParameters(field).also { + if (it == null) { + respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field") + } +}