forked from loafle/openapi-generator-original
Merge remote-tracking branch 'origin/4.2.x' into 5.0.x
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
## Requires
|
||||
|
||||
* Kotlin 1.3.31
|
||||
* Kotlin 1.3.41
|
||||
* Gradle 4.9
|
||||
|
||||
## Build
|
||||
|
||||
@@ -7,7 +7,7 @@ wrapper {
|
||||
}
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.3.31'
|
||||
ext.kotlin_version = '1.3.41'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@@ -32,6 +32,6 @@ dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
|
||||
compile "com.squareup.moshi:moshi-kotlin:1.8.0"
|
||||
compile "com.squareup.moshi:moshi-adapters:1.8.0"
|
||||
compile "com.squareup.okhttp3:okhttp:3.14.2"
|
||||
compile "com.squareup.okhttp3:okhttp:4.0.1"
|
||||
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.1.0"
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -51,8 +51,8 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -82,8 +82,8 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -113,8 +113,8 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> (response as Success<*>).data as kotlin.Array<Pet>
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -144,8 +144,8 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> (response as Success<*>).data as kotlin.Array<Pet>
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -175,8 +175,8 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> (response as Success<*>).data as Pet
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -205,8 +205,8 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -237,8 +237,8 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -270,8 +270,8 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> (response as Success<*>).data as ApiResponse
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
|
||||
@@ -50,8 +50,8 @@ class StoreApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiC
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -80,8 +80,8 @@ class StoreApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiC
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> (response as Success<*>).data as kotlin.collections.Map<kotlin.String, kotlin.Int>
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -111,8 +111,8 @@ class StoreApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiC
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> (response as Success<*>).data as Order
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -142,8 +142,8 @@ class StoreApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiC
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> (response as Success<*>).data as Order
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
|
||||
@@ -50,8 +50,8 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -80,8 +80,8 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -110,8 +110,8 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -140,8 +140,8 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -171,8 +171,8 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> (response as Success<*>).data as User
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -203,8 +203,8 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> (response as Success<*>).data as kotlin.String
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -232,8 +232,8 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
@@ -263,8 +263,8 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
|
||||
|
||||
return when (response.responseType) {
|
||||
ResponseType.Success -> Unit
|
||||
ResponseType.Informational -> TODO()
|
||||
ResponseType.Redirection -> TODO()
|
||||
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
|
||||
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
|
||||
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
|
||||
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@ package org.openapitools.client.infrastructure
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.FormBody
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import okhttp3.ResponseBody
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.Request
|
||||
import java.io.File
|
||||
|
||||
@@ -13,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()
|
||||
@@ -29,8 +38,8 @@ open class ApiClient(val baseUrl: String) {
|
||||
|
||||
protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
|
||||
when {
|
||||
content is File -> RequestBody.create(
|
||||
MediaType.parse(mediaType), content
|
||||
content is File -> content.asRequestBody(
|
||||
mediaType.toMediaTypeOrNull()
|
||||
)
|
||||
mediaType == FormDataMediaType || mediaType == FormUrlEncMediaType -> {
|
||||
FormBody.Builder().apply {
|
||||
@@ -41,12 +50,12 @@ open class ApiClient(val baseUrl: String) {
|
||||
}
|
||||
}.build()
|
||||
}
|
||||
mediaType == JsonMediaType -> RequestBody.create(
|
||||
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
|
||||
mediaType == JsonMediaType -> Serializer.moshi.adapter(T::class.java).toJson(content).toRequestBody(
|
||||
mediaType.toMediaTypeOrNull()
|
||||
)
|
||||
mediaType == XmlMediaType -> TODO("xml not currently supported.")
|
||||
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
|
||||
// TODO: this should be extended with other serializers
|
||||
else -> TODO("requestBody currently only supports JSON body and File body.")
|
||||
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
|
||||
}
|
||||
|
||||
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
|
||||
@@ -59,12 +68,30 @@ open class ApiClient(val baseUrl: String) {
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> TODO("responseBody currently only supports JSON body.")
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
}
|
||||
}
|
||||
|
||||
protected fun updateAuthParams(requestConfig: RequestConfig) {
|
||||
if (requestConfig.headers["api_key"].isNullOrEmpty()) {
|
||||
if (apiKey["api_key"] != null) {
|
||||
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 = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
|
||||
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
|
||||
|
||||
// take authMethod from operation
|
||||
updateAuthParams(requestConfig)
|
||||
|
||||
val url = httpUrl.newBuilder()
|
||||
.addPathSegments(requestConfig.path.trimStart('/'))
|
||||
@@ -114,29 +141,29 @@ open class ApiClient(val baseUrl: String) {
|
||||
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
|
||||
when {
|
||||
response.isRedirect -> return Redirection(
|
||||
response.code(),
|
||||
response.headers().toMultimap()
|
||||
response.code,
|
||||
response.headers.toMultimap()
|
||||
)
|
||||
response.isInformational -> return Informational(
|
||||
response.message(),
|
||||
response.code(),
|
||||
response.headers().toMultimap()
|
||||
response.message,
|
||||
response.code,
|
||||
response.headers.toMultimap()
|
||||
)
|
||||
response.isSuccessful -> return Success(
|
||||
responseBody(response.body(), accept),
|
||||
response.code(),
|
||||
response.headers().toMultimap()
|
||||
responseBody(response.body, accept),
|
||||
response.code,
|
||||
response.headers.toMultimap()
|
||||
)
|
||||
response.isClientError -> return ClientError(
|
||||
response.body()?.string(),
|
||||
response.code(),
|
||||
response.headers().toMultimap()
|
||||
response.body?.string(),
|
||||
response.code,
|
||||
response.headers.toMultimap()
|
||||
)
|
||||
else -> return ServerError(
|
||||
null,
|
||||
response.body()?.string(),
|
||||
response.code(),
|
||||
response.headers().toMultimap()
|
||||
response.body?.string(),
|
||||
response.code,
|
||||
response.headers.toMultimap()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,19 +5,19 @@ import okhttp3.Response
|
||||
/**
|
||||
* Provides an extension to evaluation whether the response is a 1xx code
|
||||
*/
|
||||
val Response.isInformational : Boolean get() = this.code() in 100..199
|
||||
val Response.isInformational : Boolean get() = this.code in 100..199
|
||||
|
||||
/**
|
||||
* Provides an extension to evaluation whether the response is a 3xx code
|
||||
*/
|
||||
val Response.isRedirect : Boolean get() = this.code() in 300..399
|
||||
val Response.isRedirect : Boolean get() = this.code in 300..399
|
||||
|
||||
/**
|
||||
* Provides an extension to evaluation whether the response is a 4xx code
|
||||
*/
|
||||
val Response.isClientError : Boolean get() = this.code() in 400..499
|
||||
val Response.isClientError : Boolean get() = this.code in 400..499
|
||||
|
||||
/**
|
||||
* Provides an extension to evaluation whether the response is a 5xx (Standard) through 999 (non-standard) code
|
||||
*/
|
||||
val Response.isServerError : Boolean get() = this.code() in 500..999
|
||||
val Response.isServerError : Boolean get() = this.code in 500..999
|
||||
Reference in New Issue
Block a user