diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache index 8c2213e586f9..bb12f4ebac55 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache @@ -55,7 +55,7 @@ import {{packageName}}.infrastructure.Serializer {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}companion object { @JvmStatic {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "{{{basePath}}}") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "{{{basePath}}}") } } diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache index 2faa8be0068c..0a0b7fc303f5 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache @@ -63,22 +63,81 @@ import com.squareup.moshi.adapter {{#nonPublicApi}}internal {{/nonPublicApi}}{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}open class ApiClient({{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val baseUrl: String, {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val client: Call.Factory = defaultClient) { {{#nonPublicApi}}internal {{/nonPublicApi}}{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}const val BASE_URL_KEY: String = "{{packageName}}.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}const val baseUrlKey: String = BASE_URL_KEY {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKey: MutableMap = mutableMapOf() {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKeyPrefix: MutableMap = mutableMapOf() {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}var username: String? = null {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}var password: String? = null {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}var accessToken: String? = null - {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}const val baseUrlKey: String = "{{packageName}}.baseUrl" @JvmStatic {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val defaultClient: OkHttpClient by lazy { @@ -98,7 +157,7 @@ import com.squareup.moshi.adapter protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -137,7 +196,7 @@ import com.squareup.moshi.adapter /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -167,7 +226,7 @@ import com.squareup.moshi.adapter if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { {{#moshi}} Serializer.moshi.adapter(Any::class.java).toJson(obj) @@ -191,7 +250,7 @@ import com.squareup.moshi.adapter /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -214,7 +273,7 @@ import com.squareup.moshi.adapter when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -236,7 +295,7 @@ import com.squareup.moshi.adapter } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -261,11 +320,11 @@ import com.squareup.moshi.adapter {{#kotlinx_serialization}} Serializer.kotlinxSerializationJson.encodeToString(content) {{/kotlinx_serialization}} - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @@ -273,11 +332,10 @@ import com.squareup.moshi.adapter {{#moshi}} @OptIn(ExperimentalStdlibApi::class) {{/moshi}} - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -352,8 +410,8 @@ import com.squareup.moshi.adapter }}{{#jackson}}Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference() {}){{/jackson}}{{! }}{{#kotlinx_serialization}}Serializer.kotlinxSerializationJson.decodeFromString(bodyContent){{/kotlinx_serialization}} } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -394,26 +452,26 @@ import com.squareup.moshi.adapter {{/isApiKey}} {{#isBasic}} {{#isBasicBasic}} - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { username?.let { username -> password?.let { password -> - requestConfig.headers[Authorization] = okhttp3.Credentials.basic(username, password) + requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(username, password) } } } {{/isBasicBasic}} {{#isBasicBearer}} - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken" + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken" } } {{/isBasicBearer}} {{/isBasic}} {{#isOAuth}} - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } {{/isOAuth}} @@ -440,21 +498,21 @@ import com.squareup.moshi.adapter }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -498,7 +556,7 @@ import com.squareup.moshi.adapter val response = client.newCall(request).execute() {{/useCoroutines}} - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache index 94c128e7967d..c48089c023d9 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache @@ -376,12 +376,16 @@ import okhttp3.MediaType.Companion.toMediaType } {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}companion object { - @JvmStatic - protected val baseUrlKey: String = "{{packageName}}.baseUrl" + protected const val BASE_URL_KEY: String = "{{packageName}}.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "{{{basePath}}}") + System.getProperties().getProperty(BASE_URL_KEY, "{{{basePath}}}") } } } diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-vertx/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-vertx/infrastructure/ApiClient.kt.mustache index 4a82f4680a5e..f87f194a6f42 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-vertx/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-vertx/infrastructure/ApiClient.kt.mustache @@ -12,11 +12,16 @@ import com.google.gson.reflect.TypeToken {{#nonPublicApi}}internal {{/nonPublicApi}}{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}open class ApiClient(val basePath: kotlin.String = defaultBasePath, val accessToken: String? = null, val apiKey: MutableMap = mutableMapOf(), val apiKeyPrefix: MutableMap = mutableMapOf(), var username: String? = null, var password: String? = null, val vertx: Vertx) { {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}companion object { - {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}const val baseUrlKey: String = "{{packageName}}.baseUrl" + {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}const val BASE_URL_KEY: String = "{{packageName}}.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}const val baseUrlKey: String = BASE_URL_KEY @JvmStatic {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "{{{basePath}}}") + System.getProperties().getProperty(BASE_URL_KEY, "{{{basePath}}}") } } diff --git a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/AuthApi.kt b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/AuthApi.kt index e04eeef563ad..7011b6f4b7c6 100644 --- a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/AuthApi.kt +++ b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/AuthApi.kt @@ -40,7 +40,7 @@ open class AuthApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000") } } diff --git a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/BodyApi.kt b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/BodyApi.kt index a9e6cc649eaf..1289ccfadd5a 100644 --- a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/BodyApi.kt +++ b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/BodyApi.kt @@ -42,7 +42,7 @@ open class BodyApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000") } } diff --git a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/FormApi.kt b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/FormApi.kt index e51c890865ec..07bfef2d46d0 100644 --- a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/FormApi.kt +++ b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/FormApi.kt @@ -40,7 +40,7 @@ open class FormApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000") } } diff --git a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/HeaderApi.kt b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/HeaderApi.kt index 13a8335cd837..d59003aac9b6 100644 --- a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/HeaderApi.kt +++ b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/HeaderApi.kt @@ -41,7 +41,7 @@ open class HeaderApi(basePath: kotlin.String = defaultBasePath, client: Call.Fac companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000") } } diff --git a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/PathApi.kt b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/PathApi.kt index 6894ec04db86..7c2ebfbf3281 100644 --- a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/PathApi.kt +++ b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/PathApi.kt @@ -41,7 +41,7 @@ open class PathApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000") } } diff --git a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/QueryApi.kt b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/QueryApi.kt index 75cf662eabea..470ff0236f99 100644 --- a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/QueryApi.kt +++ b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/apis/QueryApi.kt @@ -43,7 +43,7 @@ open class QueryApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost:3000") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost:3000") } } diff --git a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 53456dc6d566..b8085ef31f75 100644 --- a/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,23 +342,23 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { username?.let { username -> password?.let { password -> - requestConfig.headers[Authorization] = okhttp3.Credentials.basic(username, password) + requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(username, password) } } } - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken" + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken" } } } @@ -322,21 +380,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -359,7 +417,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/echo_api/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/echo_api/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index ddf86814d26b..696797f37b1c 100644 --- a/samples/client/echo_api/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/echo_api/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -158,12 +158,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://localhost:3000") + System.getProperties().getProperty(BASE_URL_KEY, "http://localhost:3000") } } } diff --git a/samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt b/samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt index 9d6ce3bc795d..0657a3706ff7 100644 --- a/samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt +++ b/samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt @@ -41,7 +41,7 @@ open class DefaultApi(basePath: kotlin.String = defaultBasePath, client: Call.Fa companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost") } } diff --git a/samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 608a208cac4a..c78ab5e0d9ef 100644 --- a/samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,8 +342,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -305,21 +363,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -342,7 +400,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index f79d5c65abbd..4cd7d6f714a6 100644 --- a/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index a901ca43190f..6f6b7b0dd4c6 100644 --- a/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index fb57439ae60d..4343d03a91b3 100644 --- a/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 2ddfd72c32c2..b6fabcf91636 100644 --- a/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt b/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt index e13b07eb5da7..eb5a1a3ada35 100644 --- a/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt +++ b/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt @@ -40,7 +40,7 @@ open class DefaultApi(basePath: kotlin.String = defaultBasePath, client: Call.Fa companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost") } } diff --git a/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 608a208cac4a..c78ab5e0d9ef 100644 --- a/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,8 +342,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -305,21 +363,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -342,7 +400,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/others/kotlin-jvm-okhttp-path-comments/src/main/kotlin/org/openapitools/client/apis/StuffApi.kt b/samples/client/others/kotlin-jvm-okhttp-path-comments/src/main/kotlin/org/openapitools/client/apis/StuffApi.kt index 236318dd4855..7b3573f0b464 100644 --- a/samples/client/others/kotlin-jvm-okhttp-path-comments/src/main/kotlin/org/openapitools/client/apis/StuffApi.kt +++ b/samples/client/others/kotlin-jvm-okhttp-path-comments/src/main/kotlin/org/openapitools/client/apis/StuffApi.kt @@ -41,7 +41,7 @@ open class StuffApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "https://example.org/v1") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "https://example.org/v1") } } diff --git a/samples/client/others/kotlin-jvm-okhttp-path-comments/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/others/kotlin-jvm-okhttp-path-comments/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 3b5b923b5a27..c7334e173bcd 100644 --- a/samples/client/others/kotlin-jvm-okhttp-path-comments/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/others/kotlin-jvm-okhttp-path-comments/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken" + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken" } } } @@ -315,21 +373,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -352,7 +410,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/others/kotlin-oneOf-anyOf-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/others/kotlin-oneOf-anyOf-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 556870f29311..54844953f228 100644 --- a/samples/client/others/kotlin-oneOf-anyOf-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/others/kotlin-oneOf-anyOf-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -100,12 +100,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://example.org") + System.getProperties().getProperty(BASE_URL_KEY, "http://example.org") } } } diff --git a/samples/client/others/kotlin-oneOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/others/kotlin-oneOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 556870f29311..54844953f228 100644 --- a/samples/client/others/kotlin-oneOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/others/kotlin-oneOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -100,12 +100,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://example.org") + System.getProperties().getProperty(BASE_URL_KEY, "http://example.org") } } } diff --git a/samples/client/petstore/kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/apis/BirdApi.kt b/samples/client/petstore/kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/apis/BirdApi.kt index b12852467001..4fdaf72cb29c 100644 --- a/samples/client/petstore/kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/apis/BirdApi.kt +++ b/samples/client/petstore/kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/apis/BirdApi.kt @@ -43,7 +43,7 @@ open class BirdApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://example.org") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://example.org") } } diff --git a/samples/client/petstore/kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 82abe8c6e784..cccc180e9524 100644 --- a/samples/client/petstore/kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -35,22 +35,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -70,7 +129,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -109,7 +168,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -139,7 +198,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { // Note: Without a custom serializer, kotlinx.serialization cannot serialize Any? // The custom serializer should be provided at PartConfig creation to capture type info @@ -152,7 +211,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -175,7 +234,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -197,7 +256,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -211,20 +270,19 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.kotlinxSerializationJson.encodeToString(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -286,8 +344,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.kotlinxSerializationJson.decodeFromString(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -307,21 +365,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -344,7 +402,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/apis/BirdApi.kt b/samples/client/petstore/kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/apis/BirdApi.kt index 77824ecc64f5..124dfa2a9699 100644 --- a/samples/client/petstore/kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/apis/BirdApi.kt +++ b/samples/client/petstore/kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/apis/BirdApi.kt @@ -41,7 +41,7 @@ open class BirdApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://example.org") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://example.org") } } diff --git a/samples/client/petstore/kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 608a208cac4a..c78ab5e0d9ef 100644 --- a/samples/client/petstore/kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,8 +342,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -305,21 +363,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -342,7 +400,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt b/samples/client/petstore/kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt index 56065cde7824..e5ac791dbdb4 100644 --- a/samples/client/petstore/kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt +++ b/samples/client/petstore/kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt @@ -41,7 +41,7 @@ open class DefaultApi(basePath: kotlin.String = defaultBasePath, client: Call.Fa companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost") } } diff --git a/samples/client/petstore/kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 608a208cac4a..c78ab5e0d9ef 100644 --- a/samples/client/petstore/kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,8 +342,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -305,21 +363,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -342,7 +400,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt index 293fe1a317e8..b3abaeb0baa8 100644 --- a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt +++ b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt @@ -40,7 +40,7 @@ open class DefaultApi(basePath: kotlin.String = defaultBasePath, client: Call.Fa companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost") } } diff --git a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 608a208cac4a..c78ab5e0d9ef 100644 --- a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,8 +342,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -305,21 +363,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -342,7 +400,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt b/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt index 8fb4aa9454d9..157d18c96038 100644 --- a/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt +++ b/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt @@ -41,7 +41,7 @@ open class DefaultApi(basePath: kotlin.String = defaultBasePath, client: Call.Fa companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost") } } diff --git a/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 608a208cac4a..c78ab5e0d9ef 100644 --- a/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,8 +342,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -305,21 +363,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -342,7 +400,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt b/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt index 87e875b42b95..e17c5a0fc211 100644 --- a/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt +++ b/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt @@ -40,7 +40,7 @@ open class DefaultApi(basePath: kotlin.String = defaultBasePath, client: Call.Fa companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost") } } diff --git a/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 608a208cac4a..c78ab5e0d9ef 100644 --- a/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,8 +342,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -305,21 +363,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -342,7 +400,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-default-values-jvm-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-default-values-jvm-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index aa1e42bd820d..e8455b97d3b3 100644 --- a/samples/client/petstore/kotlin-default-values-jvm-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-default-values-jvm-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -100,12 +100,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://localhost") + System.getProperties().getProperty(BASE_URL_KEY, "http://localhost") } } } diff --git a/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt b/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt index 6d75872731f8..c073367741eb 100644 --- a/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt +++ b/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt @@ -41,7 +41,7 @@ open class DefaultApi(basePath: kotlin.String = defaultBasePath, client: Call.Fa companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost") } } diff --git a/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 608a208cac4a..c78ab5e0d9ef 100644 --- a/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,8 +342,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -305,21 +363,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -342,7 +400,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 9faa2eedaf4f..ea1f10b74aa6 100644 --- a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ public open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call public companion object { @JvmStatic public val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 8aa8db04813c..d65fcc4eb6b8 100644 --- a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ public open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Ca public companion object { @JvmStatic public val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index a05b697c7d29..0ee1b7821b99 100644 --- a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ public open class UserApi(basePath: kotlin.String = defaultBasePath, client: Cal public companion object { @JvmStatic public val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 27b2d0a1d647..de25fbf36665 100644 --- a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ public val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() public open class ApiClient(public val baseUrl: String, public val client: Call.Factory = defaultClient) { public companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + public const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + public const val baseUrlKey: String = BASE_URL_KEY public val apiKey: MutableMap = mutableMapOf() public val apiKeyPrefix: MutableMap = mutableMapOf() public var username: String? = null public var password: String? = null public var accessToken: String? = null - public const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic public val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index d5605d2dfb6a..3e919f23e574 100644 --- a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 83f36f7f7765..8a5426ffb6fa 100644 --- a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 59ae1b3fcd8d..af8aecc33c62 100644 --- a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index e4d1d2ec2b5b..df427adfe681 100644 --- a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.gson.toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,20 +267,19 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.gson.toJson(content, T::class.java) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -283,16 +341,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.gson.fromJson(bodyContent, (object: TypeToken(){}).getType()) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -323,21 +381,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -360,7 +418,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 2ebcca471c90..84d88cfb6673 100644 --- a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 7363f4426a0d..58450b979123 100644 --- a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 22f5496de77e..eb0154b68e09 100644 --- a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 81368b73d125..b74ee7ab19e9 100644 --- a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.jacksonObjectMapper.writeValueAsString(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,20 +267,19 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.jacksonObjectMapper.writeValueAsString(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -283,16 +341,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference() {}) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -323,21 +381,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -360,7 +418,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 8a466b82b9bc..6317001d87be 100644 --- a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -44,7 +44,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 8aac14de6e43..fb33481553d3 100644 --- a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -43,7 +43,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 9fb061238832..8838536653ce 100644 --- a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -43,7 +43,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 3b8c498e110b..b5cbd5bd1cb4 100644 --- a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -36,22 +36,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -71,7 +130,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -110,7 +169,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -140,7 +199,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { // Note: Without a custom serializer, kotlinx.serialization cannot serialize Any? // The custom serializer should be provided at PartConfig creation to capture type info @@ -153,7 +212,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -176,7 +235,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -198,7 +257,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -212,20 +271,19 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.kotlinxSerializationJson.encodeToString(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -291,16 +349,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.kotlinxSerializationJson.decodeFromString(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -331,21 +389,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -368,7 +426,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-jvm-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 7222dca1706b..8ed82e2fd3c0 100644 --- a/samples/client/petstore/kotlin-jvm-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -211,12 +211,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } } diff --git a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 9a8e8be9a165..06e917b7c06c 100644 --- a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -44,7 +44,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 852864017f16..13445b1f3c02 100644 --- a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -43,7 +43,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 106eb564c083..3e31e0fbcac6 100644 --- a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -43,7 +43,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 104be9ffad7c..fe635449f668 100644 --- a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -37,22 +37,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -72,7 +131,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -111,7 +170,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -141,7 +200,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.gson.toJson(obj) } else { @@ -152,7 +211,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -175,7 +234,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -197,7 +256,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -211,20 +270,19 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.gson.toJson(content, T::class.java) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -286,16 +344,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.gson.fromJson(bodyContent, (object: TypeToken(){}).getType()) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -326,21 +384,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -374,7 +432,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }) } - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-jvm-retrofit2-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-retrofit2-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index d2a8ee6fbf86..6ecab1d71386 100644 --- a/samples/client/petstore/kotlin-jvm-retrofit2-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-retrofit2-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -212,12 +212,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } } diff --git a/samples/client/petstore/kotlin-jvm-vertx-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-vertx-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index ed3c8ad3ef0f..5f7a51d660ae 100644 --- a/samples/client/petstore/kotlin-jvm-vertx-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-vertx-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -7,11 +7,16 @@ import com.google.gson.reflect.TypeToken open class ApiClient(val basePath: kotlin.String = defaultBasePath, val accessToken: String? = null, val apiKey: MutableMap = mutableMapOf(), val apiKeyPrefix: MutableMap = mutableMapOf(), var username: String? = null, var password: String? = null, val vertx: Vertx) { companion object { - const val baseUrlKey: String = "org.openapitools.client.baseUrl" + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-jvm-vertx-jackson-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-vertx-jackson-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index e8b269552d46..e5793db7cc5f 100644 --- a/samples/client/petstore/kotlin-jvm-vertx-jackson-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-vertx-jackson-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -7,11 +7,16 @@ import com.fasterxml.jackson.core.type.TypeReference open class ApiClient(val basePath: kotlin.String = defaultBasePath, val accessToken: String? = null, val apiKey: MutableMap = mutableMapOf(), val apiKeyPrefix: MutableMap = mutableMapOf(), var username: String? = null, var password: String? = null, val vertx: Vertx) { companion object { - const val baseUrlKey: String = "org.openapitools.client.baseUrl" + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-jvm-vertx-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-vertx-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index e8b269552d46..e5793db7cc5f 100644 --- a/samples/client/petstore/kotlin-jvm-vertx-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-vertx-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -7,11 +7,16 @@ import com.fasterxml.jackson.core.type.TypeReference open class ApiClient(val basePath: kotlin.String = defaultBasePath, val accessToken: String? = null, val apiKey: MutableMap = mutableMapOf(), val apiKeyPrefix: MutableMap = mutableMapOf(), var username: String? = null, var password: String? = null, val vertx: Vertx) { companion object { - const val baseUrlKey: String = "org.openapitools.client.baseUrl" + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-jvm-vertx-moshi/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-vertx-moshi/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index aa29a11b2b55..a258b2d43f46 100644 --- a/samples/client/petstore/kotlin-jvm-vertx-moshi/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-vertx-moshi/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -6,11 +6,16 @@ import java.nio.charset.StandardCharsets open class ApiClient(val basePath: kotlin.String = defaultBasePath, val accessToken: String? = null, val apiKey: MutableMap = mutableMapOf(), val apiKeyPrefix: MutableMap = mutableMapOf(), var username: String? = null, var password: String? = null, val vertx: Vertx) { companion object { - const val baseUrlKey: String = "org.openapitools.client.baseUrl" + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 776939908660..6aef60486835 100644 --- a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 59993ba435a8..17b34e3f7713 100644 --- a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 2deab8c45e8f..eb9b58156696 100644 --- a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 24ba4d333f1b..879f5998dfcd 100644 --- a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 892c6a50d21f..085e5e1fe1e2 100644 --- a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -231,12 +231,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } } diff --git a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 39d6444da809..cb2390f6152d 100644 --- a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index d90b515fb867..6e6fe9177c3c 100644 --- a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index f5d3bd69a1b6..ca99d5e78e4c 100644 --- a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 24ba4d333f1b..879f5998dfcd 100644 --- a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 776939908660..6aef60486835 100644 --- a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 59993ba435a8..17b34e3f7713 100644 --- a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 2deab8c45e8f..eb9b58156696 100644 --- a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 24ba4d333f1b..879f5998dfcd 100644 --- a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt b/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt index b8965bb2fad9..7ebab165b152 100644 --- a/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt +++ b/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt @@ -41,7 +41,7 @@ open class FakeApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://localhost") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://localhost") } } diff --git a/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 608a208cac4a..c78ab5e0d9ef 100644 --- a/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,8 +342,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -305,21 +363,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -342,7 +400,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index d05e53330e23..473de291ab95 100644 --- a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ internal class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Fa companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 3e5c75d189b6..82c5de679b02 100644 --- a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ internal class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call. companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 5f849b170c6f..7193981a896d 100644 --- a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ internal class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.F companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 8ed3a3eb901a..41ca6b16533d 100644 --- a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ internal val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() internal open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { internal companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 3304b4841795..c3d0dc197da7 100644 --- a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index b9f209eddab5..534aff705f20 100644 --- a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index c26277604c00..a39974198e74 100644 --- a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 24ba4d333f1b..879f5998dfcd 100644 --- a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-retrofit2-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-retrofit2-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 7222dca1706b..8ed82e2fd3c0 100644 --- a/samples/client/petstore/kotlin-retrofit2-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-retrofit2-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -211,12 +211,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } } diff --git a/samples/client/petstore/kotlin-retrofit2-kotlinx_serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-retrofit2-kotlinx_serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 9a4b719801ca..fb54a80fbf62 100644 --- a/samples/client/petstore/kotlin-retrofit2-kotlinx_serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-retrofit2-kotlinx_serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -211,12 +211,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } } diff --git a/samples/client/petstore/kotlin-retrofit2-rx3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-retrofit2-rx3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index d9fbeb34f0b3..ad0963420783 100644 --- a/samples/client/petstore/kotlin-retrofit2-rx3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-retrofit2-rx3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -213,12 +213,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } } diff --git a/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 5a6ac9cc7fcb..657d49349d0f 100644 --- a/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -211,12 +211,16 @@ class ApiClient( } companion object { - @JvmStatic - protected val baseUrlKey: String = "org.openapitools.client.baseUrl" + protected const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + protected const val baseUrlKey: String = BASE_URL_KEY @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(BASE_URL_KEY, "http://petstore.swagger.io/v2") } } } diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 5d2a69f2177b..483d0f247d5a 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 59993ba435a8..17b34e3f7713 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 2deab8c45e8f..eb9b58156696 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } 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 24ba4d333f1b..879f5998dfcd 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 @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 776939908660..6aef60486835 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 59993ba435a8..17b34e3f7713 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 2deab8c45e8f..eb9b58156696 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } 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 560d92783571..7322b44ff2c2 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 @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/apis/EnumApi.kt b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/apis/EnumApi.kt index 2cd6c8d9af19..a04ef8851e86 100644 --- a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/apis/EnumApi.kt +++ b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/apis/EnumApi.kt @@ -43,7 +43,7 @@ open class EnumApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 82abe8c6e784..cccc180e9524 100644 --- a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -35,22 +35,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -70,7 +129,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -109,7 +168,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -139,7 +198,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { // Note: Without a custom serializer, kotlinx.serialization cannot serialize Any? // The custom serializer should be provided at PartConfig creation to capture type info @@ -152,7 +211,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -175,7 +234,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -197,7 +256,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -211,20 +270,19 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.kotlinxSerializationJson.encodeToString(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -286,8 +344,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.kotlinxSerializationJson.decodeFromString(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } @@ -307,21 +365,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -344,7 +402,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL") diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 776939908660..6aef60486835 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -42,7 +42,7 @@ open class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factor companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 59993ba435a8..17b34e3f7713 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -41,7 +41,7 @@ open class StoreApi(basePath: kotlin.String = defaultBasePath, client: Call.Fact companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 2deab8c45e8f..eb9b58156696 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -41,7 +41,7 @@ open class UserApi(basePath: kotlin.String = defaultBasePath, client: Call.Facto companion object { @JvmStatic val defaultBasePath: String by lazy { - System.getProperties().getProperty(ApiClient.baseUrlKey, "http://petstore.swagger.io/v2") + System.getProperties().getProperty(ApiClient.BASE_URL_KEY, "http://petstore.swagger.io/v2") } } diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 24ba4d333f1b..879f5998dfcd 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody() open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClient) { companion object { - protected const val ContentType: String = "Content-Type" - protected const val Accept: String = "Accept" - protected const val Authorization: String = "Authorization" - protected const val JsonMediaType: String = "application/json" - protected const val FormDataMediaType: String = "multipart/form-data" - protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded" - protected const val XmlMediaType: String = "application/xml" - protected const val OctetMediaType: String = "application/octet-stream" - protected const val TextMediaType: String = "text/plain" + protected const val CONTENT_TYPE: String = "Content-Type" + @Deprecated( + message = "Please use the capitalized constant `CONTENT_TYPE` instead.", + replaceWith = ReplaceWith("CONTENT_TYPE") + ) + protected const val ContentType: String = CONTENT_TYPE + + protected const val ACCEPT: String = "Accept" + @Deprecated( + message = "Please use the capitalized constant `ACCEPT` instead.", + replaceWith = ReplaceWith("ACCEPT") + ) + protected const val Accept: String = ACCEPT + + protected const val AUTHORIZATION: String = "Authorization" + @Deprecated( + message = "Please use the capitalized constant `AUTHORIZATION` instead.", + replaceWith = ReplaceWith("AUTHORIZATION") + ) + protected const val Authorization: String = AUTHORIZATION + + protected const val JSON_MEDIA_TYPE: String = "application/json" + @Deprecated( + message = "Please use the capitalized constant `JSON_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("JSON_MEDIA_TYPE") + ) + protected const val JsonMediaType: String = JSON_MEDIA_TYPE + + protected const val FORM_DATA_MEDIA_TYPE: String = "multipart/form-data" + @Deprecated( + message = "Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_DATA_MEDIA_TYPE") + ) + protected const val FormDataMediaType: String = FORM_DATA_MEDIA_TYPE + + protected const val FORM_URL_ENC_MEDIA_TYPE: String = "application/x-www-form-urlencoded" + @Deprecated( + message = "Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("FORM_URL_ENC_MEDIA_TYPE") + ) + protected const val FormUrlEncMediaType: String = FORM_URL_ENC_MEDIA_TYPE + + protected const val XML_MEDIA_TYPE: String = "application/xml" + @Deprecated( + message = "Please use the capitalized constant `XML_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("XML_MEDIA_TYPE") + ) + protected const val XmlMediaType: String = XML_MEDIA_TYPE + + protected const val OCTET_MEDIA_TYPE: String = "application/octet-stream" + @Deprecated( + message = "Please use the capitalized constant `OCTET_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("OCTET_MEDIA_TYPE") + ) + protected const val OctetMediaType: String = OCTET_MEDIA_TYPE + + protected const val TEXT_MEDIA_TYPE: String = "text/plain" + @Deprecated( + message = "Please use the capitalized constant `TEXT_MEDIA_TYPE` instead.", + replaceWith = ReplaceWith("TEXT_MEDIA_TYPE") + ) + protected const val TextMediaType: String = TEXT_MEDIA_TYPE + + const val BASE_URL_KEY: String = "org.openapitools.client.baseUrl" + @Deprecated( + message = "Please use the capitalized constant `BASE_URL_KEY` instead.", + replaceWith = ReplaceWith("BASE_URL_KEY") + ) + const val baseUrlKey: String = BASE_URL_KEY val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() var username: String? = null var password: String? = null var accessToken: String? = null - const val baseUrlKey: String = "org.openapitools.client.baseUrl" @JvmStatic val defaultClient: OkHttpClient by lazy { @@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String { val contentType = try { URLConnection.guessContentTypeFromStream(byteArray.inputStream()) - } catch (io: IOException) { + } catch (_: IOException) { "application/octet-stream" } return contentType @@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds a File to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie if (serializer != null) { return serializer(obj) } - + return if (contentType?.contains("json") == true) { Serializer.moshi.adapter(Any::class.java).toJson(obj) } else { @@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie /** * Adds any type to a MultipartBody.Builder * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. + * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File. * * @param name The field name to add in the request * @param headers The headers that are in the PartConfig @@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - mediaType == FormDataMediaType -> + mediaType == FORM_DATA_MEDIA_TYPE -> MultipartBody.Builder() .setType(MultipartBody.FORM) .apply { @@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } } }.build() - mediaType == FormUrlEncMediaType -> { + mediaType == FORM_URL_ENC_MEDIA_TYPE -> { FormBody.Builder().apply { // content's type *must* be Map> @Suppress("UNCHECKED_CAST") @@ -208,21 +267,20 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie EMPTY_REQUEST } else { Serializer.moshi.adapter(T::class.java).toJson(content) - .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) + .toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull()) } - mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") - mediaType == TextMediaType && content is String -> - content.toRequestBody(TextMediaType.toMediaTypeOrNull()) + mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == TEXT_MEDIA_TYPE && content is String -> + content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull()) // TODO: this should be extended with other serializers else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) - protected inline fun responseBody(response: Response, mediaType: String? = JsonMediaType): T? { + protected inline fun responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? { val body = response.body - if(body == null) { - return null - } else if (T::class.java == Unit::class.java) { + + if (T::class.java == Unit::class.java) { // No need to parse the body when we're not interested in the body // Useful when API is returning other Content-Type return null @@ -284,16 +342,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie } Serializer.moshi.adapter().fromJson(bodyContent) } - mediaType == OctetMediaType -> body.bytes() as? T - mediaType == TextMediaType -> body.string() as? T + mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T + mediaType == TEXT_MEDIA_TYPE -> body.string() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.") } } protected fun updateAuthParams(requestConfig: RequestConfig) { - if (requestConfig.headers[Authorization].isNullOrEmpty()) { + if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) { accessToken?.let { accessToken -> - requestConfig.headers[Authorization] = "Bearer $accessToken " + requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken " } } if (requestConfig.headers["api_key"].isNullOrEmpty()) { @@ -324,21 +382,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie }.build() // take content-type/accept from spec or set to default (application/json) if not defined - if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) { - requestConfig.headers[ContentType] = JsonMediaType + if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE].isNullOrEmpty()) { + requestConfig.headers[CONTENT_TYPE] = JSON_MEDIA_TYPE } - if (requestConfig.headers[Accept].isNullOrEmpty()) { - requestConfig.headers[Accept] = JsonMediaType + if (requestConfig.headers[ACCEPT].isNullOrEmpty()) { + requestConfig.headers[ACCEPT] = JSON_MEDIA_TYPE } val headers = requestConfig.headers - if (headers[Accept].isNullOrEmpty()) { - throw kotlin.IllegalStateException("Missing Accept header. This is required.") + if (headers[ACCEPT].isNullOrEmpty()) { + throw kotlin.IllegalStateException("Missing ACCEPT header. This is required.") } - val contentType = if (headers[ContentType] != null) { + val contentType = if (headers[CONTENT_TYPE] != null) { // TODO: support multiple contentType options here. - (headers[ContentType] as String).substringBefore(";").lowercase(Locale.US) + (headers[CONTENT_TYPE] as String).substringBefore(";").lowercase(Locale.US) } else { null } @@ -361,7 +419,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie val response = client.newCall(request).execute() - val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.US) + val accept = response.header(CONTENT_TYPE)?.substringBefore(";")?.lowercase(Locale.US) // TODO: handle specific mapping types. e.g. Map> @Suppress("UNNECESSARY_SAFE_CALL")