Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ import retrofit2.converter.moshi.MoshiConverterFactory
level = HttpLoggingInterceptor.Level.BODY
})
}

private var okHttpClient?: OkHttpClient = null

constructor(
baseUrl: String = defaultBasePath,
okHttpClientBuilder: OkHttpClient.Builder? = null,
serializerBuilder: {{#gson}}Gson{{/gson}}{{#moshi}}Moshi.{{/moshi}}Builder = Serializer.{{#gson}}gson{{/gson}}{{#moshi}}moshi{{/moshi}}Builder
okClient: OkHttpClient? = null
) : this(baseUrl, okHttpClientBuilder, serializerBuilder) {
this.okHttpClient = okClient
}

init {
normalizeBaseUrl()
Expand Down Expand Up @@ -281,7 +292,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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ class ApiClient(
level = HttpLoggingInterceptor.Level.BODY
})
}

private var okHttpClient?: OkHttpClient = null

constructor(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tgerth we don't need a new constructor, you can add the private val okHttpClient : OkHttpClient? = null to the constructor at line 18, because it has a default value.
And with this change, you can remove this constructor, and the var in line 49.

baseUrl: String = defaultBasePath,
okHttpClientBuilder: OkHttpClient.Builder? = null,
serializerBuilder: Moshi.Builder = Serializer.moshiBuilder
okClient: OkHttpClient? = null
) : this(baseUrl, okHttpClientBuilder, serializerBuilder) {
this.okHttpClient = okClient
}

init {
normalizeBaseUrl()
Expand Down Expand Up @@ -171,7 +182,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() {
Expand Down