[kotlin-client][jvm-spring-restclient] Extract data from PartConfig for multipart/form-data requests (#20598)

This commit is contained in:
Dimitar Tomov 2025-02-06 09:43:59 +02:00 committed by GitHub
parent 9a9c1087be
commit c74dfa3b3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 6 deletions

View File

@ -46,8 +46,23 @@ import org.springframework.util.LinkedMultiValueMap
private fun <I> RestClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) body(requestConfig.body) }
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val parts = LinkedMultiValueMap<String, Any>()
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
if (part.body != null) {
parts.add(name, part.body)
}
}
return apply { body(parts) }
}
else -> {
return apply { if (requestConfig.body != null) body(requestConfig.body) }
}
}
}
}
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}inline fun <reified T: Any> parseDateToQueryString(value : T): String {

View File

@ -46,8 +46,23 @@ open class ApiClient(protected val client: RestClient) {
private fun <I> RestClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) body(requestConfig.body) }
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val parts = LinkedMultiValueMap<String, Any>()
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
if (part.body != null) {
parts.add(name, part.body)
}
}
return apply { body(parts) }
}
else -> {
return apply { if (requestConfig.body != null) body(requestConfig.body) }
}
}
}
}
inline fun <reified T: Any> parseDateToQueryString(value : T): String {

View File

@ -46,8 +46,23 @@ open class ApiClient(protected val client: RestClient) {
private fun <I> RestClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) body(requestConfig.body) }
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val parts = LinkedMultiValueMap<String, Any>()
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
if (part.body != null) {
parts.add(name, part.body)
}
}
return apply { body(parts) }
}
else -> {
return apply { if (requestConfig.body != null) body(requestConfig.body) }
}
}
}
}
inline fun <reified T: Any> parseDateToQueryString(value : T): String {