Refine WebFlux Coroutines API

This commit provides both nullable and non-nullable variants for
awaitBody, makes awaitPrincipal return type nullable and rename
awaitResponse to awaitExchange for better consistency with Java API.

See gh-19975
This commit is contained in:
Sebastien Deleuze
2019-03-28 10:17:12 +01:00
parent 811f315440
commit c5c4ac164b
6 changed files with 39 additions and 10 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.web.reactive.function.client
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactive.awaitSingle
import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.ResponseEntity
@@ -67,7 +68,7 @@ inline fun <reified T : Any> ClientResponse.toEntityList(): Mono<ResponseEntity<
toEntityList(object : ParameterizedTypeReference<T>() {})
/**
* Coroutines variant of [ClientResponse.bodyToMono].
* Non-nullable Coroutines variant of [ClientResponse.bodyToMono].
*
* @author Sebastien Deleuze
* @since 5.2
@@ -75,6 +76,15 @@ inline fun <reified T : Any> ClientResponse.toEntityList(): Mono<ResponseEntity<
suspend inline fun <reified T : Any> ClientResponse.awaitBody(): T =
bodyToMono<T>().awaitSingle()
/**
* Nullable coroutines variant of [ClientResponse.bodyToMono].
*
* @author Sebastien Deleuze
* @since 5.2
*/
suspend inline fun <reified T : Any> ClientResponse.awaitBodyOrNull(): T? =
bodyToMono<T>().awaitFirstOrNull()
/**
* Coroutines variant of [ClientResponse.toEntity].
*

View File

@@ -67,7 +67,7 @@ inline fun <reified T : Any> WebClient.ResponseSpec.bodyToFlux(): Flux<T> =
* @author Sebastien Deleuze
* @since 5.2
*/
suspend fun WebClient.RequestHeadersSpec<out WebClient.RequestHeadersSpec<*>>.awaitResponse(): ClientResponse =
suspend fun WebClient.RequestHeadersSpec<out WebClient.RequestHeadersSpec<*>>.awaitExchange(): ClientResponse =
exchange().awaitSingle()
/**

View File

@@ -49,12 +49,21 @@ inline fun <reified T : Any> ServerRequest.bodyToFlux(): Flux<T> =
bodyToFlux(object : ParameterizedTypeReference<T>() {})
/**
* Coroutines variant of [ServerRequest.bodyToMono].
* Non-nullable Coroutines variant of [ServerRequest.bodyToMono].
*
* @author Sebastien Deleuze
* @since 5.2
*/
suspend inline fun <reified T : Any> ServerRequest.awaitBody(): T? =
suspend inline fun <reified T : Any> ServerRequest.awaitBody(): T =
bodyToMono<T>().awaitSingle()
/**
* Nullable Coroutines variant of [ServerRequest.bodyToMono].
*
* @author Sebastien Deleuze
* @since 5.2
*/
suspend inline fun <reified T : Any> ServerRequest.awaitBodyOrNull(): T? =
bodyToMono<T>().awaitFirstOrNull()
/**
@@ -81,8 +90,8 @@ suspend fun ServerRequest.awaitMultipartData(): MultiValueMap<String, Part> =
* @author Sebastien Deleuze
* @since 5.2
*/
suspend fun ServerRequest.awaitPrincipal(): Principal =
principal().awaitSingle()
suspend fun ServerRequest.awaitPrincipal(): Principal? =
principal().awaitFirstOrNull()
/**
* Coroutines variant of [ServerRequest.session].