summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMS <ms@taler.net>2023-04-12 11:08:26 +0200
committerMS <ms@taler.net>2023-04-12 11:08:26 +0200
commit3f9689f21443af123fb1ac01fab4fba47d7b2eb5 (patch)
treec3abf5a06e542de00bd7c444e9c1a59b7c0bd55e
parent1b9e30d65782e2543b54599c98b8e55ed5cbeebf (diff)
downloadlibeufin-3f9689f21443af123fb1ac01fab4fba47d7b2eb5.tar.gz
libeufin-3f9689f21443af123fb1ac01fab4fba47d7b2eb5.tar.bz2
libeufin-3f9689f21443af123fb1ac01fab4fba47d7b2eb5.zip
helper
-rw-r--r--util/src/main/kotlin/HTTP.kt11
1 files changed, 11 insertions, 0 deletions
diff --git a/util/src/main/kotlin/HTTP.kt b/util/src/main/kotlin/HTTP.kt
index d9ff4491..30a15bd9 100644
--- 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