diff options
Diffstat (limited to 'merchant-lib')
5 files changed, 23 insertions, 23 deletions
diff --git a/merchant-lib/build.gradle b/merchant-lib/build.gradle index 5082253..9b349ea 100644 --- a/merchant-lib/build.gradle +++ b/merchant-lib/build.gradle @@ -47,8 +47,6 @@ android { dependencies { api project(":taler-kotlin-android") - implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_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" 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 a4ca397..ea5b996 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt @@ -31,7 +31,6 @@ import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonConfiguration import net.taler.merchantlib.Response.Companion.response class MerchantApi( @@ -116,10 +115,8 @@ fun getDefaultHttpClient(): HttpClient = HttpClient(OkHttp) { } fun getSerializer() = KotlinxSerializer( - Json( - JsonConfiguration( - encodeDefaults = false, - ignoreUnknownKeys = true - ) - ) + Json { + encodeDefaults = false + ignoreUnknownKeys = true + } ) diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt b/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt index 0a405ad..9c23ef1 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt @@ -16,14 +16,16 @@ package net.taler.merchantlib -import kotlinx.serialization.Decoder -import kotlinx.serialization.Encoder import kotlinx.serialization.KSerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.Serializer -import kotlinx.serialization.json.JsonInput +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.JsonDecoder import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.boolean +import kotlinx.serialization.json.jsonPrimitive import net.taler.common.ContractTerms import net.taler.lib.common.Duration @@ -45,19 +47,20 @@ data class PostOrderResponse( sealed class CheckPaymentResponse { abstract val paid: Boolean + @Suppress("EXPERIMENTAL_API_USAGE") @Serializer(forClass = CheckPaymentResponse::class) companion object : KSerializer<CheckPaymentResponse> { override fun deserialize(decoder: Decoder): CheckPaymentResponse { - val input = decoder as JsonInput - val tree = input.decodeJson() as JsonObject - val orderStatus = tree.getPrimitive("order_status").content -// return if (orderStatus == "paid") decoder.json.fromJson(Paid.serializer(), tree) -// else decoder.json.fromJson(Unpaid.serializer(), tree) + val input = decoder as JsonDecoder + val tree = input.decodeJsonElement() as JsonObject + val orderStatus = tree.getValue("order_status").jsonPrimitive.content +// return if (orderStatus == "paid") decoder.json.decodeFromJsonElement(Paid.serializer(), tree) +// else decoder.json.decodeFromJsonElement(Unpaid.serializer(), tree) // manual parsing due to https://github.com/Kotlin/kotlinx.serialization/issues/576 return if (orderStatus == "paid") Paid( - refunded = tree.getPrimitive("refunded").boolean + refunded = tree.getValue("refunded").jsonPrimitive.boolean ) else Unpaid( - talerPayUri = tree.getPrimitive("taler_pay_uri").content + talerPayUri = tree.getValue("taler_pay_uri").jsonPrimitive.content ) } 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 fb48b46..1b63900 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt @@ -70,11 +70,12 @@ class Response<out T> private constructor( } private suspend fun getExceptionString(e: ResponseException): String { + val response = e.response ?: return e.toString() return try { - val error: Error = e.response.receive() + val error: Error = response.receive() "Error ${error.code}: ${error.hint}" } catch (ex: Exception) { - "Status code: ${e.response.status.value}" + "Status code: ${response.status.value}" } } diff --git a/merchant-lib/src/test/java/net/taler/merchantlib/MockHttpClient.kt b/merchant-lib/src/test/java/net/taler/merchantlib/MockHttpClient.kt index 993be15..c8e6f22 100644 --- a/merchant-lib/src/test/java/net/taler/merchantlib/MockHttpClient.kt +++ b/merchant-lib/src/test/java/net/taler/merchantlib/MockHttpClient.kt @@ -32,6 +32,7 @@ import io.ktor.http.content.TextContent import io.ktor.http.fullPath import io.ktor.http.headersOf import io.ktor.http.hostWithPort +import kotlinx.serialization.json.Json.Default.parseToJsonElement import org.junit.Assert.assertEquals object MockHttpClient { @@ -75,8 +76,8 @@ object MockHttpClient { private val Url.fullUrl: String get() = "${protocol.name}://$hostWithPortIfRequired$fullPath" private fun assertJsonEquals(json1: String, json2: String) { - val parsed1 = kotlinx.serialization.json.Json.parseJson(json1) - val parsed2 = kotlinx.serialization.json.Json.parseJson(json2) + val parsed1 = parseToJsonElement(json1) + val parsed2 = parseToJsonElement(json2) assertEquals(parsed1, parsed2) } |