[kotlin][jvm-retrofit2] Better code for OkHttp client passed to Retrofit builder (#7556)

* Better code for building Retrofit

By removing the nullable `usedClient` passed to Retrofit, the code is compatible with newer Retrofit versions which don't allow null as client.

* Updated Kotlin retrofit2 samples ApiClient
This commit is contained in:
Alex Facciorusso 2020-10-20 02:48:14 +01:00 committed by GitHub
parent b733312707
commit 1dec9ebfb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 9 deletions

View File

@ -287,8 +287,7 @@ import retrofit2.converter.moshi.MoshiConverterFactory
}
fun <S> createService(serviceClass: Class<S>): S {
var usedClient: OkHttpClient? = null
this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()}
val usedClient = this.okHttpClient ?: clientBuilder.build()
return retrofitBuilder.client(usedClient).build().create(serviceClass)
}
@ -313,4 +312,4 @@ import retrofit2.converter.moshi.MoshiConverterFactory
System.getProperties().getProperty("{{packageName}}.baseUrl", "{{{basePath}}}")
}
}
}
}

View File

@ -175,8 +175,7 @@ class ApiClient(
}
fun <S> createService(serviceClass: Class<S>): S {
var usedClient: OkHttpClient? = null
this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()}
val usedClient = this.okHttpClient ?: clientBuilder.build()
return retrofitBuilder.client(usedClient).build().create(serviceClass)
}
@ -201,4 +200,4 @@ class ApiClient(
System.getProperties().getProperty("org.openapitools.client.baseUrl", "http://petstore.swagger.io/v2")
}
}
}
}

View File

@ -172,8 +172,7 @@ class ApiClient(
}
fun <S> createService(serviceClass: Class<S>): S {
var usedClient: OkHttpClient? = null
this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()}
val usedClient = this.okHttpClient ?: clientBuilder.build()
return retrofitBuilder.client(usedClient).build().create(serviceClass)
}
@ -198,4 +197,4 @@ class ApiClient(
System.getProperties().getProperty("org.openapitools.client.baseUrl", "http://petstore.swagger.io/v2")
}
}
}
}