Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Kotlin] Make ApiClient in jvm-retrofit2 be able to use own OkHttpClient #6999

Merged
merged 2 commits into from
Aug 8, 2020
Merged
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 @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down