summaryrefslogtreecommitdiff
path: root/merchant-lib/src/main/java/net
diff options
context:
space:
mode:
Diffstat (limited to 'merchant-lib/src/main/java/net')
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt20
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt4
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/Response.kt10
3 files changed, 18 insertions, 16 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 c02907b..950cea6 100644
--- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt
+++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt
@@ -20,6 +20,7 @@ import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.okhttp.OkHttp
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
@@ -28,13 +29,12 @@ 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
-import net.taler.merchantlib.Response.Companion.response
-import io.ktor.serialization.kotlinx.json.*
-import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
+import net.taler.merchantlib.Response.Companion.response
class MerchantApi(
private val httpClient: HttpClient = getDefaultHttpClient(),
@@ -53,7 +53,7 @@ class MerchantApi(
): Response<PostOrderResponse> = withContext(ioDispatcher) {
response {
httpClient.post(merchantConfig.urlFor("private/orders")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ auth(merchantConfig)
contentType(Json)
setBody(orderRequest)
}.body()
@@ -66,7 +66,7 @@ class MerchantApi(
): Response<CheckPaymentResponse> = withContext(ioDispatcher) {
response {
httpClient.get(merchantConfig.urlFor("private/orders/$orderId")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ auth(merchantConfig)
}.body()
}
}
@@ -77,7 +77,7 @@ class MerchantApi(
): Response<Unit> = withContext(ioDispatcher) {
response {
httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ auth(merchantConfig)
}.body()
}
}
@@ -86,7 +86,7 @@ class MerchantApi(
withContext(ioDispatcher) {
response {
httpClient.get(merchantConfig.urlFor("private/orders")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ auth(merchantConfig)
}.body()
}
}
@@ -98,12 +98,16 @@ class MerchantApi(
): Response<RefundResponse> = withContext(ioDispatcher) {
response {
httpClient.post(merchantConfig.urlFor("private/orders/$orderId/refund")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ auth(merchantConfig)
contentType(Json)
setBody(request)
}.body()
}
}
+
+ private fun HttpRequestBuilder.auth(merchantConfig: MerchantConfig) {
+ header(Authorization, "Bearer ${merchantConfig.apiKey}")
+ }
}
fun getDefaultHttpClient(): HttpClient = HttpClient(OkHttp) {
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 391abf5..9348ded 100644
--- a/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt
+++ b/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt
@@ -21,14 +21,14 @@ 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.common.RelativeTime
@Serializable
data class PostOrderRequest(
@SerialName("order")
val contractTerms: ContractTerms,
@SerialName("refund_delay")
- val refundDelay: Duration? = null
+ val refundDelay: RelativeTime? = null
)
@Serializable
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 b7ba1ac..55de077 100644
--- a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt
+++ b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt
@@ -17,9 +17,7 @@
package net.taler.merchantlib
import io.ktor.client.call.body
-import io.ktor.client.plugins.ClientRequestException
import io.ktor.client.plugins.ResponseException
-import io.ktor.client.plugins.ServerResponseException
import kotlinx.serialization.Serializable
class Response<out T> private constructor(
@@ -64,8 +62,7 @@ class Response<out T> private constructor(
}
private suspend fun getFailureString(failure: Failure): String = when (failure.exception) {
- is ClientRequestException -> getExceptionString(failure.exception)
- is ServerResponseException -> getExceptionString(failure.exception)
+ is ResponseException -> getExceptionString(failure.exception)
else -> failure.exception.toString()
}
@@ -73,7 +70,7 @@ class Response<out T> private constructor(
val response = e.response
return try {
val error: Error = response.body()
- "Error ${error.code}: ${error.hint}"
+ "Error ${error.code} (${response.status.value}): ${error.hint} ${error.detail}"
} catch (ex: Exception) {
"Status code: ${response.status.value}"
}
@@ -84,6 +81,7 @@ class Response<out T> private constructor(
@Serializable
private class Error(
val code: Int?,
- val hint: String?
+ val hint: String?,
+ val detail: String? = null,
)
}