diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/webclient/KtorWebClient.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/webclient/KtorWebClient.kt index 4137579c..6a2ddf70 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/webclient/KtorWebClient.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/webclient/KtorWebClient.kt @@ -19,7 +19,7 @@ open class KtorWebClient : IWebClient { } - protected val client = HttpClient() { + protected val client = HttpClient { } @@ -37,20 +37,24 @@ open class KtorWebClient : IWebClient { override fun post(url: String, body: String, contentType: String, userAgent: String, callback: (WebClientResponse) -> Unit) { GlobalScope.async { - try { - val clientResponse = client.post(url) { - contentType(ContentType.Application.OctetStream) - setBody(body) - } + postInCoroutine(url, body, contentType, userAgent, callback) + } + } - val responseBody = clientResponse.bodyAsText() - - callback(WebClientResponse(clientResponse.status.value == 200, clientResponse.status.value, body = responseBody)) - } catch (e: Exception) { - log.error(e) { "Could not send request to url '$url'" } - - callback(WebClientResponse(false, error = e)) + protected open suspend fun postInCoroutine(url: String, body: String, contentType: String, userAgent: String, callback: (WebClientResponse) -> Unit) { + try { + val clientResponse = client.post(url) { + contentType(ContentType.Application.OctetStream) + setBody(body) } + + val responseBody = clientResponse.bodyAsText() + + callback(WebClientResponse(clientResponse.status.value == 200, clientResponse.status.value, body = responseBody)) + } catch (e: Exception) { + log.error(e) { "Could not send request to url '$url'" } + + callback(WebClientResponse(false, error = e)) } } diff --git a/fints4k/src/nativeMain/kotlin/net.dankito.banking.fints.webclient/BlockingKtorWebClient.kt b/fints4k/src/nativeMain/kotlin/net.dankito.banking.fints.webclient/BlockingKtorWebClient.kt new file mode 100644 index 00000000..479b9f48 --- /dev/null +++ b/fints4k/src/nativeMain/kotlin/net.dankito.banking.fints.webclient/BlockingKtorWebClient.kt @@ -0,0 +1,14 @@ +package net.dankito.banking.fints.webclient +import kotlinx.coroutines.runBlocking + + +open class BlockingKtorWebClient : KtorWebClient() { + + + override fun post(url: String, body: String, contentType: String, userAgent: String, callback: (WebClientResponse) -> Unit) { + runBlocking { + postInCoroutine(url, body, contentType, userAgent, callback) + } + } + +} \ No newline at end of file