Skip to content

Commit

Permalink
[kotlin-client][jvm-spring-webclient] Extract data from PartConfig fo…
Browse files Browse the repository at this point in the history
…r multipart/form-data requests (#19811)
  • Loading branch information
nschimmrich authored Oct 10, 2024
1 parent 627c0f4 commit d14eab8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.ResponseEntity
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono

Expand Down Expand Up @@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }

private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.ResponseEntity
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono

Expand Down Expand Up @@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }

private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.ResponseEntity
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono

Expand Down Expand Up @@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }

private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.ResponseEntity
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono

Expand Down Expand Up @@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }

private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
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 {
Expand Down

0 comments on commit d14eab8

Please sign in to comment.