diff --git a/packages/patrol/android/build.gradle b/packages/patrol/android/build.gradle index 9aa4b1894..6383dc1aa 100644 --- a/packages/patrol/android/build.gradle +++ b/packages/patrol/android/build.gradle @@ -66,9 +66,8 @@ 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-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..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 @@ -1,8 +1,8 @@ package pl.leancode.patrol -import org.apache.hc.core5.util.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, 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..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 @@ -7,48 +7,70 @@ 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 org.http4k.core.Method -import org.http4k.core.Request -import org.http4k.core.Status +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(private val address: String, private val port: Int, private val timeout: Timeout) { - fun listDartTests() : Contracts.ListDartTestsResponse { +class PatrolAppServiceClient(private val address: String, private val port: Int) { + + 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) } - private fun performRequest(path: String, requestBody: String? = null): String { - var request = Request(Method.POST, "$serverUrl$path") - if (requestBody != null) { - request = request.body(requestBody) - } + private fun performRequest(path: String, requestBody: String = ""): String { + val endpoint = "$serverUrl$path" + + 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") - val client = ApacheClient( - HttpClients.custom().setDefaultRequestConfig( - RequestConfig - .copy(RequestConfig.DEFAULT) - .setResponseTimeout(timeout) - .setConnectionRequestTimeout(timeout) - .build() - ).build()) - - val response = client(request) - - if (response.status != Status.OK) { - throw PatrolAppServiceClientException("Invalid response ${response.status}, ${response.bodyString()}") + // 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") + } + + 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() } - return response.bodyString() + + throw PatrolAppServiceClientException("something bad hapened!") } val serverUrl = "http://$address:$port/" diff --git a/packages/patrol/example/android/app/build.gradle b/packages/patrol/example/android/app/build.gradle index 55bba1cac..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"