diff options
Diffstat (limited to 'merchant-lib')
5 files changed, 34 insertions, 13 deletions
diff --git a/merchant-lib/build.gradle b/merchant-lib/build.gradle index 93e2d4d..128f4c1 100644 --- a/merchant-lib/build.gradle +++ b/merchant-lib/build.gradle @@ -45,14 +45,13 @@ android { } dependencies { - implementation project(":taler-kotlin-common") + api project(":taler-kotlin-common") implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - def ktor_version = "1.3.2" - implementation "io.ktor:ktor-client:$ktor_version" - implementation "io.ktor:ktor-client-okhttp:$ktor_version" - implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version" + api "io.ktor:ktor-client:$ktor_version" + api "io.ktor:ktor-client-okhttp:$ktor_version" + api "io.ktor:ktor-client-serialization-jvm:$ktor_version" testImplementation 'junit:junit:4.13' testImplementation "io.ktor:ktor-client-mock-jvm:$ktor_version" 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 |