diff --git a/samples/client/petstore/kotlin-string/docs/PetApi.md b/samples/client/petstore/kotlin-string/docs/PetApi.md index 9c09efacf7d..ea93e174527 100644 --- a/samples/client/petstore/kotlin-string/docs/PetApi.md +++ b/samples/client/petstore/kotlin-string/docs/PetApi.md @@ -51,7 +51,9 @@ null (empty response body) ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -97,7 +99,9 @@ null (empty response body) ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -144,7 +148,9 @@ Name | Type | Description | Notes ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -191,7 +197,9 @@ Name | Type | Description | Notes ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -238,7 +246,10 @@ Name | Type | Description | Notes ### Authorization -[api_key](../README.md#api_key) + +Configure api_key: + ApiClient.apiKey["api_key"] = "" + ApiClient.apiKeyPrefix["api_key"] = "" ### HTTP request headers @@ -282,7 +293,9 @@ null (empty response body) ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -330,7 +343,9 @@ null (empty response body) ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -379,7 +394,9 @@ Name | Type | Description | Notes ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers diff --git a/samples/client/petstore/kotlin-string/docs/StoreApi.md b/samples/client/petstore/kotlin-string/docs/StoreApi.md index eed73ae300a..f4986041af8 100644 --- a/samples/client/petstore/kotlin-string/docs/StoreApi.md +++ b/samples/client/petstore/kotlin-string/docs/StoreApi.md @@ -92,7 +92,10 @@ This endpoint does not need any parameter. ### Authorization -[api_key](../README.md#api_key) + +Configure api_key: + ApiClient.apiKey["api_key"] = "" + ApiClient.apiKeyPrefix["api_key"] = "" ### HTTP request headers 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 75a49d0fa43..e6ebe7982db 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 @@ -15,11 +15,18 @@ open class ApiClient(val baseUrl: String) { companion object { protected const val ContentType = "Content-Type" protected const val Accept = "Accept" + protected const val Authorization = "Authorization" protected const val JsonMediaType = "application/json" protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + val apiKey: MutableMap = mutableMapOf() + val apiKeyPrefix: MutableMap = mutableMapOf() + var username: String? = null + var password: String? = null + var accessToken: String? = null + @JvmStatic val client: OkHttpClient by lazy { builder.build() @@ -65,9 +72,25 @@ open class ApiClient(val baseUrl: String) { } } + protected fun updateAuthParams(requestConfig: RequestConfig) { + if (requestConfig.headers["api_key"].isNullOrEmpty()) { + if (apiKeyPrefix["api_key"] != null) { + requestConfig.headers["api_key"] = apiKeyPrefix["api_key"] + " " + apiKey["api_key"] + } else { + requestConfig.headers["api_key"] = apiKey["api_key"] + } + } + if (requestConfig.headers[Authorization].isNullOrEmpty()) { + requestConfig.headers[Authorization] = "Bearer " + accessToken + } + } + protected inline fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.") + // take authMethod from operation + updateAuthParams(requestConfig) + val url = httpUrl.newBuilder() .addPathSegments(requestConfig.path.trimStart('/')) .apply { diff --git a/samples/client/petstore/kotlin-threetenbp/docs/PetApi.md b/samples/client/petstore/kotlin-threetenbp/docs/PetApi.md index 9c09efacf7d..ea93e174527 100644 --- a/samples/client/petstore/kotlin-threetenbp/docs/PetApi.md +++ b/samples/client/petstore/kotlin-threetenbp/docs/PetApi.md @@ -51,7 +51,9 @@ null (empty response body) ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -97,7 +99,9 @@ null (empty response body) ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -144,7 +148,9 @@ Name | Type | Description | Notes ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -191,7 +197,9 @@ Name | Type | Description | Notes ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -238,7 +246,10 @@ Name | Type | Description | Notes ### Authorization -[api_key](../README.md#api_key) + +Configure api_key: + ApiClient.apiKey["api_key"] = "" + ApiClient.apiKeyPrefix["api_key"] = "" ### HTTP request headers @@ -282,7 +293,9 @@ null (empty response body) ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -330,7 +343,9 @@ null (empty response body) ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers @@ -379,7 +394,9 @@ Name | Type | Description | Notes ### Authorization -[petstore_auth](../README.md#petstore_auth) + +Configure petstore_auth: + ApiClient.accessToken = "" ### HTTP request headers diff --git a/samples/client/petstore/kotlin-threetenbp/docs/StoreApi.md b/samples/client/petstore/kotlin-threetenbp/docs/StoreApi.md index eed73ae300a..f4986041af8 100644 --- a/samples/client/petstore/kotlin-threetenbp/docs/StoreApi.md +++ b/samples/client/petstore/kotlin-threetenbp/docs/StoreApi.md @@ -92,7 +92,10 @@ This endpoint does not need any parameter. ### Authorization -[api_key](../README.md#api_key) + +Configure api_key: + ApiClient.apiKey["api_key"] = "" + ApiClient.apiKeyPrefix["api_key"] = "" ### HTTP request headers 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 75a49d0fa43..e6ebe7982db 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 @@ -15,11 +15,18 @@ open class ApiClient(val baseUrl: String) { companion object { protected const val ContentType = "Content-Type" protected const val Accept = "Accept" + protected const val Authorization = "Authorization" protected const val JsonMediaType = "application/json" protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + val apiKey: MutableMap = mutableMapOf() + val apiKeyPrefix: MutableMap = mutableMapOf() + var username: String? = null + var password: String? = null + var accessToken: String? = null + @JvmStatic val client: OkHttpClient by lazy { builder.build() @@ -65,9 +72,25 @@ open class ApiClient(val baseUrl: String) { } } + protected fun updateAuthParams(requestConfig: RequestConfig) { + if (requestConfig.headers["api_key"].isNullOrEmpty()) { + if (apiKeyPrefix["api_key"] != null) { + requestConfig.headers["api_key"] = apiKeyPrefix["api_key"] + " " + apiKey["api_key"] + } else { + requestConfig.headers["api_key"] = apiKey["api_key"] + } + } + if (requestConfig.headers[Authorization].isNullOrEmpty()) { + requestConfig.headers[Authorization] = "Bearer " + accessToken + } + } + protected inline fun request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse { val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.") + // take authMethod from operation + updateAuthParams(requestConfig) + val url = httpUrl.newBuilder() .addPathSegments(requestConfig.path.trimStart('/')) .apply {