forked from loafle/openapi-generator-original
[kotlin][client] fix temp file name and remove old deprecated jvm-okhttp3 (#19064)
* [kotlin][client] fix temp file name and remove old deprecated jvm-okhttp3 * [kotlin][client] fix temp file name and remove old deprecated jvm-okhttp3 * [kotlin][client] fix temp file name and remove old deprecated jvm-okhttp3
This commit is contained in:
@@ -27,10 +27,11 @@ import java.time.LocalTime
|
||||
import java.time.OffsetDateTime
|
||||
import java.time.OffsetTime
|
||||
import java.util.Locale
|
||||
import java.util.regex.Pattern
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
|
||||
val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody()
|
||||
val EMPTY_REQUEST: RequestBody = ByteArray(0).toRequestBody()
|
||||
|
||||
open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClient) {
|
||||
companion object {
|
||||
@@ -121,17 +122,55 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
|
||||
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? {
|
||||
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JsonMediaType): T? {
|
||||
val body = response.body
|
||||
if(body == null) {
|
||||
return null
|
||||
}
|
||||
if (T::class.java == File::class.java) {
|
||||
// return tempFile
|
||||
val contentDisposition = response.header("Content-Disposition")
|
||||
|
||||
val fileName = if (contentDisposition != null) {
|
||||
// Get filename from the Content-Disposition header.
|
||||
val pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?")
|
||||
val matcher = pattern.matcher(contentDisposition)
|
||||
if (matcher.find()) {
|
||||
matcher.group(1)
|
||||
?.replace(".*[/\\\\]", "")
|
||||
?.replace(";", "")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
var prefix: String?
|
||||
val suffix: String?
|
||||
if (fileName == null) {
|
||||
prefix = "download"
|
||||
suffix = ""
|
||||
} else {
|
||||
val pos = fileName.lastIndexOf(".")
|
||||
if (pos == -1) {
|
||||
prefix = fileName
|
||||
suffix = null
|
||||
} else {
|
||||
prefix = fileName.substring(0, pos)
|
||||
suffix = fileName.substring(pos)
|
||||
}
|
||||
// Files.createTempFile requires the prefix to be at least three characters long
|
||||
if (prefix.length < 3) {
|
||||
prefix = "download"
|
||||
}
|
||||
}
|
||||
|
||||
val tempFile = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
java.nio.file.Files.createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null).toFile()
|
||||
java.nio.file.Files.createTempFile(prefix, suffix).toFile()
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null)
|
||||
createTempFile(prefix, suffix)
|
||||
}
|
||||
tempFile.deleteOnExit()
|
||||
body.byteStream().use { inputStream ->
|
||||
@@ -238,7 +277,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
|
||||
it.headers.toMultimap()
|
||||
)
|
||||
it.isSuccessful -> Success(
|
||||
responseBody(it.body, accept),
|
||||
responseBody(it, accept),
|
||||
it.code,
|
||||
it.headers.toMultimap()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user