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

This commit is contained in:
Norman Schimmrich 2024-10-10 07:41:55 +02:00 committed by GitHub
parent 627c0f43cd
commit d14eab8446
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 76 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
import org.springframework.http.MediaType import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.util.LinkedMultiValueMap import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) = private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } } apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) = private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) } when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val builder = MultipartBodyBuilder()
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
if (part.body != null) {
val partBuilder = builder.part(name, part.body)
val partHeaders = part.headers
partHeaders.forEach { partBuilder.header(it.key, it.value) }
}
}
return apply { bodyValue(builder.build()) }
}
else -> {
return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
}
}
}
} }
inline fun <reified T: Any> parseDateToQueryString(value : T): String { inline fun <reified T: Any> parseDateToQueryString(value : T): String {

View File

@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
import org.springframework.http.MediaType import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.util.LinkedMultiValueMap import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) = private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } } apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) = private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) } when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val builder = MultipartBodyBuilder()
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
if (part.body != null) {
val partBuilder = builder.part(name, part.body)
val partHeaders = part.headers
partHeaders.forEach { partBuilder.header(it.key, it.value) }
}
}
return apply { bodyValue(builder.build()) }
}
else -> {
return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
}
}
}
} }
inline fun <reified T: Any> parseDateToQueryString(value : T): String { inline fun <reified T: Any> parseDateToQueryString(value : T): String {

View File

@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
import org.springframework.http.MediaType import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.util.LinkedMultiValueMap import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) = private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } } apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) = private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) } when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val builder = MultipartBodyBuilder()
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
if (part.body != null) {
val partBuilder = builder.part(name, part.body)
val partHeaders = part.headers
partHeaders.forEach { partBuilder.header(it.key, it.value) }
}
}
return apply { bodyValue(builder.build()) }
}
else -> {
return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
}
}
}
} }
inline fun <reified T: Any> parseDateToQueryString(value : T): String { inline fun <reified T: Any> parseDateToQueryString(value : T): String {

View File

@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
import org.springframework.http.MediaType import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.util.LinkedMultiValueMap import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) = private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } } apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) = private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) } when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val builder = MultipartBodyBuilder()
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
if (part.body != null) {
val partBuilder = builder.part(name, part.body)
val partHeaders = part.headers
partHeaders.forEach { partBuilder.header(it.key, it.value) }
}
}
return apply { bodyValue(builder.build()) }
}
else -> {
return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
}
}
}
} }
inline fun <reified T: Any> parseDateToQueryString(value : T): String { inline fun <reified T: Any> parseDateToQueryString(value : T): String {