Skip to content

Commit

Permalink
[Kotlin][retrofit2] replace okhttpclient with callfactory (#9451)
Browse files Browse the repository at this point in the history
* [Kotlin][Retrofit][#9448] Replace OkHttpClient parameter with Call.Factory.

* [Kotlin][Retrofit][#9448] Update sample project.
  • Loading branch information
shanselm-ergon authored May 28, 2021
1 parent 763f65f commit 1b6fd2d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {{packageName}}.auth.HttpBearerAuth
{{/authMethods}}
{{/hasAuthMethods}}

import okhttp3.Call
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
Expand Down Expand Up @@ -56,7 +57,7 @@ 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 callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
Expand Down Expand Up @@ -303,8 +304,8 @@ import okhttp3.MediaType.Companion.toMediaType
}

fun <S> createService(serviceClass: Class<S>): S {
val usedClient = this.okHttpClient ?: clientBuilder.build()
return retrofitBuilder.client(usedClient).build().create(serviceClass)
val usedCallFactory = this.callFactory ?: clientBuilder.build()
return retrofitBuilder.callFactory(usedCallFactory).build().create(serviceClass)
}

private fun normalizeBaseUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.openapitools.client.auth.OAuth
import org.openapitools.client.auth.OAuth.AccessTokenListener
import org.openapitools.client.auth.OAuthFlow

import okhttp3.Call
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
Expand All @@ -21,7 +22,7 @@ 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 callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
Expand Down Expand Up @@ -180,8 +181,8 @@ class ApiClient(
}

fun <S> createService(serviceClass: Class<S>): S {
val usedClient = this.okHttpClient ?: clientBuilder.build()
return retrofitBuilder.client(usedClient).build().create(serviceClass)
val usedCallFactory = this.callFactory ?: clientBuilder.build()
return retrofitBuilder.callFactory(usedCallFactory).build().create(serviceClass)
}

private fun normalizeBaseUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.openapitools.client.auth.OAuth
import org.openapitools.client.auth.OAuth.AccessTokenListener
import org.openapitools.client.auth.OAuthFlow

import okhttp3.Call
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
Expand All @@ -22,7 +23,7 @@ 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 callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
Expand Down Expand Up @@ -183,8 +184,8 @@ class ApiClient(
}

fun <S> createService(serviceClass: Class<S>): S {
val usedClient = this.okHttpClient ?: clientBuilder.build()
return retrofitBuilder.client(usedClient).build().create(serviceClass)
val usedCallFactory = this.callFactory ?: clientBuilder.build()
return retrofitBuilder.callFactory(usedCallFactory).build().create(serviceClass)
}

private fun normalizeBaseUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.openapitools.client.auth.OAuth
import org.openapitools.client.auth.OAuth.AccessTokenListener
import org.openapitools.client.auth.OAuthFlow

import okhttp3.Call
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
Expand All @@ -21,7 +22,7 @@ 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 callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
Expand Down Expand Up @@ -180,8 +181,8 @@ class ApiClient(
}

fun <S> createService(serviceClass: Class<S>): S {
val usedClient = this.okHttpClient ?: clientBuilder.build()
return retrofitBuilder.client(usedClient).build().create(serviceClass)
val usedCallFactory = this.callFactory ?: clientBuilder.build()
return retrofitBuilder.callFactory(usedCallFactory).build().create(serviceClass)
}

private fun normalizeBaseUrl() {
Expand Down

0 comments on commit 1b6fd2d

Please sign in to comment.