All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.kr.jadekim.jext.ktor.extension.parameter.kt Maven / Gradle / Ivy

There is a newer version: 2.1.4
Show newest version
package kr.jadekim.jext.ktor.extension

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.util.*
import io.ktor.util.pipeline.*
import kr.jadekim.server.http.exception.MissingParameterException

inline val PipelineContext<*, ApplicationCall>.pathParam: Parameters get() = context.parameters

inline val PipelineContext<*, ApplicationCall>.queryParam: Parameters get() = context.request.queryParameters

suspend fun PipelineContext<*, ApplicationCall>.bodyParam(): Parameters? = context.receiveOrNull()

fun PipelineContext<*, ApplicationCall>.pathParamSafe(key: String, default: String? = null): String? {
    return pathParam[key] ?: default
}

fun PipelineContext<*, ApplicationCall>.pathParam(key: String, default: String? = null): String {
    return pathParamSafe(key, default) ?: throw MissingParameterException("required $key")
}

fun PipelineContext<*, ApplicationCall>.queryParamSafe(key: String, default: String? = null): String? {
    return queryParam[key] ?: default
}

fun PipelineContext<*, ApplicationCall>.queryParam(key: String, default: String? = null): String {
    return queryParamSafe(key, default) ?: throw MissingParameterException("required $key")
}

suspend fun PipelineContext<*, ApplicationCall>.bodyParamListSafe(key: String): List {
    return bodyParam()?.getAll(key) ?: emptyList()
}

suspend fun PipelineContext<*, ApplicationCall>.bodyParamList(key: String): List {
    val result = bodyParamListSafe(key)

    if (result.isEmpty()) {
        throw MissingParameterException("required $key")
    }

    return result
}

suspend fun PipelineContext<*, ApplicationCall>.bodyParamSafe(key: String, default: String? = null): String? {
    return bodyParam()?.get(key) ?: default
}

suspend fun PipelineContext<*, ApplicationCall>.bodyParam(key: String, default: String? = null): String {
    return bodyParamSafe(key, default) ?: throw MissingParameterException("required $key")
}

suspend inline fun  PipelineContext.body(): T = context.receive()

fun Parameters?.toSingleValueMap(): Map {
    return this?.toMap()
        ?.mapValues { it.value.firstOrNull() }
        ?.filterValues { !it.isNullOrBlank() }
        ?.mapValues { it.value!! }
        ?: emptyMap()
}

val HttpMethod.canReadBody
    get() = when (this) {
        HttpMethod.Post, HttpMethod.Put, HttpMethod.Patch -> true
        else -> false
    }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy