forked from loafle/openapi-generator-original
[Kotlin][#7925] Make ApiClient in jvm-retrofit2 be able to add additional retrofit Converter.Factory (#9316)
* [Kotlin][#7925] Add an optional Converter.Factory to the Kotlin retrofit2 ApiClient template. * [Kotlin][#7925] Update sample project.
This commit is contained in:
parent
05f329959c
commit
5468b22b83
@ -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
|
||||
@ -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
|
||||
@ -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 {
|
||||
|
@ -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
|
||||
@ -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
|
||||
@ -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 {
|
||||
|
@ -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
|
||||
@ -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
|
||||
@ -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 {
|
||||
|
@ -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
|
||||
@ -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
|
||||
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user