diff options
author | Florian Dold <florian@dold.me> | 2022-06-01 13:31:38 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2022-06-01 13:31:38 +0200 |
commit | a229b33997842a66295aa4d8dfb5dab6d2f769df (patch) | |
tree | 72031a742800ceaae337f04377fb052c91cc2068 /merchant-lib | |
parent | 1ef7ae77475f5dd99401a992a92f6c622b29c0a6 (diff) | |
download | taler-android-a229b33997842a66295aa4d8dfb5dab6d2f769df.tar.gz taler-android-a229b33997842a66295aa4d8dfb5dab6d2f769df.tar.bz2 taler-android-a229b33997842a66295aa4d8dfb5dab6d2f769df.zip |
use new annotation for polymorphic serialization
Diffstat (limited to 'merchant-lib')
5 files changed, 35 insertions, 10 deletions
diff --git a/merchant-lib/build.gradle b/merchant-lib/build.gradle index c31009d..42dc3ec 100644 --- a/merchant-lib/build.gradle +++ b/merchant-lib/build.gradle @@ -65,3 +65,9 @@ dependencies { testImplementation "io.ktor:ktor-client-mock-jvm:$ktor_version" testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1' } + +tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { + kotlinOptions { + freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn" + } +}
\ No newline at end of file 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 d973813..c02907b 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt @@ -33,6 +33,8 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import net.taler.merchantlib.Response.Companion.response import io.ktor.serialization.kotlinx.json.* +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json class MerchantApi( private val httpClient: HttpClient = getDefaultHttpClient(), @@ -41,7 +43,7 @@ class MerchantApi( suspend fun getConfig(baseUrl: String): Response<ConfigResponse> = withContext(ioDispatcher) { response { - httpClient.get("$baseUrl/config") as ConfigResponse + httpClient.get("$baseUrl/config").body() } } @@ -76,7 +78,7 @@ class MerchantApi( response { httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) { header(Authorization, "ApiKey ${merchantConfig.apiKey}") - } as Unit + }.body() } } @@ -85,7 +87,7 @@ class MerchantApi( response { httpClient.get(merchantConfig.urlFor("private/orders")) { header(Authorization, "ApiKey ${merchantConfig.apiKey}") - } as OrderHistory + }.body() } } @@ -99,18 +101,22 @@ class MerchantApi( header(Authorization, "ApiKey ${merchantConfig.apiKey}") contentType(Json) setBody(request) - } as RefundResponse + }.body() } } } fun getDefaultHttpClient(): HttpClient = HttpClient(OkHttp) { + expectSuccess = true engine { config { retryOnConnectionFailure(true) } } install(ContentNegotiation) { - json() + json(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 9572e07..391abf5 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt @@ -16,11 +16,12 @@ package net.taler.merchantlib +import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonClassDiscriminator import net.taler.common.ContractTerms import net.taler.common.Duration -import net.taler.lib.android.CustomClassDiscriminator @Serializable data class PostOrderRequest( @@ -36,9 +37,10 @@ data class PostOrderResponse( val orderId: String ) +@OptIn(ExperimentalSerializationApi::class) @Serializable -sealed class CheckPaymentResponse: CustomClassDiscriminator { - override val discriminator: String = "order_status" +@JsonClassDiscriminator("order_status") +sealed class CheckPaymentResponse { abstract val paid: Boolean @Serializable 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 8a45c9f..a89e2d3 100644 --- a/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt +++ b/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt @@ -20,6 +20,7 @@ import io.ktor.http.HttpStatusCode.Companion.NotFound import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.TestCoroutineDispatcher +import kotlinx.coroutines.test.UnconfinedTestDispatcher import net.taler.common.Amount import net.taler.common.ContractProduct import net.taler.common.ContractTerms @@ -33,7 +34,7 @@ import org.junit.Test @ExperimentalCoroutinesApi class MerchantApiTest { - private val api = MerchantApi(httpClient, TestCoroutineDispatcher()) + private val api = MerchantApi(httpClient, UnconfinedTestDispatcher()) private val merchantConfig = MerchantConfig( baseUrl = "http://example.net/instances/testInstance", apiKey = "apiKeyFooBar" 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 880228c..32c7ee1 100644 --- a/merchant-lib/src/test/java/net/taler/merchantlib/MockHttpClient.kt +++ b/merchant-lib/src/test/java/net/taler/merchantlib/MockHttpClient.kt @@ -20,6 +20,7 @@ import io.ktor.client.HttpClient import io.ktor.client.engine.mock.MockEngine import io.ktor.client.engine.mock.MockEngineConfig import io.ktor.client.engine.mock.respond +import io.ktor.client.plugins.contentnegotiation.ContentNegotiation import io.ktor.http.ContentType.Application.Json import io.ktor.http.HttpStatusCode import io.ktor.http.Url @@ -27,6 +28,8 @@ import io.ktor.http.content.TextContent import io.ktor.http.fullPath import io.ktor.http.headersOf import io.ktor.http.hostWithPort +import io.ktor.serialization.kotlinx.json.json +import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json.Default.parseToJsonElement import org.junit.Assert.assertEquals @@ -36,13 +39,20 @@ object MockHttpClient { engine { addHandler { error("No response handler set") } } + expectSuccess = true + install(ContentNegotiation) { + json(Json { + encodeDefaults = false + ignoreUnknownKeys = true + }) + } } fun HttpClient.giveJsonResponse( url: String, expectedBody: String? = null, statusCode: HttpStatusCode = HttpStatusCode.OK, - jsonProducer: () -> String + jsonProducer: () -> String, ) { val httpConfig = engineConfig as MockEngineConfig httpConfig.requestHandlers.removeAt(0) |