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.kt76
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/Response.kt4
2 files changed, 48 insertions, 32 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 c92d4d2..a4ca397 100644
--- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt
+++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt
@@ -27,63 +27,81 @@ import io.ktor.client.request.post
import io.ktor.http.ContentType.Application.Json
import io.ktor.http.HttpHeaders.Authorization
import io.ktor.http.contentType
+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(private val httpClient: HttpClient) {
+class MerchantApi(
+ private val httpClient: HttpClient = getDefaultHttpClient(),
+ private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
+) {
- suspend fun getConfig(baseUrl: String): Response<ConfigResponse> = response {
- httpClient.get("$baseUrl/config") as ConfigResponse
+ suspend fun getConfig(baseUrl: String): Response<ConfigResponse> = withContext(ioDispatcher) {
+ response {
+ httpClient.get("$baseUrl/config") as ConfigResponse
+ }
}
suspend fun postOrder(
merchantConfig: MerchantConfig,
orderRequest: PostOrderRequest
- ): Response<PostOrderResponse> = response {
- httpClient.post(merchantConfig.urlFor("private/orders")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
- contentType(Json)
- body = orderRequest
- } as PostOrderResponse
+ ): Response<PostOrderResponse> = withContext(ioDispatcher) {
+ response {
+ httpClient.post(merchantConfig.urlFor("private/orders")) {
+ header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ contentType(Json)
+ body = orderRequest
+ } as PostOrderResponse
+ }
}
suspend fun checkOrder(
merchantConfig: MerchantConfig,
orderId: String
- ): Response<CheckPaymentResponse> = response {
- httpClient.get(merchantConfig.urlFor("private/orders/$orderId")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
- } as CheckPaymentResponse
+ ): Response<CheckPaymentResponse> = withContext(ioDispatcher) {
+ response {
+ httpClient.get(merchantConfig.urlFor("private/orders/$orderId")) {
+ header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ } as CheckPaymentResponse
+ }
}
suspend fun deleteOrder(
merchantConfig: MerchantConfig,
orderId: String
- ): Response<Unit> = response {
- httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
- } as Unit
+ ): Response<Unit> = withContext(ioDispatcher) {
+ response {
+ httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) {
+ header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ } as Unit
+ }
}
- suspend fun getOrderHistory(merchantConfig: MerchantConfig): Response<OrderHistory> = response {
- httpClient.get(merchantConfig.urlFor("private/orders")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
- } as OrderHistory
- }
+ suspend fun getOrderHistory(merchantConfig: MerchantConfig): Response<OrderHistory> =
+ withContext(ioDispatcher) {
+ response {
+ httpClient.get(merchantConfig.urlFor("private/orders")) {
+ header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ } as OrderHistory
+ }
+ }
suspend fun giveRefund(
merchantConfig: MerchantConfig,
orderId: String,
request: RefundRequest
- ): Response<RefundResponse> = response {
- httpClient.post(merchantConfig.urlFor("private/orders/$orderId/refund")) {
- header(Authorization, "ApiKey ${merchantConfig.apiKey}")
- contentType(Json)
- body = request
- } as RefundResponse
+ ): Response<RefundResponse> = withContext(ioDispatcher) {
+ response {
+ httpClient.post(merchantConfig.urlFor("private/orders/$orderId/refund")) {
+ header(Authorization, "ApiKey ${merchantConfig.apiKey}")
+ contentType(Json)
+ body = request
+ } as RefundResponse
+ }
}
-
}
fun getDefaultHttpClient(): HttpClient = HttpClient(OkHttp) {
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 65a12a9..fb48b46 100644
--- a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt
+++ b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt
@@ -25,7 +25,6 @@ import kotlinx.serialization.Serializable
class Response<out T> private constructor(
private val value: Any?
) {
-
companion object {
suspend fun <T> response(request: suspend () -> T): Response<T> {
return try {
@@ -45,7 +44,7 @@ class Response<out T> private constructor(
val isFailure: Boolean get() = value is Failure
- suspend fun handle(onFailure: ((String) -> Any)? = null, onSuccess: ((T) -> Any)? = null) {
+ suspend fun handle(onFailure: ((String) -> Unit)? = null, onSuccess: ((T) -> Unit)? = null) {
if (value is Failure) onFailure?.let { it(getFailureString(value)) }
else onSuccess?.let {
@Suppress("UNCHECKED_CAST")
@@ -86,5 +85,4 @@ class Response<out T> private constructor(
val code: Int?,
val hint: String?
)
-
}