From f0670e2f3936f0223c02e9ec0d0de52f31a3539f Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 11 Aug 2020 09:41:40 -0300 Subject: [pos] Improve coroutine-based merchant library access --- .../main/java/net/taler/merchantlib/MerchantApi.kt | 76 +++++++++++++--------- .../main/java/net/taler/merchantlib/Response.kt | 4 +- 2 files changed, 48 insertions(+), 32 deletions(-) (limited to 'merchant-lib/src/main') 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 = response { - httpClient.get("$baseUrl/config") as ConfigResponse + suspend fun getConfig(baseUrl: String): Response = withContext(ioDispatcher) { + response { + httpClient.get("$baseUrl/config") as ConfigResponse + } } suspend fun postOrder( merchantConfig: MerchantConfig, orderRequest: PostOrderRequest - ): Response = response { - httpClient.post(merchantConfig.urlFor("private/orders")) { - header(Authorization, "ApiKey ${merchantConfig.apiKey}") - contentType(Json) - body = orderRequest - } as PostOrderResponse + ): Response = 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 = response { - httpClient.get(merchantConfig.urlFor("private/orders/$orderId")) { - header(Authorization, "ApiKey ${merchantConfig.apiKey}") - } as CheckPaymentResponse + ): Response = 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 = response { - httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) { - header(Authorization, "ApiKey ${merchantConfig.apiKey}") - } as Unit + ): Response = withContext(ioDispatcher) { + response { + httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) { + header(Authorization, "ApiKey ${merchantConfig.apiKey}") + } as Unit + } } - suspend fun getOrderHistory(merchantConfig: MerchantConfig): Response = response { - httpClient.get(merchantConfig.urlFor("private/orders")) { - header(Authorization, "ApiKey ${merchantConfig.apiKey}") - } as OrderHistory - } + suspend fun getOrderHistory(merchantConfig: MerchantConfig): Response = + 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 = response { - httpClient.post(merchantConfig.urlFor("private/orders/$orderId/refund")) { - header(Authorization, "ApiKey ${merchantConfig.apiKey}") - contentType(Json) - body = request - } as RefundResponse + ): Response = 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 private constructor( private val value: Any? ) { - companion object { suspend fun response(request: suspend () -> T): Response { return try { @@ -45,7 +44,7 @@ class Response 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 private constructor( val code: Int?, val hint: String? ) - } -- cgit v1.2.3