@@ -63,22 +63,22 @@ import com.squareup.moshi.adapter
6363
6464{ {#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) {
6565 {{#nonPublicApi} }internal { {/nonPublicApi} }{ {^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }companion object {
66- protected const val ContentType : String = " Content-Type"
67- protected const val Accept : String = " Accept "
68- protected const val Authorization : String = " Authorization "
69- protected const val JsonMediaType : String = " application/json"
70- protected const val FormDataMediaType : String = " multipart/form-data"
71- protected const val FormUrlEncMediaType : String = " application/x-www-form-urlencoded"
72- protected const val XmlMediaType : String = " application/xml"
73- protected const val OctetMediaType : String = " application/octet-stream"
74- protected const val TextMediaType : String = " text/plain"
66+ protected const val CONTENT_TYPE : String = " Content-Type"
67+ protected const val ACCEPT : String = " ACCEPT "
68+ protected const val AUTHORIZATION : String = " AUTHORIZATION "
69+ protected const val JSON_MEDIA_TYPE : String = " application/json"
70+ protected const val FORM_DATA_MEDIA_TYPE : String = " multipart/form-data"
71+ protected const val FORM_URL_ENC_MEDIA_TYPE : String = " application/x-www-form-urlencoded"
72+ protected const val XML_MEDIA_TYPE : String = " application/xml"
73+ protected const val OCTET_MEDIA_TYPE : String = " application/octet-stream"
74+ protected const val TEXT_MEDIA_TYPE : String = " text/plain"
7575
7676 {{^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }val apiKey: MutableMap<String , String > = mutableMapOf()
7777 { {^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }val apiKeyPrefix: MutableMap<String , String > = mutableMapOf()
7878 { {^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }var username: String? = null
7979 { {^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }var password: String? = null
8080 { {^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }var accessToken: String? = null
81- { {^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }const val baseUrlKey : String = "{ {packageName} }.baseUrl"
81+ { {^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }const val BASE_URL_KEY : String = "{ {packageName} }.baseUrl"
8282
8383 @JvmStatic
8484 { {^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }val defaultClient: OkHttpClient by lazy {
@@ -137,7 +137,7 @@ import com.squareup.moshi.adapter
137137 /**
138138 * Adds a File to a MultipartBody.Builder
139139 * Defined a helper in the requestBody method to not duplicate code
140- * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
140+ * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is a File
141141 *
142142 * @param name The field name to add in the request
143143 * @param headers The headers that are in the PartConfig
@@ -167,7 +167,7 @@ import com.squareup.moshi.adapter
167167 if (serializer != null) {
168168 return serializer(obj)
169169 }
170-
170+
171171 return if (contentType?.contains("json") == true) {
172172 {{#moshi} }
173173 Serializer.moshi.adapter(Any::class.java).toJson(obj)
@@ -191,7 +191,7 @@ import com.squareup.moshi.adapter
191191 /**
192192 * Adds any type to a MultipartBody.Builder
193193 * Defined a helper in the requestBody method to not duplicate code
194- * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
194+ * It will be used when the content is a `FORM_DATA_MEDIA_TYPE` and the body of the PartConfig is not a File.
195195 *
196196 * @param name The field name to add in the request
197197 * @param headers The headers that are in the PartConfig
@@ -214,7 +214,7 @@ import com.squareup.moshi.adapter
214214 when {
215215 content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
216216 content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
217- mediaType == FormDataMediaType ->
217+ mediaType == FORM_DATA_MEDIA_TYPE ->
218218 MultipartBody.Builder()
219219 .setType(MultipartBody.FORM)
220220 .apply {
@@ -236,7 +236,7 @@ import com.squareup.moshi.adapter
236236 }
237237 }
238238 }.build()
239- mediaType == FormUrlEncMediaType -> {
239+ mediaType == FORM_URL_ENC_MEDIA_TYPE -> {
240240 FormBody.Builder().apply {
241241 // content' s type *must* be Map< String, PartConfig< *>>
242242 @Suppress(" UNCHECKED_CAST" )
@@ -261,19 +261,19 @@ import com.squareup.moshi.adapter
261261 { {#kotlinx_serialization} }
262262 Serializer.kotlinxSerializationJson.encodeToString(content)
263263 { {/kotlinx_serialization} }
264- .toRequestBody((mediaType ?: JsonMediaType ).toMediaTypeOrNull())
264+ .toRequestBody((mediaType ?: JSON_MEDIA_TYPE ).toMediaTypeOrNull())
265265 }
266- mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
267- mediaType == TextMediaType && content is String ->
268- content.toRequestBody(TextMediaType .toMediaTypeOrNull())
266+ mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
267+ mediaType == TEXT_MEDIA_TYPE && content is String ->
268+ content.toRequestBody(TEXT_MEDIA_TYPE .toMediaTypeOrNull())
269269 // TODO: this should be extended with other serializers
270270 else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
271271 }
272272
273273 { {#moshi} }
274274 @OptIn(ExperimentalStdlibApi::class)
275275 { {/moshi} }
276- protected inline fun <reified T: Any? > responseBody(response: Response, mediaType: String? = JsonMediaType ): T? {
276+ protected inline fun <reified T: Any? > responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE ): T? {
277277 val body = response.body
278278 if (body == null) {
279279 return null
@@ -352,8 +352,8 @@ import com.squareup.moshi.adapter
352352 } }{ {#jackson} }Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference<T >() { } ){ {/jackson} }{ {!
353353 } }{ {#kotlinx_serialization} }Serializer.kotlinxSerializationJson.decodeFromString<T >(bodyContent){ {/kotlinx_serialization} }
354354 }
355- mediaType == OctetMediaType -> body.bytes() as? T
356- mediaType == TextMediaType -> body.string() as? T
355+ mediaType == OCTET_MEDIA_TYPE -> body.bytes() as? T
356+ mediaType == TEXT_MEDIA_TYPE -> body.string() as? T
357357 else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
358358 }
359359 }
@@ -394,26 +394,26 @@ import com.squareup.moshi.adapter
394394 { {/isApiKey} }
395395 { {#isBasic} }
396396 { {#isBasicBasic} }
397- if (requestConfig.headers[Authorization ].isNullOrEmpty()) {
397+ if (requestConfig.headers[AUTHORIZATION ].isNullOrEmpty()) {
398398 username?.let { username ->
399399 password?.let { password ->
400- requestConfig.headers[Authorization ] = okhttp3.Credentials.basic(username, password)
400+ requestConfig.headers[AUTHORIZATION ] = okhttp3.Credentials.basic(username, password)
401401 }
402402 }
403403 }
404404 { {/isBasicBasic} }
405405 { {#isBasicBearer} }
406- if (requestConfig.headers[Authorization ].isNullOrEmpty()) {
406+ if (requestConfig.headers[AUTHORIZATION ].isNullOrEmpty()) {
407407 accessToken?.let { accessToken ->
408- requestConfig.headers[Authorization ] = " Bearer $accessToken"
408+ requestConfig.headers[AUTHORIZATION ] = " Bearer $accessToken"
409409 }
410410 }
411411 { {/isBasicBearer} }
412412 { {/isBasic} }
413413 { {#isOAuth} }
414- if (requestConfig.headers[Authorization ].isNullOrEmpty()) {
414+ if (requestConfig.headers[AUTHORIZATION ].isNullOrEmpty()) {
415415 accessToken?.let { accessToken ->
416- requestConfig.headers[Authorization ] = " Bearer $accessToken "
416+ requestConfig.headers[AUTHORIZATION ] = " Bearer $accessToken "
417417 }
418418 }
419419 { {/isOAuth} }
@@ -440,21 +440,21 @@ import com.squareup.moshi.adapter
440440 }.build()
441441
442442 // take content-type/accept from spec or set to default (application/json) if not defined
443- if (requestConfig.body != null && requestConfig.headers[ContentType ].isNullOrEmpty()) {
444- requestConfig.headers[ContentType ] = JsonMediaType
443+ if (requestConfig.body != null && requestConfig.headers[CONTENT_TYPE ].isNullOrEmpty()) {
444+ requestConfig.headers[CONTENT_TYPE ] = JSON_MEDIA_TYPE
445445 }
446- if (requestConfig.headers[Accept ].isNullOrEmpty()) {
447- requestConfig.headers[Accept ] = JsonMediaType
446+ if (requestConfig.headers[ACCEPT ].isNullOrEmpty()) {
447+ requestConfig.headers[ACCEPT ] = JSON_MEDIA_TYPE
448448 }
449449 val headers = requestConfig.headers
450450
451- if (headers[Accept ].isNullOrEmpty()) {
452- throw kotlin.IllegalStateException(" Missing Accept header. This is required." )
451+ if (headers[ACCEPT ].isNullOrEmpty()) {
452+ throw kotlin.IllegalStateException(" Missing ACCEPT header. This is required." )
453453 }
454454
455- val contentType = if (headers[ContentType ] != null) {
455+ val contentType = if (headers[CONTENT_TYPE ] != null) {
456456 // TODO: support multiple contentType options here.
457- (headers[ContentType ] as String).substringBefore(" ;" ).lowercase(Locale.US)
457+ (headers[CONTENT_TYPE ] as String).substringBefore(" ;" ).lowercase(Locale.US)
458458 } else {
459459 null
460460 }
@@ -498,7 +498,7 @@ import com.squareup.moshi.adapter
498498 val response = client.newCall(request).execute()
499499 { {/useCoroutines} }
500500
501- val accept = response.header(ContentType )?.substringBefore(";")?.lowercase(Locale.US)
501+ val accept = response.header(CONTENT_TYPE )?.substringBefore(";")?.lowercase(Locale.US)
502502
503503 // TODO: handle specific mapping types. e.g. Map<int , Class <? >>
504504 @Suppress("UNNECESSARY_SAFE_CALL")
0 commit comments