[Kotlin Client] Implement octet-stream/binary (#16341)

* Implement octet-stream/binary for Kotlin client code generator

* Fix OkHttp3 generator flavor
This commit is contained in:
Simon Wegendt 2023-08-17 11:40:22 +02:00 committed by GitHub
parent e69c58623f
commit 778a53a406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 282 additions and 139 deletions

View File

@ -849,7 +849,7 @@ 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.startsWith("application/") && (mediaType.endsWith("json") || mediaType.endsWith("octet-stream")));
};
operation.consumes = operation.consumes == null ? null : operation.consumes.stream()
.filter(isSerializable)

View File

@ -86,6 +86,7 @@ import com.squareup.moshi.adapter
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -214,8 +215,15 @@ import com.squareup.moshi.adapter
}
{{/jvm-okhttp4}}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
{{#jvm-okhttp3}}
RequestBody.create(MediaType.parse(OctetMediaType), content)
{{/jvm-okhttp3}}
{{#jvm-okhttp4}}
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
{{/jvm-okhttp4}}
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
{{#moshi}}
@ -247,16 +255,19 @@ import com.squareup.moshi.adapter
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
{{#moshi}}Serializer.moshi.adapter<T>().fromJson(bodyContent){{/moshi}}{{!
}}{{#gson}}Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType()){{/gson}}{{!
}}{{#jackson}}Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference<T>() {}){{/jackson}}{{!
}}{{#kotlinx_serialization}}Serializer.kotlinxSerializationJson.decodeFromString<T>(bodyContent){{/kotlinx_serialization}}
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -35,6 +35,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -109,8 +110,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
)
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
RequestBody.create(MediaType.parse(OctetMediaType), content)
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -130,13 +133,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -35,6 +35,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -109,8 +110,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
)
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
RequestBody.create(MediaType.parse(OctetMediaType), content)
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -130,13 +133,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
@ -131,13 +134,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType())
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
@ -131,13 +134,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference<T>() {})
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -40,6 +40,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -113,8 +114,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
@ -137,13 +140,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.kotlinxSerializationJson.decodeFromString<T>(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -114,8 +115,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
@ -134,13 +137,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType())
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ internal open class ApiClient(val baseUrl: String, val client: OkHttpClient = de
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ internal open class ApiClient(val baseUrl: String, val client: OkHttpClient = de
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ internal open class ApiClient(val baseUrl: String, val client: OkHttpClient = de
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -35,6 +35,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -109,8 +110,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
)
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
RequestBody.create(MediaType.parse(OctetMediaType), content)
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -130,13 +133,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -39,6 +39,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -112,8 +113,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.kotlinxSerializationJson.decodeFromString<T>(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}

View File

@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
protected const val OctetMediaType = "application/octet-stream"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
}
@OptIn(ExperimentalStdlibApi::class)
@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
return tempFile as T
}
return when {
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}