@@ -34,22 +34,81 @@ val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody()
3434
3535open class ApiClient (val baseUrl : String , val client : Call .Factory = defaultClient) {
3636 companion object {
37- protected const val ContentType : String = " Content-Type"
38- protected const val Accept : String = " Accept"
39- protected const val Authorization : String = " Authorization"
40- protected const val JsonMediaType : String = " application/json"
41- protected const val FormDataMediaType : String = " multipart/form-data"
42- protected const val FormUrlEncMediaType : String = " application/x-www-form-urlencoded"
43- protected const val XmlMediaType : String = " application/xml"
44- protected const val OctetMediaType : String = " application/octet-stream"
45- protected const val TextMediaType : String = " text/plain"
37+ protected const val CONTENT_TYPE : String = " Content-Type"
38+ @Deprecated(
39+ message = " Please use the capitalized constant `CONTENT_TYPE` instead." ,
40+ replaceWith = ReplaceWith (" CONTENT_TYPE" )
41+ )
42+ protected const val ContentType : String = CONTENT_TYPE
43+
44+ protected const val ACCEPT : String = " Accept"
45+ @Deprecated(
46+ message = " Please use the capitalized constant `ACCEPT` instead." ,
47+ replaceWith = ReplaceWith (" ACCEPT" )
48+ )
49+ protected const val Accept : String = ACCEPT
50+
51+ protected const val AUTHORIZATION : String = " Authorization"
52+ @Deprecated(
53+ message = " Please use the capitalized constant `AUTHORIZATION` instead." ,
54+ replaceWith = ReplaceWith (" AUTHORIZATION" )
55+ )
56+ protected const val Authorization : String = AUTHORIZATION
57+
58+ protected const val JSON_MEDIA_TYPE : String = " application/json"
59+ @Deprecated(
60+ message = " Please use the capitalized constant `JSON_MEDIA_TYPE` instead." ,
61+ replaceWith = ReplaceWith (" JSON_MEDIA_TYPE" )
62+ )
63+ protected const val JsonMediaType : String = JSON_MEDIA_TYPE
64+
65+ protected const val FORM_DATA_MEDIA_TYPE : String = " multipart/form-data"
66+ @Deprecated(
67+ message = " Please use the capitalized constant `FORM_DATA_MEDIA_TYPE` instead." ,
68+ replaceWith = ReplaceWith (" FORM_DATA_MEDIA_TYPE" )
69+ )
70+ protected const val FormDataMediaType : String = FORM_DATA_MEDIA_TYPE
71+
72+ protected const val FORM_URL_ENC_MEDIA_TYPE : String = " application/x-www-form-urlencoded"
73+ @Deprecated(
74+ message = " Please use the capitalized constant `FORM_URL_ENC_MEDIA_TYPE` instead." ,
75+ replaceWith = ReplaceWith (" FORM_URL_ENC_MEDIA_TYPE" )
76+ )
77+ protected const val FormUrlEncMediaType : String = FORM_URL_ENC_MEDIA_TYPE
78+
79+ protected const val XML_MEDIA_TYPE : String = " application/xml"
80+ @Deprecated(
81+ message = " Please use the capitalized constant `XML_MEDIA_TYPE` instead." ,
82+ replaceWith = ReplaceWith (" XML_MEDIA_TYPE" )
83+ )
84+ protected const val XmlMediaType : String = XML_MEDIA_TYPE
85+
86+ protected const val OCTET_MEDIA_TYPE : String = " application/octet-stream"
87+ @Deprecated(
88+ message = " Please use the capitalized constant `OCTET_MEDIA_TYPE` instead." ,
89+ replaceWith = ReplaceWith (" OCTET_MEDIA_TYPE" )
90+ )
91+ protected const val OctetMediaType : String = OCTET_MEDIA_TYPE
92+
93+ protected const val TEXT_MEDIA_TYPE : String = " text/plain"
94+ @Deprecated(
95+ message = " Please use the capitalized constant `TEXT_MEDIA_TYPE` instead." ,
96+ replaceWith = ReplaceWith (" TEXT_MEDIA_TYPE" )
97+ )
98+ protected const val TextMediaType : String = TEXT_MEDIA_TYPE
99+
100+ const val BASE_URL_KEY : String = " org.openapitools.client.baseUrl"
101+ @Deprecated(
102+ message = " Please use the capitalized constant `BASE_URL_KEY` instead." ,
103+ replaceWith = ReplaceWith (" BASE_URL_KEY" )
104+ )
105+ const val baseUrlKey: String = BASE_URL_KEY
46106
47107 val apiKey: MutableMap <String , String > = mutableMapOf ()
48108 val apiKeyPrefix: MutableMap <String , String > = mutableMapOf ()
49109 var username: String? = null
50110 var password: String? = null
51111 var accessToken: String? = null
52- const val baseUrlKey: String = " org.openapitools.client.baseUrl"
53112
54113 @JvmStatic
55114 val defaultClient: OkHttpClient by lazy {
@@ -69,7 +128,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
69128 protected fun guessContentTypeFromByteArray (byteArray : ByteArray ): String {
70129 val contentType = try {
71130 URLConnection .guessContentTypeFromStream(byteArray.inputStream())
72- } catch (io : IOException ) {
131+ } catch (_ : IOException ) {
73132 " application/octet-stream"
74133 }
75134 return contentType
@@ -108,7 +167,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
108167 /* *
109168 * Adds a File to a MultipartBody.Builder
110169 * Defined a helper in the requestBody method to not duplicate code
111- * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
170+ * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File
112171 *
113172 * @param name The field name to add in the request
114173 * @param headers The headers that are in the PartConfig
@@ -138,7 +197,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
138197 if (serializer != null ) {
139198 return serializer(obj)
140199 }
141-
200+
142201 return if (contentType?.contains(" json" ) == true ) {
143202 Serializer .jacksonObjectMapper.writeValueAsString(obj)
144203 } else {
@@ -149,7 +208,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
149208 /* *
150209 * Adds any type to a MultipartBody.Builder
151210 * Defined a helper in the requestBody method to not duplicate code
152- * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
211+ * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File.
153212 *
154213 * @param name The field name to add in the request
155214 * @param headers The headers that are in the PartConfig
@@ -172,7 +231,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
172231 when {
173232 content is ByteArray -> content.toRequestBody((mediaType ? : guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
174233 content is File -> content.asRequestBody((mediaType ? : guessContentTypeFromFile(content)).toMediaTypeOrNull())
175- mediaType == FormDataMediaType ->
234+ mediaType == FORM_DATA_MEDIA_TYPE ->
176235 MultipartBody .Builder ()
177236 .setType(MultipartBody .FORM )
178237 .apply {
@@ -194,7 +253,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
194253 }
195254 }
196255 }.build()
197- mediaType == FormUrlEncMediaType -> {
256+ mediaType == FORM_URL_ENC_MEDIA_TYPE -> {
198257 FormBody .Builder ().apply {
199258 // content's type *must* be Map<String, PartConfig<*>>
200259 @Suppress(" UNCHECKED_CAST" )
@@ -208,20 +267,19 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
208267 EMPTY_REQUEST
209268 } else {
210269 Serializer .jacksonObjectMapper.writeValueAsString(content)
211- .toRequestBody((mediaType ? : JsonMediaType ).toMediaTypeOrNull())
270+ .toRequestBody((mediaType ? : JSON_MEDIA_TYPE ).toMediaTypeOrNull())
212271 }
213- mediaType == XmlMediaType -> throw UnsupportedOperationException (" xml not currently supported." )
214- mediaType == TextMediaType && content is String ->
215- content.toRequestBody(TextMediaType .toMediaTypeOrNull())
272+ mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException (" xml not currently supported." )
273+ mediaType == TEXT_MEDIA_TYPE && content is String ->
274+ content.toRequestBody(TEXT_MEDIA_TYPE .toMediaTypeOrNull())
216275 // TODO: this should be extended with other serializers
217276 else -> throw UnsupportedOperationException (" requestBody currently only supports JSON body, text body, byte body and File body." )
218277 }
219278
220- protected inline fun <reified T : Any ? > responseBody (response : Response , mediaType : String? = JsonMediaType ): T ? {
279+ protected inline fun <reified T : Any ? > responseBody (response : Response , mediaType : String? = JSON_MEDIA_TYPE ): T ? {
221280 val body = response.body
222- if (body == null ) {
223- return null
224- } else if (T ::class .java == Unit ::class .java) {
281+
282+ if (T ::class .java == Unit ::class .java) {
225283 // No need to parse the body when we're not interested in the body
226284 // Useful when API is returning other Content-Type
227285 return null
@@ -283,16 +341,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
283341 }
284342 Serializer .jacksonObjectMapper.readValue(bodyContent, object : TypeReference <T >() {})
285343 }
286- mediaType == OctetMediaType -> body.bytes() as ? T
287- mediaType == TextMediaType -> body.string() as ? T
344+ mediaType == OCTET_MEDIA_TYPE -> body.bytes() as ? T
345+ mediaType == TEXT_MEDIA_TYPE -> body.string() as ? T
288346 else -> throw UnsupportedOperationException (" responseBody currently only supports JSON body, text body and byte body." )
289347 }
290348 }
291349
292350 protected fun <T > updateAuthParams (requestConfig : RequestConfig <T >) {
293- if (requestConfig.headers[Authorization ].isNullOrEmpty()) {
351+ if (requestConfig.headers[AUTHORIZATION ].isNullOrEmpty()) {
294352 accessToken?.let { accessToken ->
295- requestConfig.headers[Authorization ] = " Bearer $accessToken "
353+ requestConfig.headers[AUTHORIZATION ] = " Bearer $accessToken "
296354 }
297355 }
298356 if (requestConfig.headers[" api_key" ].isNullOrEmpty()) {
@@ -323,21 +381,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
323381 }.build()
324382
325383 // take content-type/accept from spec or set to default (application/json) if not defined
326- if (requestConfig.body != null && requestConfig.headers[ContentType ].isNullOrEmpty()) {
327- requestConfig.headers[ContentType ] = JsonMediaType
384+ if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE ].isNullOrEmpty()) {
385+ requestConfig.headers[CONTENT_TYPE ] = JSON_MEDIA_TYPE
328386 }
329- if (requestConfig.headers[Accept ].isNullOrEmpty()) {
330- requestConfig.headers[Accept ] = JsonMediaType
387+ if (requestConfig.headers[ACCEPT ].isNullOrEmpty()) {
388+ requestConfig.headers[ACCEPT ] = JSON_MEDIA_TYPE
331389 }
332390 val headers = requestConfig.headers
333391
334- if (headers[Accept ].isNullOrEmpty()) {
335- throw kotlin.IllegalStateException (" Missing Accept header. This is required." )
392+ if (headers[ACCEPT ].isNullOrEmpty()) {
393+ throw kotlin.IllegalStateException (" Missing ACCEPT header. This is required." )
336394 }
337395
338- val contentType = if (headers[ContentType ] != null ) {
396+ val contentType = if (headers[CONTENT_TYPE ] != null ) {
339397 // TODO: support multiple contentType options here.
340- (headers[ContentType ] as String ).substringBefore(" ;" ).lowercase(Locale .US )
398+ (headers[CONTENT_TYPE ] as String ).substringBefore(" ;" ).lowercase(Locale .US )
341399 } else {
342400 null
343401 }
@@ -360,7 +418,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
360418
361419 val response = client.newCall(request).execute()
362420
363- val accept = response.header(ContentType )?.substringBefore(" ;" )?.lowercase(Locale .US )
421+ val accept = response.header(CONTENT_TYPE )?.substringBefore(" ;" )?.lowercase(Locale .US )
364422
365423 // TODO: handle specific mapping types. e.g. Map<int, Class<?>>
366424 @Suppress(" UNNECESSARY_SAFE_CALL" )
0 commit comments