summaryrefslogtreecommitdiff
path: root/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt
diff options
context:
space:
mode:
Diffstat (limited to 'merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt')
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt53
1 files changed, 28 insertions, 25 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 0d22f91..950cea6 100644
--- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt
+++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt
@@ -17,16 +17,19 @@
package net.taler.merchantlib
import io.ktor.client.HttpClient
+import io.ktor.client.call.body
import io.ktor.client.engine.okhttp.OkHttp
-import io.ktor.client.features.json.JsonFeature
-import io.ktor.client.features.json.serializer.KotlinxSerializer
+import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
+import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.delete
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.post
+import io.ktor.client.request.setBody
import io.ktor.http.ContentType.Application.Json
import io.ktor.http.HttpHeaders.Authorization
import io.ktor.http.contentType
+import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@@ -40,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()
}
}
@@ -50,10 +53,10 @@ class MerchantApi(
): Response<PostOrderResponse> = withContext(ioDispatcher) {
response {
httpClient.post(merchantConfig.urlFor("private/orders")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ auth(merchantConfig)
contentType(Json)
- body = orderRequest
- } as PostOrderResponse
+ setBody(orderRequest)
+ }.body()
}
}
@@ -63,8 +66,8 @@ class MerchantApi(
): Response<CheckPaymentResponse> = withContext(ioDispatcher) {
response {
httpClient.get(merchantConfig.urlFor("private/orders/$orderId")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
- } as CheckPaymentResponse
+ auth(merchantConfig)
+ }.body()
}
}
@@ -74,8 +77,8 @@ class MerchantApi(
): Response<Unit> = withContext(ioDispatcher) {
response {
httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
- } as Unit
+ auth(merchantConfig)
+ }.body()
}
}
@@ -83,8 +86,8 @@ class MerchantApi(
withContext(ioDispatcher) {
response {
httpClient.get(merchantConfig.urlFor("private/orders")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
- } as OrderHistory
+ auth(merchantConfig)
+ }.body()
}
}
@@ -95,29 +98,29 @@ class MerchantApi(
): Response<RefundResponse> = withContext(ioDispatcher) {
response {
httpClient.post(merchantConfig.urlFor("private/orders/$orderId/refund")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ auth(merchantConfig)
contentType(Json)
- body = request
- } as RefundResponse
+ setBody(request)
+ }.body()
}
}
+
+ private fun HttpRequestBuilder.auth(merchantConfig: MerchantConfig) {
+ header(Authorization, "Bearer ${merchantConfig.apiKey}")
+ }
}
fun getDefaultHttpClient(): HttpClient = HttpClient(OkHttp) {
+ expectSuccess = true
engine {
config {
retryOnConnectionFailure(true)
}
}
- install(JsonFeature) {
- serializer = getSerializer()
+ install(ContentNegotiation) {
+ json(Json {
+ encodeDefaults = false
+ ignoreUnknownKeys = true
+ })
}
}
-
-fun getSerializer() = KotlinxSerializer(
- Json {
- encodeDefaults = false
- ignoreUnknownKeys = true
- classDiscriminator = "order_status"
- }
-)