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:
parent
aa532b864c
commit
7534c6eb54
|
@ -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) {
|
override fun post(url: String, body: String, contentType: String, userAgent: String, callback: (WebClientResponse) -> Unit) {
|
||||||
GlobalScope.async {
|
GlobalScope.async {
|
||||||
try {
|
postInCoroutine(url, body, contentType, userAgent, callback)
|
||||||
val clientResponse = client.post(url) {
|
}
|
||||||
contentType(ContentType.Application.OctetStream)
|
}
|
||||||
setBody(body)
|
|
||||||
}
|
|
||||||
|
|
||||||
val responseBody = clientResponse.bodyAsText()
|
protected open suspend fun postInCoroutine(url: String, body: String, contentType: String, userAgent: String, callback: (WebClientResponse) -> Unit) {
|
||||||
|
try {
|
||||||
callback(WebClientResponse(clientResponse.status.value == 200, clientResponse.status.value, body = responseBody))
|
val clientResponse = client.post(url) {
|
||||||
} catch (e: Exception) {
|
contentType(ContentType.Application.OctetStream)
|
||||||
log.error(e) { "Could not send request to url '$url'" }
|
setBody(body)
|
||||||
|
|
||||||
callback(WebClientResponse(false, error = e))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue