Replaced kmp-web-client with directly configuring Ktor

This commit is contained in:
dankito 2024-08-19 08:56:05 +02:00
parent e96f184934
commit 0ce74a0688
3 changed files with 55 additions and 5 deletions

View File

@ -71,20 +71,23 @@ kotlin {
implementation "co.touchlab:stately-concurrency:1.2.0"
implementation "io.ktor:ktor-client-core:$ktorVersion" // only left here cause kmp-web-client doesn't expose ktor as api
implementation("net.dankito.web.client:kmp-web-client:1.0.0-SNAPSHOT")
implementation("io.ktor:ktor-client-core:$ktorVersion")
}
}
commonTest {
dependencies {
implementation kotlin("test")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion")
}
}
jvmMain {
dependencies {
// or use client-java or client-okhttp?
implementation("io.ktor:ktor-client-cio:$ktorVersion")
}
}
@ -104,7 +107,7 @@ kotlin {
jsMain {
dependencies {
implementation("io.ktor:ktor-client-js:$ktorVersion")
}
}
@ -118,5 +121,23 @@ kotlin {
}
}
linuxMain {
dependencies {
implementation("io.ktor:ktor-client-curl:$ktorVersion")
}
}
mingwMain {
dependencies {
implementation("io.ktor:ktor-client-winhttp:$ktorVersion")
}
}
appleMain {
dependencies {
implementation("io.ktor:ktor-client-darwin:$ktorVersion")
}
}
}
}

View File

@ -2,8 +2,7 @@ package net.dankito.banking.fints.webclient
import io.ktor.client.HttpClient
import io.ktor.client.plugins.*
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.request.*
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import io.ktor.http.contentType
@ -38,6 +37,14 @@ open class KtorWebClient(
}
suspend fun get(url: String): WebClientResponse {
val clientResponse = client.get(url)
val responseBody = clientResponse.bodyAsText()
return WebClientResponse(clientResponse.status.value == 200, clientResponse.status.value, body = responseBody)
}
override suspend fun post(url: String, body: String, contentType: String, userAgent: String): WebClientResponse {
return postInCoroutine(url, body, contentType, userAgent)
}

View File

@ -0,0 +1,22 @@
package net.dankito.banking.fints.webclient
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
class KtorWebClientTest {
private val underTest = KtorWebClient()
@Test
fun get() = runTest {
val result = underTest.get("https://staging.dankito.net/bankfinder?maxItems=1&query=720")
assertTrue(result.successful)
assertEquals(200, result.responseCode)
assertNotNull(result.body)
}
}