libeufin

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

commit 4f0135998694fd67f34e587bbdd0ad3411208819
parent 4dc2291775e2eaca3c82ca943338962285051b68
Author: Antoine A <>
Date:   Mon, 23 Oct 2023 10:17:09 +0000

cleanup

Diffstat:
Mbank/src/main/kotlin/tech/libeufin/bank/CoreBankApi.kt | 2+-
Mbank/src/main/kotlin/tech/libeufin/bank/WireGatewayApi.kt | 2+-
Mbank/src/main/kotlin/tech/libeufin/bank/helpers.kt | 56++++++++++++++++++--------------------------------------
Mbank/src/test/kotlin/CoreBankApiTest.kt | 4++--
Mbank/src/test/kotlin/WireGatewayApiTest.kt | 6+++---
5 files changed, 25 insertions(+), 45 deletions(-)

diff --git a/bank/src/main/kotlin/tech/libeufin/bank/CoreBankApi.kt b/bank/src/main/kotlin/tech/libeufin/bank/CoreBankApi.kt @@ -343,7 +343,7 @@ private fun Routing.coreBankAccountsMgmtApi(db: Database, ctx: BankApplicationCo private fun Routing.coreBankTransactionsApi(db: Database, ctx: BankApplicationContext) { get("/accounts/{USERNAME}/transactions") { call.authCheck(db, TokenScope.readonly) - val params = getHistoryParams(call.request.queryParameters) + val params = HistoryParams.extract(call.request.queryParameters) val bankAccount = call.bankAccount(db) val history: List<BankAccountTransactionInfo> = db.bankPoolHistory(params, bankAccount.bankAccountId!!) diff --git a/bank/src/main/kotlin/tech/libeufin/bank/WireGatewayApi.kt b/bank/src/main/kotlin/tech/libeufin/bank/WireGatewayApi.kt @@ -95,7 +95,7 @@ fun Routing.wireGatewayApi(db: Database, ctx: BankApplicationContext) { dbLambda: suspend Database.(HistoryParams, Long) -> List<T> ) { val (login, _) = call.authCheck(db, TokenScope.readonly) - val params = getHistoryParams(call.request.queryParameters) + val params = HistoryParams.extract(call.request.queryParameters) val bankAccount = call.bankAccount(db) if (!bankAccount.isTalerExchange) diff --git a/bank/src/main/kotlin/tech/libeufin/bank/helpers.kt b/bank/src/main/kotlin/tech/libeufin/bank/helpers.kt @@ -197,11 +197,7 @@ data class MonitorParams( companion object { fun extract(params: Parameters): MonitorParams { val timeframe = Timeframe.valueOf(params["timeframe"] ?: "hour") - val which = try { - params["which"]?.toInt() - } catch (e: Exception) { - throw badRequest("Param 'which' not a number") - } + val which = params["which"]?.run { toIntOrNull() ?: throw badRequest("Param 'which' not a number") } if (which != null) { val lastDayOfMonth = OffsetDateTime.now(ZoneOffset.UTC).with(TemporalAdjusters.lastDayOfMonth()).dayOfMonth when { @@ -223,41 +219,25 @@ data class MonitorParams( data class HistoryParams( val delta: Int, val start: Long, val poll_ms: Long -) - -/** - * Extracts the query parameters from "history-like" endpoints, - * providing the defaults according to the API specification. - */ -fun getHistoryParams(params: Parameters): HistoryParams { - val deltaParam: String = - params["delta"] ?: throw MissingRequestParameterException(parameterName = "delta") - val delta: Int = try { - deltaParam.toInt() - } catch (e: Exception) { - logger.error(e.message) - throw badRequest("Param 'delta' not a number") - } - // Note: minimum 'start' is zero, as database IDs start from 1. - val start: Long = when (val param = params["start"]) { - null -> if (delta >= 0) 0L else Long.MAX_VALUE - else -> try { - param.toLong() - } catch (e: Exception) { - logger.error(e.message) - throw badRequest("Param 'start' not a number") - } - } - val poll_ms: Long = when (val param = params["long_poll_ms"]) { - null -> 0 - else -> try { - param.toLong() - } catch (e: Exception) { - logger.error(e.message) - throw badRequest("Param 'long_poll_ms' not a number") +) { + companion object { + fun extract(params: Parameters): HistoryParams { + val deltaParam: String = + params["delta"] ?: throw MissingRequestParameterException(parameterName = "delta") + val delta: Int = deltaParam.toIntOrNull() ?: throw badRequest("Param 'delta' not a number") + // Note: minimum 'start' is zero, as database IDs start from 1. + val start: Long = when (val param = params["start"]) { + null -> if (delta >= 0) 0L else Long.MAX_VALUE + else -> param.toLongOrNull() ?: throw badRequest("Param 'start' not a number") + } + val poll_ms: Long = when (val param = params["long_poll_ms"]) { + null -> 0 + else -> param.toLongOrNull() ?: throw badRequest("Param 'long_poll_ms' not a number") + } + // TODO check params range + return HistoryParams(delta = delta, start = start, poll_ms = poll_ms) } } - return HistoryParams(delta = delta, start = start, poll_ms = poll_ms) } /** diff --git a/bank/src/test/kotlin/CoreBankApiTest.kt b/bank/src/test/kotlin/CoreBankApiTest.kt @@ -456,9 +456,9 @@ class CoreBankTransactionsApiTest { fun testHistory() = bankSetup { _ -> suspend fun HttpResponse.assertHistory(size: Int) { assertOk() - val txt = this.bodyAsText() + val txt = bodyAsText() val history = Json.decodeFromString<BankAccountTransactionsResponse>(txt) - val params = getHistoryParams(this.call.request.url.parameters) + val params = HistoryParams.extract(call.request.url.parameters) // testing the size is like expected. assert(history.transactions.size == size) { diff --git a/bank/src/test/kotlin/WireGatewayApiTest.kt b/bank/src/test/kotlin/WireGatewayApiTest.kt @@ -213,9 +213,9 @@ class WireGatewayApiTest { suspend fun HttpResponse.assertHistory(size: Int) { assertOk() - val txt = this.bodyAsText() + val txt = bodyAsText() val history = Json.decodeFromString<IncomingHistory>(txt) - val params = getHistoryParams(this.call.request.url.parameters) + val params = HistoryParams.extract(call.request.url.parameters) // testing the size is like expected. assert(history.incoming_transactions.size == size) { @@ -394,7 +394,7 @@ class WireGatewayApiTest { assertOk() val txt = this.bodyAsText() val history = Json.decodeFromString<OutgoingHistory>(txt) - val params = getHistoryParams(this.call.request.url.parameters) + val params = HistoryParams.extract(this.call.request.url.parameters) // testing the size is like expected. assert(history.outgoing_transactions.size == size) {