Skip to content

Commit d78e915

Browse files
authored
[Kotlin] Make ApiClient in jvm-retrofit2 be able to use own OkHttpClient (#6999)
* added okHttpClient as parameter to the constructor, adapted createService * updated sample
1 parent 2a17625 commit d78e915

File tree

2 files changed

+10
-4
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure
  • samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure

2 files changed

+10
-4
lines changed

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ import retrofit2.converter.moshi.MoshiConverterFactory
4848
{{#nonPublicApi}}internal {{/nonPublicApi}}class ApiClient(
4949
private var baseUrl: String = defaultBasePath,
5050
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
51-
private val serializerBuilder: {{#gson}}Gson{{/gson}}{{#moshi}}Moshi.{{/moshi}}Builder = Serializer.{{#gson}}gson{{/gson}}{{#moshi}}moshi{{/moshi}}Builder
51+
private val serializerBuilder: {{#gson}}Gson{{/gson}}{{#moshi}}Moshi.{{/moshi}}Builder = Serializer.{{#gson}}gson{{/gson}}{{#moshi}}moshi{{/moshi}}Builder,
52+
private val okHttpClient : OkHttpClient? = null
5253
) {
5354
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
5455
var logger: ((String) -> Unit)? = null
@@ -286,7 +287,9 @@ import retrofit2.converter.moshi.MoshiConverterFactory
286287
}
287288

288289
fun <S> createService(serviceClass: Class<S>): S {
289-
return retrofitBuilder.client(clientBuilder.build()).build().create(serviceClass)
290+
var usedClient: OkHttpClient? = null
291+
this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()}
292+
return retrofitBuilder.client(usedClient).build().create(serviceClass)
290293
}
291294

292295
private fun normalizeBaseUrl() {

samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import retrofit2.converter.moshi.MoshiConverterFactory
1818
class ApiClient(
1919
private var baseUrl: String = defaultBasePath,
2020
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
21-
private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder
21+
private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder,
22+
private val okHttpClient : OkHttpClient? = null
2223
) {
2324
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
2425
var logger: ((String) -> Unit)? = null
@@ -171,7 +172,9 @@ class ApiClient(
171172
}
172173

173174
fun <S> createService(serviceClass: Class<S>): S {
174-
return retrofitBuilder.client(clientBuilder.build()).build().create(serviceClass)
175+
var usedClient: OkHttpClient? = null
176+
this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()}
177+
return retrofitBuilder.client(usedClient).build().create(serviceClass)
175178
}
176179

177180
private fun normalizeBaseUrl() {

0 commit comments

Comments
 (0)