Skip to content

Commit

Permalink
[Kotlin][#7925] Make ApiClient in jvm-retrofit2 be able to add additi…
Browse files Browse the repository at this point in the history
…onal retrofit Converter.Factory (#9316)

* [Kotlin][#7925] Add an optional Converter.Factory to the Kotlin retrofit2 ApiClient template.

* [Kotlin][#7925] Update sample project.
  • Loading branch information
shanselm-ergon authored Apr 22, 2021
1 parent 05f3299 commit 5468b22
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.converter.scalars.ScalarsConverterFactory
{{#useRxJava}}
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
Expand Down Expand Up @@ -55,7 +56,8 @@ import okhttp3.MediaType.Companion.toMediaType
private var baseUrl: String = defaultBasePath,
private val okHttpClientBuilder: OkHttpClient.Builder? = null{{^kotlinx_serialization}},
private val serializerBuilder: {{#gson}}Gson{{/gson}}{{#moshi}}Moshi.{{/moshi}}Builder = Serializer.{{#gson}}gson{{/gson}}{{#moshi}}moshi{{/moshi}}Builder{{/kotlinx_serialization}},
private val okHttpClient : OkHttpClient? = null
private val okHttpClient : OkHttpClient? = null,
private val converterFactory: Converter.Factory? = null,
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
Expand All @@ -81,6 +83,11 @@ import okhttp3.MediaType.Companion.toMediaType
{{#kotlinx_serialization}}
.addConverterFactory(jvmJson.asConverterFactory("application/json".toMediaType()))
{{/kotlinx_serialization}}
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
}
}
}

private val clientBuilder: OkHttpClient.Builder by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.converter.scalars.ScalarsConverterFactory

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
Expand All @@ -20,7 +21,8 @@ import okhttp3.MediaType.Companion.toMediaType
class ApiClient(
private var baseUrl: String = defaultBasePath,
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
private val okHttpClient : OkHttpClient? = null
private val okHttpClient : OkHttpClient? = null,
private val converterFactory: Converter.Factory? = null,
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
Expand All @@ -30,6 +32,11 @@ class ApiClient(
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(jvmJson.asConverterFactory("application/json".toMediaType()))
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
}
}
}

private val clientBuilder: OkHttpClient.Builder by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import com.squareup.moshi.Moshi
Expand All @@ -21,7 +22,8 @@ class ApiClient(
private var baseUrl: String = defaultBasePath,
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder,
private val okHttpClient : OkHttpClient? = null
private val okHttpClient : OkHttpClient? = null,
private val converterFactory: Converter.Factory? = null,
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
Expand All @@ -33,6 +35,11 @@ class ApiClient(

.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(serializerBuilder.build()))
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
}
}
}

private val clientBuilder: OkHttpClient.Builder by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.converter.scalars.ScalarsConverterFactory
import com.squareup.moshi.Moshi
import retrofit2.converter.moshi.MoshiConverterFactory
Expand All @@ -20,7 +21,8 @@ class ApiClient(
private var baseUrl: String = defaultBasePath,
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder,
private val okHttpClient : OkHttpClient? = null
private val okHttpClient : OkHttpClient? = null,
private val converterFactory: Converter.Factory? = null,
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
Expand All @@ -30,6 +32,11 @@ class ApiClient(
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(serializerBuilder.build()))
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
}
}
}

private val clientBuilder: OkHttpClient.Builder by lazy {
Expand Down

0 comments on commit 5468b22

Please sign in to comment.