Added converterFactories property to ApiClient in jvm-retrofit2. (#15008)

* Added converterFactories property to ApiClient in jvm-retrofit2.

* [retrofit2] Supplement deprecate contents of converterFactory

* [retrofit2] Supplement deprecate contents of converterFactory

* [retrofit2] converterFactory removed.

* [retrofit2] sample update.

* [retrofit2] Type mismatch fix in Rx.
This commit is contained in:
Dylan Kwon
2023-03-23 00:06:04 +09:00
committed by GitHub
parent 5dc0b70081
commit b6d2e0d222
8 changed files with 95 additions and 87 deletions

View File

@@ -29,6 +29,7 @@ import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.CallAdapter
import retrofit2.converter.scalars.ScalarsConverterFactory
{{#useRxJava}}
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
@@ -60,7 +61,29 @@ import okhttp3.MediaType.Companion.toMediaType
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 callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
private val callAdapterFactories: List<CallAdapter.Factory> = listOf(
{{#useRxJava}}
RxJavaCallAdapterFactory.create(),
{{/useRxJava}}
{{#useRxJava2}}
RxJava2CallAdapterFactory.create(),
{{/useRxJava2}}
{{#useRxJava3}}
RxJava3CallAdapterFactory.create(),
{{/useRxJava3}}
),
private val converterFactories: List<Converter.Factory> = listOf(
ScalarsConverterFactory.create(),
{{#gson}}
GsonConverterFactory.create(serializerBuilder.create()),
{{/gson}}
{{#moshi}}
MoshiConverterFactory.create(serializerBuilder.build()),
{{/moshi}}
{{#kotlinx_serialization}}
kotlinxSerializationJson.asConverterFactory("application/json".toMediaType()),
{{/kotlinx_serialization}}
)
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
@@ -68,27 +91,14 @@ import okhttp3.MediaType.Companion.toMediaType
private val retrofitBuilder: Retrofit.Builder by lazy {
Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
{{#gson}}
.addConverterFactory(GsonConverterFactory.create(serializerBuilder.create()))
{{/gson}}
{{#useRxJava}}
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
{{/useRxJava}}
{{#useRxJava2}}
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
{{/useRxJava2}}{{#useRxJava3}}
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
{{/useRxJava3}}
{{#moshi}}
.addConverterFactory(MoshiConverterFactory.create(serializerBuilder.build()))
{{/moshi}}
{{#kotlinx_serialization}}
.addConverterFactory(kotlinxSerializationJson.asConverterFactory("application/json".toMediaType()))
{{/kotlinx_serialization}}
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
callAdapterFactories.forEach {
addCallAdapterFactory(it)
}
}
.apply {
converterFactories.forEach {
addConverterFactory(it)
}
}
}

View File

@@ -7,6 +7,7 @@ import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.CallAdapter
import retrofit2.converter.scalars.ScalarsConverterFactory
import com.squareup.moshi.Moshi
import retrofit2.converter.moshi.MoshiConverterFactory
@@ -17,7 +18,12 @@ class ApiClient(
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder,
private val callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
private val callAdapterFactories: List<CallAdapter.Factory> = listOf(
),
private val converterFactories: List<Converter.Factory> = listOf(
ScalarsConverterFactory.create(),
MoshiConverterFactory.create(serializerBuilder.build()),
)
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
@@ -25,11 +31,14 @@ class ApiClient(
private val retrofitBuilder: Retrofit.Builder by lazy {
Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(serializerBuilder.build()))
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
callAdapterFactories.forEach {
addCallAdapterFactory(it)
}
}
.apply {
converterFactories.forEach {
addConverterFactory(it)
}
}
}

View File

@@ -13,6 +13,7 @@ import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.CallAdapter
import retrofit2.converter.scalars.ScalarsConverterFactory
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
@@ -23,7 +24,12 @@ class ApiClient(
private var baseUrl: String = defaultBasePath,
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
private val callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
private val callAdapterFactories: List<CallAdapter.Factory> = listOf(
),
private val converterFactories: List<Converter.Factory> = listOf(
ScalarsConverterFactory.create(),
kotlinxSerializationJson.asConverterFactory("application/json".toMediaType()),
)
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
@@ -31,11 +37,14 @@ class ApiClient(
private val retrofitBuilder: Retrofit.Builder by lazy {
Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(kotlinxSerializationJson.asConverterFactory("application/json".toMediaType()))
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
callAdapterFactories.forEach {
addCallAdapterFactory(it)
}
}
.apply {
converterFactories.forEach {
addConverterFactory(it)
}
}
}

View File

@@ -13,6 +13,7 @@ import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.CallAdapter
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import com.squareup.moshi.Moshi
@@ -24,7 +25,13 @@ class ApiClient(
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder,
private val callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
private val callAdapterFactories: List<CallAdapter.Factory> = listOf(
RxJava3CallAdapterFactory.create(),
),
private val converterFactories: List<Converter.Factory> = listOf(
ScalarsConverterFactory.create(),
MoshiConverterFactory.create(serializerBuilder.build()),
)
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
@@ -32,13 +39,14 @@ class ApiClient(
private val retrofitBuilder: Retrofit.Builder by lazy {
Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(serializerBuilder.build()))
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
callAdapterFactories.forEach {
addCallAdapterFactory(it)
}
}
.apply {
converterFactories.forEach {
addConverterFactory(it)
}
}
}

View File

@@ -13,6 +13,7 @@ import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.CallAdapter
import retrofit2.converter.scalars.ScalarsConverterFactory
import com.squareup.moshi.Moshi
import retrofit2.converter.moshi.MoshiConverterFactory
@@ -23,7 +24,12 @@ class ApiClient(
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder,
private val callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
private val callAdapterFactories: List<CallAdapter.Factory> = listOf(
),
private val converterFactories: List<Converter.Factory> = listOf(
ScalarsConverterFactory.create(),
MoshiConverterFactory.create(serializerBuilder.build()),
)
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
@@ -31,11 +37,14 @@ class ApiClient(
private val retrofitBuilder: Retrofit.Builder by lazy {
Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(serializerBuilder.build()))
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
callAdapterFactories.forEach {
addCallAdapterFactory(it)
}
}
.apply {
converterFactories.forEach {
addConverterFactory(it)
}
}
}

View File

@@ -5,14 +5,6 @@
//
import Foundation
// We reverted the change of PetstoreClientAPI to PetstoreClient introduced in https://github.com/OpenAPITools/openapi-generator/pull/9624
// Because it was causing the following issue https://github.com/OpenAPITools/openapi-generator/issues/9953
// If you are affected by this issue, please consider removing the following two lines,
// By setting the option removeMigrationProjectNameClass to true in the generator
@available(*, deprecated, renamed: "PetstoreClientAPI")
public typealias PetstoreClient = PetstoreClientAPI
open class PetstoreClientAPI {
public static var basePath = "http://localhost"
public static var customHeaders: [String: String] = [:]
@@ -31,8 +23,6 @@ open class RequestBuilder<T> {
public let requiresAuthentication: Bool
/// Optional block to obtain a reference to the request's progress instance when available.
/// With the URLSession http client the request's progress only works on iOS 11.0, macOS 10.13, macCatalyst 13.0, tvOS 11.0, watchOS 4.0.
/// If you need to get the request's progress in older OS versions, please use Alamofire http client.
public var onProgressReady: ((Progress) -> Void)?
required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool) {

View File

@@ -8,10 +8,6 @@ import Foundation
open class Configuration {
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.

View File

@@ -52,7 +52,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
- intercept and handle errors like authorization
- retry the request.
*/
@available(*, deprecated, message: "Please override execute() method to intercept and handle errors like authorization or retry the request. Check the Wiki for more info. https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-do-i-implement-bearer-token-authentication-with-urlsession-on-the-swift-api-client")
@available(*, unavailable, message: "Please override execute() method to intercept and handle errors like authorization or retry the request. Check the Wiki for more info. https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-do-i-implement-bearer-token-authentication-with-urlsession-on-the-swift-api-client")
public var taskCompletionShouldRetry: ((Data?, URLResponse?, Error?, @escaping (Bool) -> Void) -> Void)?
required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool) {
@@ -92,10 +92,6 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
originalRequest.httpMethod = method.rawValue
headers.forEach { key, value in
originalRequest.setValue(value, forHTTPHeaderField: key)
}
buildHeaders().forEach { key, value in
originalRequest.setValue(value, forHTTPHeaderField: key)
}
@@ -145,32 +141,13 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
if let taskCompletionShouldRetry = self.taskCompletionShouldRetry {
taskCompletionShouldRetry(data, response, error) { shouldRetry in
if shouldRetry {
cleanupRequest()
self.execute(apiResponseQueue, completion)
} else {
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()
}
}
}
} else {
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()
}
}
}
if #available(iOS 11.0, macOS 10.13, macCatalyst 13.0, tvOS 11.0, watchOS 4.0, *) {
onProgressReady?(dataTask.progress)
}
taskIdentifier = dataTask.taskIdentifier
challengeHandlerStore[dataTask.taskIdentifier] = taskDidReceiveChallenge
@@ -218,10 +195,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
open func buildHeaders() -> [String: String] {
var httpHeaders: [String: String] = [:]
for (key, value) in headers {
for (key, value) in PetstoreClientAPI.customHeaders {
httpHeaders[key] = value
}
for (key, value) in PetstoreClientAPI.customHeaders {
for (key, value) in headers {
httpHeaders[key] = value
}
return httpHeaders