update kotlin client sample

This commit is contained in:
William Cheng
2019-08-22 21:43:06 +08:00
parent 4898ffa4e9
commit 14a39349d5
6 changed files with 104 additions and 18 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = 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 <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
// take authMethod from operation
updateAuthParams(requestConfig)
val url = httpUrl.newBuilder()
.addPathSegments(requestConfig.path.trimStart('/'))
.apply {