forked from loafle/openapi-generator-original
update kotlin client sample
This commit is contained in:
@@ -51,7 +51,9 @@ null (empty response body)
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -97,7 +99,9 @@ null (empty response body)
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -144,7 +148,9 @@ Name | Type | Description | Notes
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -191,7 +197,9 @@ Name | Type | Description | Notes
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -238,7 +246,10 @@ Name | Type | Description | Notes
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[api_key](../README.md#api_key)
|
|
||||||
|
Configure api_key:
|
||||||
|
ApiClient.apiKey["api_key"] = ""
|
||||||
|
ApiClient.apiKeyPrefix["api_key"] = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -282,7 +293,9 @@ null (empty response body)
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -330,7 +343,9 @@ null (empty response body)
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -379,7 +394,9 @@ Name | Type | Description | Notes
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
|
|||||||
@@ -92,7 +92,10 @@ This endpoint does not need any parameter.
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[api_key](../README.md#api_key)
|
|
||||||
|
Configure api_key:
|
||||||
|
ApiClient.apiKey["api_key"] = ""
|
||||||
|
ApiClient.apiKeyPrefix["api_key"] = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
|
|||||||
@@ -15,11 +15,18 @@ open class ApiClient(val baseUrl: String) {
|
|||||||
companion object {
|
companion object {
|
||||||
protected const val ContentType = "Content-Type"
|
protected const val ContentType = "Content-Type"
|
||||||
protected const val Accept = "Accept"
|
protected const val Accept = "Accept"
|
||||||
|
protected const val Authorization = "Authorization"
|
||||||
protected const val JsonMediaType = "application/json"
|
protected const val JsonMediaType = "application/json"
|
||||||
protected const val FormDataMediaType = "multipart/form-data"
|
protected const val FormDataMediaType = "multipart/form-data"
|
||||||
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
|
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
|
||||||
protected const val XmlMediaType = "application/xml"
|
protected const val XmlMediaType = "application/xml"
|
||||||
|
|
||||||
|
val apiKey: MutableMap<String, String> = mutableMapOf()
|
||||||
|
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
|
||||||
|
var username: String? = null
|
||||||
|
var password: String? = null
|
||||||
|
var accessToken: String? = null
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val client: OkHttpClient by lazy {
|
val client: OkHttpClient by lazy {
|
||||||
builder.build()
|
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 <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
|
protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
|
||||||
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
|
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
|
||||||
|
|
||||||
|
// take authMethod from operation
|
||||||
|
updateAuthParams(requestConfig)
|
||||||
|
|
||||||
val url = httpUrl.newBuilder()
|
val url = httpUrl.newBuilder()
|
||||||
.addPathSegments(requestConfig.path.trimStart('/'))
|
.addPathSegments(requestConfig.path.trimStart('/'))
|
||||||
.apply {
|
.apply {
|
||||||
|
|||||||
@@ -51,7 +51,9 @@ null (empty response body)
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -97,7 +99,9 @@ null (empty response body)
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -144,7 +148,9 @@ Name | Type | Description | Notes
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -191,7 +197,9 @@ Name | Type | Description | Notes
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -238,7 +246,10 @@ Name | Type | Description | Notes
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[api_key](../README.md#api_key)
|
|
||||||
|
Configure api_key:
|
||||||
|
ApiClient.apiKey["api_key"] = ""
|
||||||
|
ApiClient.apiKeyPrefix["api_key"] = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -282,7 +293,9 @@ null (empty response body)
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -330,7 +343,9 @@ null (empty response body)
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
@@ -379,7 +394,9 @@ Name | Type | Description | Notes
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[petstore_auth](../README.md#petstore_auth)
|
|
||||||
|
Configure petstore_auth:
|
||||||
|
ApiClient.accessToken = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
|
|||||||
@@ -92,7 +92,10 @@ This endpoint does not need any parameter.
|
|||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
[api_key](../README.md#api_key)
|
|
||||||
|
Configure api_key:
|
||||||
|
ApiClient.apiKey["api_key"] = ""
|
||||||
|
ApiClient.apiKeyPrefix["api_key"] = ""
|
||||||
|
|
||||||
### HTTP request headers
|
### HTTP request headers
|
||||||
|
|
||||||
|
|||||||
@@ -15,11 +15,18 @@ open class ApiClient(val baseUrl: String) {
|
|||||||
companion object {
|
companion object {
|
||||||
protected const val ContentType = "Content-Type"
|
protected const val ContentType = "Content-Type"
|
||||||
protected const val Accept = "Accept"
|
protected const val Accept = "Accept"
|
||||||
|
protected const val Authorization = "Authorization"
|
||||||
protected const val JsonMediaType = "application/json"
|
protected const val JsonMediaType = "application/json"
|
||||||
protected const val FormDataMediaType = "multipart/form-data"
|
protected const val FormDataMediaType = "multipart/form-data"
|
||||||
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
|
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
|
||||||
protected const val XmlMediaType = "application/xml"
|
protected const val XmlMediaType = "application/xml"
|
||||||
|
|
||||||
|
val apiKey: MutableMap<String, String> = mutableMapOf()
|
||||||
|
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
|
||||||
|
var username: String? = null
|
||||||
|
var password: String? = null
|
||||||
|
var accessToken: String? = null
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val client: OkHttpClient by lazy {
|
val client: OkHttpClient by lazy {
|
||||||
builder.build()
|
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 <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
|
protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
|
||||||
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
|
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
|
||||||
|
|
||||||
|
// take authMethod from operation
|
||||||
|
updateAuthParams(requestConfig)
|
||||||
|
|
||||||
val url = httpUrl.newBuilder()
|
val url = httpUrl.newBuilder()
|
||||||
.addPathSegments(requestConfig.path.trimStart('/'))
|
.addPathSegments(requestConfig.path.trimStart('/'))
|
||||||
.apply {
|
.apply {
|
||||||
|
|||||||
Reference in New Issue
Block a user