forked from loafle/openapi-generator-original
[Kotlin]] add support for binary response (#9284)
* Support File responses for Kotlin client * Regen samples * Reset version * Fix file class check * Add imports * use Files.createTempFile instead * better tmp file naming Co-authored-by: Mischa Spiegelmock <me@mish.dev>
This commit is contained in:
parent
77dfd40681
commit
da37e980d3
@ -25,7 +25,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
{{^threetenbp}}
|
||||
import java.time.LocalDate
|
||||
@ -171,6 +174,15 @@ import org.threeten.bp.OffsetTime
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.{{packageName}}", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> {{#moshi}}Serializer.moshi.adapter(T::class.java).fromJson(bodyContent){{/moshi}}{{#gson}}Serializer.gson.fromJson(bodyContent, T::class.java){{/gson}}{{#jackson}}Serializer.jacksonObjectMapper.readValue(bodyContent, T::class.java){{/jackson}}
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.gson.fromJson(bodyContent, T::class.java)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.jacksonObjectMapper.readValue(bodyContent, T::class.java)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.gson.fromJson(bodyContent, T::class.java)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ internal open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -11,7 +11,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -112,6 +115,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import org.threeten.bp.LocalDate
|
||||
import org.threeten.bp.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
@ -13,7 +13,10 @@ import okhttp3.Request
|
||||
import okhttp3.Headers
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileWriter
|
||||
import java.net.URLConnection
|
||||
import java.nio.file.Files
|
||||
import java.util.Date
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@ -114,6 +117,15 @@ open class ApiClient(val baseUrl: String) {
|
||||
if (bodyContent.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempfile
|
||||
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
||||
f.deleteOnExit()
|
||||
val out = BufferedWriter(FileWriter(f))
|
||||
out.write(bodyContent)
|
||||
out.close()
|
||||
return f as T
|
||||
}
|
||||
return when(mediaType) {
|
||||
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
|
||||
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
||||
|
Loading…
x
Reference in New Issue
Block a user