Skip to content

Commit

Permalink
patrol_gen: update code generator for PatrolAppServiceClient
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Sep 19, 2023
1 parent c2f5988 commit daec9cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
''';
}
Expand All @@ -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")
}''';
}

Expand All @@ -100,7 +102,7 @@ $endpoints
return performRequest("${endpoint.name}"$serializeParameter)''';

return '''
fun ${endpoint.name}($parameterDef) $returnDef {
fun ${endpoint.name}($parameterDef)$returnDef {
$body
}''';
}
Expand Down

0 comments on commit daec9cb

Please sign in to comment.