[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
24 changed files with 282 additions and 139 deletions

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
}
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> {
val bodyContent = body.string()
if (bodyContent.isEmpty()) {
return null
}
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}