libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 3f9689f21443af123fb1ac01fab4fba47d7b2eb5
parent 1b9e30d65782e2543b54599c98b8e55ed5cbeebf
Author: MS <ms@taler.net>
Date:   Wed, 12 Apr 2023 11:08:26 +0200

helper

Diffstat:
Mutil/src/main/kotlin/HTTP.kt | 11+++++++++++
1 file changed, 11 insertions(+), 0 deletions(-)

diff --git a/util/src/main/kotlin/HTTP.kt b/util/src/main/kotlin/HTTP.kt @@ -9,6 +9,7 @@ import io.ktor.server.util.* import io.ktor.util.* import logger import java.net.URLDecoder +import kotlin.reflect.typeOf fun unauthorized(msg: String): UtilError { return UtilError( @@ -208,4 +209,14 @@ fun expectLong(uriParam: String): Long { logger.error(e.message) throw badRequest("'$uriParam' is not Long") } +} + +// Returns null, or tries to convert the parameter to type T. +// Throws Bad Request, if the conversion could not be done. +fun ApplicationCall.maybeLong(uriParamName: String): Long? { + val maybeParam = this.parameters[uriParamName] ?: return null + return try { maybeParam.toLong() } + catch (e: Exception) { + throw badRequest("Could not convert '$uriParamName' to Long") + } } \ No newline at end of file