[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

@@ -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
}
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.")
}
}