forked from loafle/openapi-generator-original
This reverts commit 71ccc88037216d973b0df12d4b6c618ae00b355b.
This commit is contained in:
parent
6ba311e85c
commit
8035da8639
@ -114,48 +114,6 @@ import com.squareup.moshi.adapter
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -87,48 +87,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -88,48 +88,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -86,48 +86,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
|
|||||||
return contentType ?: "application/octet-stream"
|
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 =
|
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
||||||
when {
|
when {
|
||||||
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
|
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<*>>
|
// content's type *must* be Map<String, PartConfig<*>>
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
||||||
when (part.body) {
|
if (part.body is File) {
|
||||||
is File -> addPartToMultiPart(name, part.headers, part.body)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
is List<*> -> {
|
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
||||||
part.body.forEach {
|
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
||||||
if (it is File) {
|
addPart(
|
||||||
addPartToMultiPart(name, part.headers, it)
|
partHeaders.toHeaders(),
|
||||||
|
part.body.asRequestBody(fileMediaType)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
addPartToMultiPart(name, part.headers, it)
|
val partHeaders = part.headers.toMutableMap() +
|
||||||
}
|
("Content-Disposition" to "form-data; name=\"$name\"")
|
||||||
}
|
addPart(
|
||||||
}
|
partHeaders.toHeaders(),
|
||||||
else -> addPartToMultiPart(name, part.headers, part.body)
|
parameterToString(part.body).toRequestBody(null)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.build()
|
}.build()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user