diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache index 742a69012ed..84fbd169c09 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache @@ -26,9 +26,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection {{^threetenbp}} import java.time.LocalDate @@ -39,6 +43,11 @@ import java.time.OffsetTime {{/threetenbp}} import java.util.Date import java.util.Locale +{{#useCoroutines}} +import kotlin.coroutines.resume +import kotlin.coroutines.resumeWithException +import kotlinx.coroutines.suspendCancellableCoroutine +{{/useCoroutines}} {{#kotlinx_serialization}} import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString @@ -271,7 +280,7 @@ import com.squareup.moshi.adapter } {{/hasAuthMethods}} - protected inline fun request(requestConfig: RequestConfig): ApiInfrastructureResponse { + protected {{#useCoroutines}}suspend {{/useCoroutines}}inline fun request(requestConfig: RequestConfig): ApiInfrastructureResponse { {{#jvm-okhttp3}} val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.") {{/jvm-okhttp3}} @@ -326,7 +335,24 @@ import com.squareup.moshi.adapter headers.forEach { header -> addHeader(header.key, header.value) } }.build() + {{#useCoroutines}} + val response: Response = suspendCancellableCoroutine { continuation -> + val call = client.newCall(request) + continuation.invokeOnCancellation { call.cancel() } + call.enqueue(object : Callback { + override fun onFailure(c: Call, e: IOException) { + continuation.resumeWithException(e) + } + override fun onResponse(c: Call, response: Response) { + continuation.resume(response) + } + }) + } + {{/useCoroutines}} + {{^useCoroutines}} val response = client.newCall(request).execute() + {{/useCoroutines}} + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 9d8cfd8c184..2deeb3eaeb6 100644 --- a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -196,6 +200,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 5db80e34f81..6910a1d58de 100644 --- a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -196,6 +200,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 2dfe0159d45..3223314734f 100644 --- a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -12,9 +12,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -203,6 +207,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 9d8cfd8c184..d67840dcc7e 100644 --- a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -22,6 +26,9 @@ import java.time.OffsetDateTime import java.time.OffsetTime import java.util.Date import java.util.Locale +import kotlin.coroutines.resume +import kotlin.coroutines.resumeWithException +import kotlinx.coroutines.suspendCancellableCoroutine import com.google.gson.reflect.TypeToken open class ApiClient(val baseUrl: String) { @@ -147,7 +154,7 @@ open class ApiClient(val baseUrl: String) { } } - protected inline fun request(requestConfig: RequestConfig): ApiInfrastructureResponse { + protected suspend inline fun request(requestConfig: RequestConfig): ApiInfrastructureResponse { val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.") // take authMethod from operation @@ -195,7 +202,19 @@ open class ApiClient(val baseUrl: String) { headers.forEach { header -> addHeader(header.key, header.value) } }.build() - val response = client.newCall(request).execute() + val response: Response = suspendCancellableCoroutine { continuation -> + val call = client.newCall(request) + continuation.invokeOnCancellation { call.cancel() } + call.enqueue(object : Callback { + override fun onFailure(c: Call, e: IOException) { + continuation.resumeWithException(e) + } + override fun onResponse(c: Call, response: Response) { + continuation.resume(response) + } + }) + } + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index bc8a5963fb6..58c8ac294a5 100644 --- a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -197,6 +201,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 924fb6cd4c8..24d224a6909 100644 --- a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -197,6 +201,7 @@ internal open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index bc8a5963fb6..58c8ac294a5 100644 --- a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -197,6 +201,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index b591a6d5e50..1d0ed1043bd 100644 --- a/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -9,9 +9,13 @@ import okhttp3.ResponseBody import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -195,6 +199,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index bc8a5963fb6..58c8ac294a5 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -197,6 +201,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index a7441f81d1b..14ce7181d91 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.util.Date import java.util.Locale @@ -197,6 +201,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index b359be1e41d..3791101dfe1 100644 --- a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -178,6 +182,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map> diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index bc8a5963fb6..58c8ac294a5 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -11,9 +11,13 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request import okhttp3.Headers import okhttp3.MultipartBody +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response import java.io.BufferedWriter import java.io.File import java.io.FileWriter +import java.io.IOException import java.net.URLConnection import java.time.LocalDate import java.time.LocalDateTime @@ -197,6 +201,7 @@ open class ApiClient(val baseUrl: String) { }.build() val response = client.newCall(request).execute() + val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault()) // TODO: handle specific mapping types. e.g. Map>