Extracted postInCoroutine() and created a blocking WebClient for Kotlin/Native so that mutable objects don't get passed between threads in Kotlin/Native

This commit is contained in:
dankito 2022-02-13 22:07:11 +01:00
parent aa532b864c
commit 7534c6eb54
2 changed files with 31 additions and 13 deletions

View File

@ -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))
}
}

View File

@ -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)
}
}
}