Revert "[Kotlin] fix #20231, OkHttp client can handle a field with a list of…" (#20257)

This reverts commit 71ccc88037216d973b0df12d4b6c618ae00b355b.
This commit is contained in:
William Cheng 2024-12-05 17:04:42 +08:00 committed by GitHub
parent 6ba311e85c
commit 8035da8639
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
22 changed files with 330 additions and 1188 deletions

View File

@ -114,48 +114,6 @@ import com.squareup.moshi.adapter
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -167,18 +125,21 @@ import com.squareup.moshi.adapter
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -87,48 +87,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -140,18 +98,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -88,48 +88,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -141,18 +99,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -86,48 +86,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -139,18 +97,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()

View File

@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
return contentType ?: "application/octet-stream"
}
/**
* Adds a File to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param file The file that will be added as the field value
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, file: File) {
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"")
val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
file.asRequestBody(fileMediaType)
)
}
/**
* Adds any type to a MultipartBody.Builder
* Defined a helper in the requestBody method to not duplicate code
* It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File.
*
* @param name The field name to add in the request
* @param headers The headers that are in the PartConfig
* @param obj The field name to add in the request
* @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on
* @see requestBody
*/
protected fun <T> MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map<String, String>, obj: T?) {
if (obj == null) return
val partHeaders = headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(obj).toRequestBody(null)
)
}
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
// content's type *must* be Map<String, PartConfig<*>>
@Suppress("UNCHECKED_CAST")
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
when (part.body) {
is File -> addPartToMultiPart(name, part.headers, part.body)
is List<*> -> {
part.body.forEach {
if (it is File) {
addPartToMultiPart(name, part.headers, it)
} else {
addPartToMultiPart(name, part.headers, it)
}
}
}
else -> addPartToMultiPart(name, part.headers, part.body)
if (part.body is File) {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
addPart(
partHeaders.toHeaders(),
part.body.asRequestBody(fileMediaType)
)
} else {
val partHeaders = part.headers.toMutableMap() +
("Content-Disposition" to "form-data; name=\"$name\"")
addPart(
partHeaders.toHeaders(),
parameterToString(part.body).toRequestBody(null)
)
}
}
}.build()