Skip to content

Commit

Permalink
[kotlin][client] define kotlin coroutines context (#9936)
Browse files Browse the repository at this point in the history
* [kotlin][client] define coroutines context

* [kotlin][client] update sample projects

* [kotlin][client] add missing kotlinx coroutines dependency

* [kotlin][client] update sample projects
  • Loading branch information
4brunu authored Jul 13, 2021
1 parent 7573234 commit 26f998b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ test {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
{{^doNotUseRxAndCoroutines}}
{{#useCoroutines}}
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1"
{{/useCoroutines}}
{{/doNotUseRxAndCoroutines}}
{{#moshi}}
{{^moshiCodeGen}}
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ package {{apiPackage}}
{{#imports}}import {{import}}
{{/imports}}

{{^doNotUseRxAndCoroutines}}
{{#useCoroutines}}
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
{{/useCoroutines}}
{{/doNotUseRxAndCoroutines}}
import {{packageName}}.infrastructure.ApiClient
import {{packageName}}.infrastructure.ClientException
import {{packageName}}.infrastructure.ClientError
Expand Down Expand Up @@ -40,7 +46,7 @@ import {{packageName}}.infrastructure.toMultiValue
{{#isDeprecated}}
@Deprecated(message = "This operation is deprecated.")
{{/isDeprecated}}
{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}suspend {{/useCoroutines}}{{/doNotUseRxAndCoroutines}}fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) : {{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}} {
{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}suspend {{/useCoroutines}}{{/doNotUseRxAndCoroutines}}fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) : {{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{^doNotUseRxAndCoroutines}}{{#useCoroutines}} = withContext(Dispatchers.IO){{/useCoroutines}}{{/doNotUseRxAndCoroutines}} {
{{#isDeprecated}}
@Suppress("DEPRECATION")
{{/isDeprecated}}
Expand All @@ -50,7 +56,7 @@ import {{packageName}}.infrastructure.toMultiValue
localVariableConfig
)

return when (localVarResponse.responseType) {
return{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}@withContext{{/useCoroutines}}{{/doNotUseRxAndCoroutines}} when (localVarResponse.responseType) {
ResponseType.Success -> {{#returnType}}(localVarResponse as Success<*>).data as {{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1"
implementation "com.google.code.gson:gson:2.8.7"
implementation "com.squareup.okhttp3:okhttp:4.9.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ package org.openapitools.client.apis
import org.openapitools.client.models.ApiResponse
import org.openapitools.client.models.Pet

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.openapitools.client.infrastructure.ApiClient
import org.openapitools.client.infrastructure.ClientException
import org.openapitools.client.infrastructure.ClientError
Expand Down Expand Up @@ -44,14 +46,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
* @throws ServerException If the API returns a server error response
*/
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun addPet(body: Pet) : Unit {
suspend fun addPet(body: Pet) : Unit = withContext(Dispatchers.IO) {
val localVariableConfig = addPetRequestConfig(body = body)

val localVarResponse = request<Pet, Unit>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> Unit
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -97,14 +99,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
* @throws ServerException If the API returns a server error response
*/
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?) : Unit {
suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?) : Unit = withContext(Dispatchers.IO) {
val localVariableConfig = deletePetRequestConfig(petId = petId, apiKey = apiKey)

val localVarResponse = request<Unit, Unit>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> Unit
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -152,14 +154,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
*/
@Suppress("UNCHECKED_CAST")
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun findPetsByStatus(status: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> {
suspend fun findPetsByStatus(status: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> = withContext(Dispatchers.IO) {
val localVariableConfig = findPetsByStatusRequestConfig(status = status)

val localVarResponse = request<Unit, kotlin.collections.List<Pet>>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> (localVarResponse as Success<*>).data as kotlin.collections.List<Pet>
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -209,15 +211,15 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
@Suppress("UNCHECKED_CAST")
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
@Deprecated(message = "This operation is deprecated.")
suspend fun findPetsByTags(tags: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> {
suspend fun findPetsByTags(tags: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> = withContext(Dispatchers.IO) {
@Suppress("DEPRECATION")
val localVariableConfig = findPetsByTagsRequestConfig(tags = tags)

val localVarResponse = request<Unit, kotlin.collections.List<Pet>>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> (localVarResponse as Success<*>).data as kotlin.collections.List<Pet>
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -267,14 +269,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
*/
@Suppress("UNCHECKED_CAST")
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun getPetById(petId: kotlin.Long) : Pet {
suspend fun getPetById(petId: kotlin.Long) : Pet = withContext(Dispatchers.IO) {
val localVariableConfig = getPetByIdRequestConfig(petId = petId)

val localVarResponse = request<Unit, Pet>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> (localVarResponse as Success<*>).data as Pet
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -319,14 +321,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
* @throws ServerException If the API returns a server error response
*/
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun updatePet(body: Pet) : Unit {
suspend fun updatePet(body: Pet) : Unit = withContext(Dispatchers.IO) {
val localVariableConfig = updatePetRequestConfig(body = body)

val localVarResponse = request<Pet, Unit>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> Unit
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -373,14 +375,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
* @throws ServerException If the API returns a server error response
*/
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?) : Unit {
suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?) : Unit = withContext(Dispatchers.IO) {
val localVariableConfig = updatePetWithFormRequestConfig(petId = petId, name = name, status = status)

val localVarResponse = request<Map<String, Any?>, Unit>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> Unit
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -430,14 +432,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
*/
@Suppress("UNCHECKED_CAST")
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : ApiResponse {
suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : ApiResponse = withContext(Dispatchers.IO) {
val localVariableConfig = uploadFileRequestConfig(petId = petId, additionalMetadata = additionalMetadata, file = file)

val localVarResponse = request<Map<String, Any?>, ApiResponse>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> (localVarResponse as Success<*>).data as ApiResponse
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ package org.openapitools.client.apis

import org.openapitools.client.models.Order

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.openapitools.client.infrastructure.ApiClient
import org.openapitools.client.infrastructure.ClientException
import org.openapitools.client.infrastructure.ClientError
Expand Down Expand Up @@ -43,14 +45,14 @@ class StoreApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath)
* @throws ServerException If the API returns a server error response
*/
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun deleteOrder(orderId: kotlin.String) : Unit {
suspend fun deleteOrder(orderId: kotlin.String) : Unit = withContext(Dispatchers.IO) {
val localVariableConfig = deleteOrderRequestConfig(orderId = orderId)

val localVarResponse = request<Unit, Unit>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> Unit
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -95,14 +97,14 @@ class StoreApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath)
*/
@Suppress("UNCHECKED_CAST")
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun getInventory() : kotlin.collections.Map<kotlin.String, kotlin.Int> {
suspend fun getInventory() : kotlin.collections.Map<kotlin.String, kotlin.Int> = withContext(Dispatchers.IO) {
val localVariableConfig = getInventoryRequestConfig()

val localVarResponse = request<Unit, kotlin.collections.Map<kotlin.String, kotlin.Int>>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> (localVarResponse as Success<*>).data as kotlin.collections.Map<kotlin.String, kotlin.Int>
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -147,14 +149,14 @@ class StoreApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath)
*/
@Suppress("UNCHECKED_CAST")
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun getOrderById(orderId: kotlin.Long) : Order {
suspend fun getOrderById(orderId: kotlin.Long) : Order = withContext(Dispatchers.IO) {
val localVariableConfig = getOrderByIdRequestConfig(orderId = orderId)

val localVarResponse = request<Unit, Order>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> (localVarResponse as Success<*>).data as Order
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down Expand Up @@ -200,14 +202,14 @@ class StoreApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath)
*/
@Suppress("UNCHECKED_CAST")
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
suspend fun placeOrder(body: Order) : Order {
suspend fun placeOrder(body: Order) : Order = withContext(Dispatchers.IO) {
val localVariableConfig = placeOrderRequestConfig(body = body)

val localVarResponse = request<Order, Order>(
localVariableConfig
)

return when (localVarResponse.responseType) {
return@withContext when (localVarResponse.responseType) {
ResponseType.Success -> (localVarResponse as Success<*>).data as Order
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
Expand Down
Loading

0 comments on commit 26f998b

Please sign in to comment.