aboutsummaryrefslogtreecommitdiff
path: root/merchant-lib/src
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-07-23 15:41:50 -0300
committerTorsten Grote <t@grobox.de>2020-07-23 15:42:43 -0300
commit8eb241ccce345a35b05a6335d11306465220f66d (patch)
treefe78879b2c0cafdb9ba47f38db6a3572a7116594 /merchant-lib/src
parent08b10a2408f958cae96ae0c674ee450a35109e8a (diff)
downloadtaler-android-8eb241ccce345a35b05a6335d11306465220f66d.tar.gz
taler-android-8eb241ccce345a35b05a6335d11306465220f66d.tar.bz2
taler-android-8eb241ccce345a35b05a6335d11306465220f66d.zip
[pos] refactor configuration fetching and validation
Diffstat (limited to 'merchant-lib/src')
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt9
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/MerchantConfig.kt5
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/Response.kt19
-rw-r--r--merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt5
4 files changed, 30 insertions, 8 deletions
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<ConfigResponse> = 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<String, String>? = 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<out T> 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