From 8eb241ccce345a35b05a6335d11306465220f66d Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 23 Jul 2020 15:41:50 -0300 Subject: [pos] refactor configuration fetching and validation --- .../main/java/net/taler/merchantlib/MerchantApi.kt | 9 +++++++-- .../main/java/net/taler/merchantlib/MerchantConfig.kt | 5 +++-- .../src/main/java/net/taler/merchantlib/Response.kt | 19 +++++++++++++++++-- .../java/net/taler/merchantlib/MerchantApiTest.kt | 5 +++-- 4 files changed, 30 insertions(+), 8 deletions(-) (limited to 'merchant-lib/src') diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt index 06388f4..e995724 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt @@ -37,8 +37,8 @@ import net.taler.merchantlib.Response.Companion.response class MerchantApi(private val httpClient: HttpClient) { - suspend fun getConfig(baseUrl: String): ConfigResponse { - return httpClient.get("$baseUrl/config") + suspend fun getConfig(baseUrl: String): Response = response { + httpClient.get("$baseUrl/config") as ConfigResponse } suspend fun postOrder( @@ -77,6 +77,11 @@ class MerchantApi(private val httpClient: HttpClient) { } fun getDefaultHttpClient(): HttpClient = HttpClient(OkHttp) { + engine { + config { + retryOnConnectionFailure(true) + } + } install(JsonFeature) { serializer = getSerializer() } diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantConfig.kt b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantConfig.kt index 71185b9..a01624e 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantConfig.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantConfig.kt @@ -23,11 +23,12 @@ import kotlinx.serialization.Serializable data class MerchantConfig( @SerialName("base_url") val baseUrl: String, - val instance: String, + // TODO remove instance when it is part of baseURL + val instance: String? = null, @SerialName("api_key") val apiKey: String ) { - fun urlFor(endpoint: String, params: Map? = null): String { + fun urlFor(endpoint: String): String { val sb = StringBuilder(baseUrl) if (sb.last() != '/') sb.append('/') sb.append("instances/$instance/") diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt index eb1ef27..1b49d78 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt @@ -50,14 +50,29 @@ class Response private constructor( } } + suspend fun handleSuspend( + onFailure: ((String) -> Any)? = null, + onSuccess: (suspend (T) -> Any)? = null + ) { + if (value is Failure) onFailure?.let { it(getFailureString(value)) } + else onSuccess?.let { + @Suppress("UNCHECKED_CAST") + it(value as T) + } + } + private suspend fun getFailureString(failure: Failure): String = when (failure.exception) { is ClientRequestException -> getExceptionString(failure.exception) else -> failure.exception.toString() } private suspend fun getExceptionString(e: ClientRequestException): String { - val error: Error = e.response.receive() - return "Error ${error.code}: ${error.hint}" + return try { + val error: Error = e.response.receive() + "Error ${error.code}: ${error.hint}" + } catch (ex: Exception) { + "Status code: ${e.response.status.value}" + } } private class Failure(val exception: Throwable) diff --git a/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt b/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt index de1ca33..ea5a12a 100644 --- a/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt +++ b/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt @@ -46,8 +46,9 @@ class MerchantApiTest { } """.trimIndent() } - val response = api.getConfig("https://backend.int.taler.net") - assertEquals(ConfigResponse("0:0:0", "INTKUDOS"), response) + api.getConfig("https://backend.int.taler.net").assertSuccess { + assertEquals(ConfigResponse("0:0:0", "INTKUDOS"), it) + } } @Test -- cgit v1.2.3