[Kotlin][client] fix file upload (#5548)

* [kotlin] fix file upload

* [kotlin] fix file upload

* [kotlin] fix file upload

* [kotlin][client] fix jackson integration

* [kotlin] fix file upload

* [kotlin] fix file upload
This commit is contained in:
Bruno Coelho
2020-03-10 17:04:53 +00:00
committed by GitHub
parent ce8cdcdf25
commit f402126460
22 changed files with 856 additions and 89 deletions

View File

@@ -293,9 +293,9 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
*/
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?) : Unit {
val localVariableBody: kotlin.Any? = mapOf("name" to "$name", "status" to "$status")
val localVariableBody: kotlin.Any? = mapOf("name" to name, "status" to status)
val localVariableQuery: MultiValueMap = mutableMapOf()
val localVariableHeaders: MutableMap<String, String> = mutableMapOf("Content-Type" to "")
val localVariableHeaders: MutableMap<String, String> = mutableMapOf("Content-Type" to "application/x-www-form-urlencoded")
val localVariableConfig = RequestConfig(
RequestMethod.POST,
"/pet/{petId}".replace("{"+"petId"+"}", "$petId"),
@@ -336,9 +336,9 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
@Suppress("UNCHECKED_CAST")
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : ApiResponse {
val localVariableBody: kotlin.Any? = mapOf("additionalMetadata" to "$additionalMetadata", "file" to "$file")
val localVariableBody: kotlin.Any? = mapOf("additionalMetadata" to additionalMetadata, "file" to file)
val localVariableQuery: MultiValueMap = mutableMapOf()
val localVariableHeaders: MutableMap<String, String> = mutableMapOf("Content-Type" to "")
val localVariableHeaders: MutableMap<String, String> = mutableMapOf("Content-Type" to "multipart/form-data")
val localVariableConfig = RequestConfig(
RequestMethod.POST,
"/pet/{petId}/uploadImage".replace("{"+"petId"+"}", "$petId"),

View File

@@ -10,7 +10,16 @@ import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.ResponseBody
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.Headers
import okhttp3.MultipartBody
import java.io.File
import java.net.URLConnection
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.OffsetTime
open class ApiClient(val baseUrl: String) {
companion object {
@@ -37,17 +46,55 @@ open class ApiClient(val baseUrl: String) {
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
* @param file The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromFile(file: File): String {
val contentType = URLConnection.guessContentTypeFromName(file.name)
return contentType ?: "application/octet-stream"
}
protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
when {
content is File -> content.asRequestBody(
mediaType.toMediaTypeOrNull()
)
mediaType == FormDataMediaType || mediaType == FormUrlEncMediaType -> {
mediaType == FormDataMediaType -> {
MultipartBody.Builder()
.setType(MultipartBody.FORM)
.apply {
// content's type *must* be Map<String, Any?>
@Suppress("UNCHECKED_CAST")
(content as Map<String, Any?>).forEach { (key, value) ->
if (value is File) {
val partHeaders = Headers.headersOf(
"Content-Disposition",
"form-data; name=\"$key\"; filename=\"${value.name}\""
)
val fileMediaType = guessContentTypeFromFile(value).toMediaTypeOrNull()
addPart(partHeaders, value.asRequestBody(fileMediaType))
} else {
val partHeaders = Headers.headersOf(
"Content-Disposition",
"form-data; name=\"$key\""
)
addPart(
partHeaders,
parameterToString(value).toRequestBody(null)
)
}
}
}.build()
}
mediaType == FormUrlEncMediaType -> {
FormBody.Builder().apply {
// content's type *must* be Map<String, Any>
// content's type *must* be Map<String, Any?>
@Suppress("UNCHECKED_CAST")
(content as Map<String,String>).forEach { (key, value) ->
add(key, value)
(content as Map<String, Any?>).forEach { (key, value) ->
add(key, parameterToString(value))
}
}.build()
}
@@ -172,6 +219,26 @@ open class ApiClient(val baseUrl: String) {
}
}
protected fun parameterToString(value: Any?): String {
when (value) {
null -> {
return ""
}
is Array<*> -> {
return toMultiValue(value, "csv").toString()
}
is Iterable<*> -> {
return toMultiValue(value, "csv").toString()
}
is OffsetDateTime, is OffsetTime, is LocalDateTime, is LocalDate, is LocalTime, is Date -> {
return parseDateToQueryString<Any>(value)
}
else -> {
return value.toString()
}
}
}
protected inline fun <reified T: Any> parseDateToQueryString(value : T): String {
return value.toString()
}