From 4495cf0d8601bffc9260f99bb4e34edb85a598c3 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 18 Sep 2023 10:11:06 +0200 Subject: [PATCH 1/7] replace server: netty -> ktorcio, replace client: apache -> okhttp --- packages/patrol/android/build.gradle | 6 ++-- .../leancode/patrol/PatrolAppServiceClient.kt | 8 ++--- .../kotlin/pl/leancode/patrol/PatrolServer.kt | 4 +-- .../contracts/PatrolAppServiceClient.kt | 29 ++++++++++--------- .../patrol/example/android/app/build.gradle | 10 +++---- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/packages/patrol/android/build.gradle b/packages/patrol/android/build.gradle index 9aa4b1894..c3ad2da43 100644 --- a/packages/patrol/android/build.gradle +++ b/packages/patrol/android/build.gradle @@ -66,9 +66,9 @@ android { api "androidx.test.uiautomator:uiautomator:2.2.0" implementation platform("org.http4k:http4k-bom:5.7.4.0") - implementation "org.http4k:http4k-core:5.7.4.0" - implementation "org.http4k:http4k-client-apache:5.7.4.0" - implementation "org.http4k:http4k-server-netty:5.7.4.0" + implementation "org.http4k:http4k-core" + implementation "org.http4k:http4k-client-okhttp" + implementation "org.http4k:http4k-server-ktorcio" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-guava:1.5.2" diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt index eb163fbdf..4719ce611 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt @@ -1,6 +1,6 @@ package pl.leancode.patrol -import org.apache.hc.core5.util.Timeout +import okio.Timeout import pl.leancode.patrol.contracts.Contracts import pl.leancode.patrol.contracts.PatrolAppServiceClientException import pl.leancode.patrol.contracts.PatrolAppServiceClient as Client @@ -13,15 +13,15 @@ class PatrolAppServiceClient { private var client: Client // https://github.com/leancodepl/patrol/issues/1683 - private val timeout = Timeout.ofHours(2) + // private val timeout = Timeout.ofHours(2) constructor() { - client = Client(address = "localhost", port = 8082, timeout = timeout) + client = Client(address = "localhost", port = 8082) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } constructor(address: String) { - client = Client(address = address, port = 8082, timeout = timeout) + client = Client(address = address, port = 8082) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt index bfeb11c39..3e8d395a8 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt @@ -5,7 +5,7 @@ import com.google.common.util.concurrent.SettableFuture import org.http4k.core.ContentType import org.http4k.filter.ServerFilters import org.http4k.server.Http4kServer -import org.http4k.server.Netty +import org.http4k.server.KtorCIO import org.http4k.server.asServer import java.util.concurrent.Future @@ -29,7 +29,7 @@ class PatrolServer { .withFilter(catcher) .withFilter(printer) .withFilter(ServerFilters.SetContentType(ContentType.TEXT_PLAIN)) - .asServer(Netty(port)) + .asServer(KtorCIO(port)) .start() Logger.i("Created and started PatrolServer, port: $port") diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt index f1a9b8c7a..a85437a77 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt @@ -7,15 +7,20 @@ package pl.leancode.patrol.contracts; import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json -import org.apache.hc.client5.http.config.RequestConfig -import org.apache.hc.client5.http.impl.classic.HttpClients -import org.apache.hc.core5.util.Timeout -import org.http4k.client.ApacheClient +import okhttp3.OkHttpClient +import okio.Timeout +import org.http4k.client.OkHttp +//import org.apache.hc.client5.http.config.RequestConfig +//import org.apache.hc.client5.http.impl.classic.HttpClients +//import org.apache.hc.core5.util.Timeout +// import org.http4k.client.ApacheClient import org.http4k.core.Method import org.http4k.core.Request import org.http4k.core.Status +import org.http4k.metrics.MetricsDefaults.Companion.client +import java.time.Duration -class PatrolAppServiceClient(private val address: String, private val port: Int, private val timeout: Timeout) { +class PatrolAppServiceClient(private val address: String, private val port: Int) { fun listDartTests() : Contracts.ListDartTestsResponse { val response = performRequest("listDartTests") @@ -33,16 +38,12 @@ class PatrolAppServiceClient(private val address: String, private val port: Int, request = request.body(requestBody) } - val client = ApacheClient( - HttpClients.custom().setDefaultRequestConfig( - RequestConfig - .copy(RequestConfig.DEFAULT) - .setResponseTimeout(timeout) - .setConnectionRequestTimeout(timeout) - .build() - ).build()) + // FIXME: Figure out how to add timeouts (java.time is API 26+ only) + val okHttpClient = OkHttp(OkHttpClient.Builder() + .build() + ) - val response = client(request) + val response = okHttpClient (request) if (response.status != Status.OK) { throw PatrolAppServiceClientException("Invalid response ${response.status}, ${response.bodyString()}") diff --git a/packages/patrol/example/android/app/build.gradle b/packages/patrol/example/android/app/build.gradle index 55bba1cac..7cb38b0df 100644 --- a/packages/patrol/example/android/app/build.gradle +++ b/packages/patrol/example/android/app/build.gradle @@ -34,11 +34,11 @@ android { } //TODO verify - packagingOptions { - pickFirst "META-INF/INDEX.LIST" - pickFirst "META-INF/io.netty.versions.properties" - pickFirst "META-INF/DEPENDENCIES" - } +// packagingOptions { +// pickFirst "META-INF/INDEX.LIST" +// pickFirst "META-INF/io.netty.versions.properties" +// pickFirst "META-INF/DEPENDENCIES" +// } sourceSets { main.java.srcDirs += "src/main/kotlin" From 352fe77ede684d01f3b79f42ca7c278a378cd83f Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 18 Sep 2023 10:27:10 +0200 Subject: [PATCH 2/7] bring back timeout --- .../leancode/patrol/PatrolAppServiceClient.kt | 9 +++---- .../contracts/PatrolAppServiceClient.kt | 24 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt index 4719ce611..7bdfa7bad 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt @@ -1,8 +1,8 @@ package pl.leancode.patrol -import okio.Timeout import pl.leancode.patrol.contracts.Contracts import pl.leancode.patrol.contracts.PatrolAppServiceClientException +import java.util.concurrent.TimeUnit import pl.leancode.patrol.contracts.PatrolAppServiceClient as Client /** @@ -13,15 +13,16 @@ class PatrolAppServiceClient { private var client: Client // https://github.com/leancodepl/patrol/issues/1683 - // private val timeout = Timeout.ofHours(2) + private val timeout = 2L + private val timeUnit = TimeUnit.HOURS constructor() { - client = Client(address = "localhost", port = 8082) + client = Client(address = "localhost", port = 8082, timeout = timeout, timeUnit = timeUnit) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } constructor(address: String) { - client = Client(address = address, port = 8082) + client = Client(address = address, port = 8082, timeout = timeout, timeUnit = timeUnit) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt index a85437a77..54edd46d8 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt @@ -8,26 +8,21 @@ package pl.leancode.patrol.contracts; import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import okhttp3.OkHttpClient -import okio.Timeout import org.http4k.client.OkHttp -//import org.apache.hc.client5.http.config.RequestConfig -//import org.apache.hc.client5.http.impl.classic.HttpClients -//import org.apache.hc.core5.util.Timeout -// import org.http4k.client.ApacheClient import org.http4k.core.Method import org.http4k.core.Request import org.http4k.core.Status -import org.http4k.metrics.MetricsDefaults.Companion.client import java.time.Duration +import java.util.concurrent.TimeUnit -class PatrolAppServiceClient(private val address: String, private val port: Int) { +class PatrolAppServiceClient(address: String, port: Int, private val timeout: Long, private val timeUnit: TimeUnit) { - fun listDartTests() : Contracts.ListDartTestsResponse { + fun listDartTests(): Contracts.ListDartTestsResponse { val response = performRequest("listDartTests") return json.decodeFromString(response) } - fun runDartTest(request: Contracts.RunDartTestRequest) : Contracts.RunDartTestResponse { + fun runDartTest(request: Contracts.RunDartTestRequest): Contracts.RunDartTestResponse { val response = performRequest("runDartTest", json.encodeToString(request)) return json.decodeFromString(response) } @@ -39,11 +34,14 @@ class PatrolAppServiceClient(private val address: String, private val port: Int) } // FIXME: Figure out how to add timeouts (java.time is API 26+ only) - val okHttpClient = OkHttp(OkHttpClient.Builder() - .build() + val okHttpClient = OkHttp( + OkHttpClient.Builder() + // all other timeouts (write, read, connect) default to 10 seconds + .callTimeout(timeout, timeUnit) + .build() ) - - val response = okHttpClient (request) + + val response = okHttpClient(request) if (response.status != Status.OK) { throw PatrolAppServiceClientException("Invalid response ${response.status}, ${response.bodyString()}") From feb4b6785aa50798d0a1460e4bd7c3945667ec44 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 18 Sep 2023 10:28:04 +0200 Subject: [PATCH 3/7] example app: remove packagingOptions --- packages/patrol/example/android/app/build.gradle | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/patrol/example/android/app/build.gradle b/packages/patrol/example/android/app/build.gradle index 7cb38b0df..cb9dd3f5c 100644 --- a/packages/patrol/example/android/app/build.gradle +++ b/packages/patrol/example/android/app/build.gradle @@ -32,13 +32,6 @@ android { kotlinOptions { jvmTarget = "1.8" } - - //TODO verify -// packagingOptions { -// pickFirst "META-INF/INDEX.LIST" -// pickFirst "META-INF/io.netty.versions.properties" -// pickFirst "META-INF/DEPENDENCIES" -// } sourceSets { main.java.srcDirs += "src/main/kotlin" From ce54fac5bcb8936fdac51a7c21f16b7db7fb6bb9 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 18 Sep 2023 10:58:20 +0200 Subject: [PATCH 4/7] bring back timeouts --- .../pl/leancode/patrol/contracts/PatrolAppServiceClient.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt index 54edd46d8..0bd9b9d92 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt @@ -33,10 +33,11 @@ class PatrolAppServiceClient(address: String, port: Int, private val timeout: Lo request = request.body(requestBody) } - // FIXME: Figure out how to add timeouts (java.time is API 26+ only) val okHttpClient = OkHttp( OkHttpClient.Builder() - // all other timeouts (write, read, connect) default to 10 seconds + .connectTimeout(timeout, timeUnit) + .readTimeout(timeout, timeUnit) + .writeTimeout(timeout, timeUnit) .callTimeout(timeout, timeUnit) .build() ) From 2966060f6c5939bc5701fa1b9251de24ed6eb9f9 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 18 Sep 2023 11:39:32 +0200 Subject: [PATCH 5/7] try using JavaHttpClient - not available on Android --- .../contracts/PatrolAppServiceClient.kt | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt index 0bd9b9d92..58f7de84c 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt @@ -7,11 +7,18 @@ package pl.leancode.patrol.contracts; import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json -import okhttp3.OkHttpClient -import org.http4k.client.OkHttp +import org.http4k.client.JavaHttpClient +import org.http4k.core.HttpHandler import org.http4k.core.Method import org.http4k.core.Request import org.http4k.core.Status +import org.http4k.core.then +import org.http4k.filter.DebuggingFilters.PrintResponse +//import okhttp3.OkHttpClient +//import org.http4k.client.OkHttp +//import org.http4k.core.Method +//import org.http4k.core.Request +//import org.http4k.core.Status import java.time.Duration import java.util.concurrent.TimeUnit @@ -33,16 +40,20 @@ class PatrolAppServiceClient(address: String, port: Int, private val timeout: Lo request = request.body(requestBody) } - val okHttpClient = OkHttp( - OkHttpClient.Builder() - .connectTimeout(timeout, timeUnit) - .readTimeout(timeout, timeUnit) - .writeTimeout(timeout, timeUnit) - .callTimeout(timeout, timeUnit) - .build() - ) + val client = JavaHttpClient() - val response = okHttpClient(request) + // val printingClient = PrintResponse().then(client) + +// val okHttpClient = OkHttp( +// OkHttpClient.Builder() +// .connectTimeout(timeout, timeUnit) +// .readTimeout(timeout, timeUnit) +// .writeTimeout(timeout, timeUnit) +// .callTimeout(timeout, timeUnit) +// .build() +// ) + + val response = client(request) if (response.status != Status.OK) { throw PatrolAppServiceClientException("Invalid response ${response.status}, ${response.bodyString()}") From 63d0357c13bbe80f30ccec0ffc8cc21d7a3fca59 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 18 Sep 2023 12:33:18 +0200 Subject: [PATCH 6/7] try to use Fuel - same problem with CLEARTEXT is thrown --- packages/patrol/android/build.gradle | 2 +- .../contracts/PatrolAppServiceClient.kt | 58 +++++++++---------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/packages/patrol/android/build.gradle b/packages/patrol/android/build.gradle index c3ad2da43..3529f2ca3 100644 --- a/packages/patrol/android/build.gradle +++ b/packages/patrol/android/build.gradle @@ -67,7 +67,7 @@ android { implementation platform("org.http4k:http4k-bom:5.7.4.0") implementation "org.http4k:http4k-core" - implementation "org.http4k:http4k-client-okhttp" + implementation "org.http4k:http4k-client-fuel" implementation "org.http4k:http4k-server-ktorcio" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-guava:1.5.2" diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt index 58f7de84c..ea9844b2e 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt @@ -5,21 +5,10 @@ package pl.leancode.patrol.contracts; +import com.github.kittinunf.fuel.httpPost +import com.github.kittinunf.result.Result import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json -import org.http4k.client.JavaHttpClient -import org.http4k.core.HttpHandler -import org.http4k.core.Method -import org.http4k.core.Request -import org.http4k.core.Status -import org.http4k.core.then -import org.http4k.filter.DebuggingFilters.PrintResponse -//import okhttp3.OkHttpClient -//import org.http4k.client.OkHttp -//import org.http4k.core.Method -//import org.http4k.core.Request -//import org.http4k.core.Status -import java.time.Duration import java.util.concurrent.TimeUnit class PatrolAppServiceClient(address: String, port: Int, private val timeout: Long, private val timeUnit: TimeUnit) { @@ -35,31 +24,38 @@ class PatrolAppServiceClient(address: String, port: Int, private val timeout: Lo } private fun performRequest(path: String, requestBody: String? = null): String { - var request = Request(Method.POST, "$serverUrl$path") - if (requestBody != null) { - request = request.body(requestBody) - } + val endpoint = "$serverUrl$path" + + val (_, response, result) = endpoint + .httpPost() + .also { + if (requestBody != null) { + it.body(requestBody) + } + } + .responseString() - val client = JavaHttpClient() + when (result) { + is Result.Failure -> { + val ex = result.getException() + println(ex) + } - // val printingClient = PrintResponse().then(client) + is Result.Success -> { + val data = result.get() + println(data) + } + } -// val okHttpClient = OkHttp( -// OkHttpClient.Builder() -// .connectTimeout(timeout, timeUnit) -// .readTimeout(timeout, timeUnit) -// .writeTimeout(timeout, timeUnit) -// .callTimeout(timeout, timeUnit) -// .build() -// ) + println("response: $response") - val response = client(request) + val bodyString = response.body().asString(null) - if (response.status != Status.OK) { - throw PatrolAppServiceClientException("Invalid response ${response.status}, ${response.bodyString()}") + if (response.statusCode != 200) { + throw PatrolAppServiceClientException("Invalid response ${response.statusCode}, $bodyString") } - return response.bodyString() + return bodyString } val serverUrl = "http://$address:$port/" From f33e09deedf096def0599c473eb8a3f390828930 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 18 Sep 2023 13:03:41 +0200 Subject: [PATCH 7/7] try the vanilla HttpURLConnection - turns out it's the same OkHttp under the hood! --- packages/patrol/android/build.gradle | 1 - .../leancode/patrol/PatrolAppServiceClient.kt | 4 +- .../contracts/PatrolAppServiceClient.kt | 73 +++++++++++-------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/packages/patrol/android/build.gradle b/packages/patrol/android/build.gradle index 3529f2ca3..6383dc1aa 100644 --- a/packages/patrol/android/build.gradle +++ b/packages/patrol/android/build.gradle @@ -67,7 +67,6 @@ android { implementation platform("org.http4k:http4k-bom:5.7.4.0") implementation "org.http4k:http4k-core" - implementation "org.http4k:http4k-client-fuel" implementation "org.http4k:http4k-server-ktorcio" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-guava:1.5.2" diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt index 7bdfa7bad..2f01dc873 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt @@ -17,12 +17,12 @@ class PatrolAppServiceClient { private val timeUnit = TimeUnit.HOURS constructor() { - client = Client(address = "localhost", port = 8082, timeout = timeout, timeUnit = timeUnit) + client = Client(address = "localhost", port = 8082) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } constructor(address: String) { - client = Client(address = address, port = 8082, timeout = timeout, timeUnit = timeUnit) + client = Client(address = address, port = 8082) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt index ea9844b2e..997dca6ab 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt @@ -5,13 +5,17 @@ package pl.leancode.patrol.contracts; -import com.github.kittinunf.fuel.httpPost -import com.github.kittinunf.result.Result import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json -import java.util.concurrent.TimeUnit +import java.io.BufferedReader +import java.io.IOException +import java.io.InputStreamReader +import java.net.HttpURLConnection +import java.net.URL +import java.nio.charset.StandardCharsets -class PatrolAppServiceClient(address: String, port: Int, private val timeout: Long, private val timeUnit: TimeUnit) { + +class PatrolAppServiceClient(private val address: String, private val port: Int) { fun listDartTests(): Contracts.ListDartTestsResponse { val response = performRequest("listDartTests") @@ -23,39 +27,50 @@ class PatrolAppServiceClient(address: String, port: Int, private val timeout: Lo return json.decodeFromString(response) } - private fun performRequest(path: String, requestBody: String? = null): String { + private fun performRequest(path: String, requestBody: String = ""): String { val endpoint = "$serverUrl$path" - val (_, response, result) = endpoint - .httpPost() - .also { - if (requestBody != null) { - it.body(requestBody) - } - } - .responseString() - - when (result) { - is Result.Failure -> { - val ex = result.getException() - println(ex) + var urlConnection: HttpURLConnection? = null + try { + val url = URL(endpoint) + urlConnection = url.openConnection() as HttpURLConnection + + // Set request properties + urlConnection.setRequestMethod("POST") + urlConnection.setRequestProperty("Content-Type", "application/json") + + // Create request body + val postData = requestBody.toByteArray(StandardCharsets.UTF_8) + val postDataLength = postData.size + urlConnection.setDoOutput(true) + urlConnection.instanceFollowRedirects = false + urlConnection.setRequestProperty("Content-Length", postDataLength.toString()) + urlConnection.outputStream.write(postData) + + // Read the response + val responseCode = urlConnection.getResponseCode() + if (responseCode != 200) { + throw PatrolAppServiceClientException("Invalid response $responseCode") } - is Result.Success -> { - val data = result.get() - println(data) + val reader = BufferedReader(InputStreamReader(urlConnection.inputStream)) + val response = StringBuilder() + var line: String? + while (reader.readLine().also { line = it } != null) { + response.append(line) } + reader.close() + val responseData = response.toString() + println("Response: $responseData") + return responseData + } catch (e: IOException) { + e.printStackTrace() + } finally { + urlConnection?.disconnect() } - println("response: $response") - - val bodyString = response.body().asString(null) - - if (response.statusCode != 200) { - throw PatrolAppServiceClientException("Invalid response ${response.statusCode}, $bodyString") - } - return bodyString + throw PatrolAppServiceClientException("something bad hapened!") } val serverUrl = "http://$address:$port/"