[Kotlin-client] Authentication support (#3722)

* Handle and document authorization for kotlin client

* Regenerate clients and tests

* Remove testing files
This commit is contained in:
Armand Mégrot
2019-08-22 15:32:07 +02:00
committed by William Cheng
parent 62ca0c78ff
commit 4898ffa4e9
80 changed files with 1012 additions and 370 deletions

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 {