aboutsummaryrefslogtreecommitdiff
path: root/merchant-lib/src
diff options
context:
space:
mode:
Diffstat (limited to 'merchant-lib/src')
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt16
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt8
-rw-r--r--merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt3
-rw-r--r--merchant-lib/src/test/java/net/taler/merchantlib/MockHttpClient.kt12
4 files changed, 29 insertions, 10 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 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)