[kotlin][client] support text/plain in okhttp (#20250)

* refactor: simplify application/octet-stream check

* feat: support text/plain in kotlin okhttp client

* refactor: remove redundant always-false condition

content is ByteArray is checked earlier
This commit is contained in:
Kirill Romanov 2024-12-05 06:51:10 +03:00 committed by GitHub
parent 8a07557865
commit 05d4aa9f62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
23 changed files with 135 additions and 89 deletions

View File

@ -962,7 +962,9 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
).trim();
return "multipart/form-data".equals(mediaType)
|| "application/x-www-form-urlencoded".equals(mediaType)
|| (mediaType.startsWith("application/") && (mediaType.endsWith("json") || mediaType.endsWith("octet-stream")));
|| (mediaType.startsWith("application/") && mediaType.endsWith("json"))
|| "application/octet-stream".equals(mediaType)
|| "text/plain".equals(mediaType);
};
operation.consumes = operation.consumes == null ? null : operation.consumes.stream()
.filter(isSerializable)

View File

@ -70,6 +70,7 @@ import com.squareup.moshi.adapter
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"
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKey: MutableMap<String, String> = mutableMapOf()
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -209,10 +210,10 @@ import com.squareup.moshi.adapter
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
{{#moshi}}
@ -298,7 +299,8 @@ import com.squareup.moshi.adapter
}}{{#kotlinx_serialization}}Serializer.kotlinxSerializationJson.decodeFromString<T>(bodyContent){{/kotlinx_serialization}}
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
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"
public val apiKey: MutableMap<String, String> = mutableMapOf()
public val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JsonMediaType): T? {
@ -242,7 +243,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType())
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JsonMediaType): T? {
@ -242,7 +243,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference<T>() {})
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -43,6 +43,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -171,10 +172,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JsonMediaType): T? {
@ -248,7 +249,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.kotlinxSerializationJson.decodeFromString<T>(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -44,6 +44,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -172,10 +173,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JsonMediaType): T? {
@ -245,7 +246,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType())
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -42,6 +42,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -170,10 +171,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JsonMediaType): T? {
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.kotlinxSerializationJson.decodeFromString<T>(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
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"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}