diff --git a/packages/patrol/android/build.gradle b/packages/patrol/android/build.gradle index d7feddf65..579274215 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-apache" + implementation "com.squareup.okhttp:okhttp:2.7.5" // See https://github.com/square/okhttp/issues/8031 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..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 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, timeout = timeout, timeUnit = timeUnit) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } constructor(address: String) { - client = Client(address = address, port = 8082, timeout = timeout) + 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 f1a9b8c7a..5f39bd12e 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,55 +5,57 @@ package pl.leancode.patrol.contracts; +import com.squareup.okhttp.MediaType +import com.squareup.okhttp.OkHttpClient +import com.squareup.okhttp.Request +import com.squareup.okhttp.RequestBody 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.util.concurrent.TimeUnit -class PatrolAppServiceClient(private val address: String, private val port: Int, private val timeout: Timeout) { +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) } 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 client = OkHttpClient().apply { + setConnectTimeout(timeout, timeUnit) + setReadTimeout(timeout, timeUnit) + setWriteTimeout(timeout, timeUnit) } - 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()}") + val request = Request.Builder() + .url(endpoint) + .also { + if (requestBody != null) { + it.post(RequestBody.create(jsonMediaType, requestBody)) + } + } + .build() + + val response = client.newCall(request).execute() + if (response.code() != 200) { + throw PatrolAppServiceClientException("Invalid response ${response.code()}, ${response?.body()?.string()}") } - return response.bodyString() + return response.body().string() } val serverUrl = "http://$address:$port/" private val json = Json { ignoreUnknownKeys = true } + + private val jsonMediaType = MediaType.parse("application/json; charset=utf-8") } class PatrolAppServiceClientException(message: String) : Exception(message) diff --git a/packages/patrol/example/android/app/build.gradle b/packages/patrol/example/android/app/build.gradle index af8d1fd21..cb9dd3f5c 100644 --- a/packages/patrol/example/android/app/build.gradle +++ b/packages/patrol/example/android/app/build.gradle @@ -32,11 +32,6 @@ android { kotlinOptions { jvmTarget = "1.8" } - - // TODO: Find workaround - packagingOptions { - pickFirst "META-INF/DEPENDENCIES" - } sourceSets { main.java.srcDirs += "src/main/kotlin" diff --git a/packages/patrol_gen/lib/src/generators/android/android_http4k_client_generator.dart b/packages/patrol_gen/lib/src/generators/android/android_http4k_client_generator.dart index 0773c0daa..2ecf09a90 100644 --- a/packages/patrol_gen/lib/src/generators/android/android_http4k_client_generator.dart +++ b/packages/patrol_gen/lib/src/generators/android/android_http4k_client_generator.dart @@ -25,15 +25,13 @@ class AndroidHttp4kClientGenerator { package ${config.package}; +import com.squareup.okhttp.MediaType +import com.squareup.okhttp.OkHttpClient +import com.squareup.okhttp.Request +import com.squareup.okhttp.RequestBody 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.util.concurrent.TimeUnit '''; } @@ -42,42 +40,46 @@ import org.http4k.core.Status const url = r'"http://$address:$port/"'; final endpoints = service.endpoints.map(_createEndpoint).join('\n\n'); const throwException = - r'throw PatrolAppServiceClientException("Invalid response ${response.status}, ${response.bodyString()}")'; + r'throw PatrolAppServiceClientException("Invalid response ${response.code()}, ${response?.body()?.string()}")'; const urlWithPath = r'"$serverUrl$path"'; return ''' -class ${service.name}Client(private val address: String, private val port: Int, private val timeout: Timeout) { +class ${service.name}Client(address: String, port: Int, private val timeout: Long, private val timeUnit: TimeUnit) { $endpoints private fun performRequest(path: String, requestBody: String? = null): String { - var request = Request(Method.POST, $urlWithPath) - if (requestBody != null) { - request = request.body(requestBody) + val endpoint = $urlWithPath + + val client = OkHttpClient().apply { + setConnectTimeout(timeout, timeUnit) + setReadTimeout(timeout, timeUnit) + setWriteTimeout(timeout, timeUnit) } - 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) { + val request = Request.Builder() + .url(endpoint) + .also { + if (requestBody != null) { + it.post(RequestBody.create(jsonMediaType, requestBody)) + } + } + .build() + + val response = client.newCall(request).execute() + if (response.code() != 200) { $throwException } - return response.bodyString() + return response.body().string() } val serverUrl = $url private val json = Json { ignoreUnknownKeys = true } + + private val jsonMediaType = MediaType.parse("application/json; charset=utf-8") }'''; } @@ -100,7 +102,7 @@ $endpoints return performRequest("${endpoint.name}"$serializeParameter)'''; return ''' - fun ${endpoint.name}($parameterDef) $returnDef { + fun ${endpoint.name}($parameterDef)$returnDef { $body }'''; }