From 74edcf66cb342a6cb3f740b2b38ace46e29dc552 Mon Sep 17 00:00:00 2001 From: Tatiana Gerth <tatiana.gerth@ergon.ch> Date: Mon, 20 Jul 2020 15:38:02 +0200 Subject: [PATCH 1/2] added okHttpClient as parameter to the constructor, adapted createService --- .../jvm-retrofit2/infrastructure/ApiClient.kt.mustache | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache index 6c20b54ccf6..64ce16e2ebe 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache @@ -45,7 +45,8 @@ import retrofit2.converter.moshi.MoshiConverterFactory {{#nonPublicApi}}internal {{/nonPublicApi}}class ApiClient( private var baseUrl: String = defaultBasePath, private val okHttpClientBuilder: OkHttpClient.Builder? = null, - private val serializerBuilder: {{#gson}}Gson{{/gson}}{{#moshi}}Moshi.{{/moshi}}Builder = Serializer.{{#gson}}gson{{/gson}}{{#moshi}}moshi{{/moshi}}Builder + private val serializerBuilder: {{#gson}}Gson{{/gson}}{{#moshi}}Moshi.{{/moshi}}Builder = Serializer.{{#gson}}gson{{/gson}}{{#moshi}}moshi{{/moshi}}Builder, + private val okHttpClient : OkHttpClient? = null ) { private val apiAuthorizations = mutableMapOf<String, Interceptor>() var logger: ((String) -> Unit)? = null @@ -281,7 +282,9 @@ import retrofit2.converter.moshi.MoshiConverterFactory } fun <S> createService(serviceClass: Class<S>): S { - return retrofitBuilder.client(clientBuilder.build()).build().create(serviceClass) + var usedClient: OkHttpClient? = null + this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()} + return retrofitBuilder.client(usedClient).build().create(serviceClass) } private fun normalizeBaseUrl() { -- GitLab From a8ba5015e3eb1566028c625166b57c76a5808dd1 Mon Sep 17 00:00:00 2001 From: Tatiana Gerth <tatiana.gerth@ergon.ch> Date: Mon, 20 Jul 2020 15:39:01 +0200 Subject: [PATCH 2/2] updated sample --- .../org/openapitools/client/infrastructure/ApiClient.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 5c4022ea3f2..8f822585a6e 100644 --- a/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -18,7 +18,8 @@ import retrofit2.converter.moshi.MoshiConverterFactory class ApiClient( private var baseUrl: String = defaultBasePath, private val okHttpClientBuilder: OkHttpClient.Builder? = null, - private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder + private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder, + private val okHttpClient : OkHttpClient? = null ) { private val apiAuthorizations = mutableMapOf<String, Interceptor>() var logger: ((String) -> Unit)? = null @@ -171,7 +172,9 @@ class ApiClient( } fun <S> createService(serviceClass: Class<S>): S { - return retrofitBuilder.client(clientBuilder.build()).build().create(serviceClass) + var usedClient: OkHttpClient? = null + this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()} + return retrofitBuilder.client(usedClient).build().create(serviceClass) } private fun normalizeBaseUrl() { -- GitLab