ClientResponseExtensions.kt 3.8 KB
Newer Older
1
/*
2
 * Copyright 2002-2019 the original author or authors.
3 4 5 6 7
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
S
Spring Operator 已提交
8
 *      https://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17 18
package org.springframework.web.reactive.function.client

19
import kotlinx.coroutines.ExperimentalCoroutinesApi
20
import kotlinx.coroutines.flow.Flow
21
import kotlinx.coroutines.reactive.awaitFirstOrNull
22
import kotlinx.coroutines.reactive.awaitSingle
23
import kotlinx.coroutines.reactive.asFlow
24
import org.springframework.core.ParameterizedTypeReference
25
import org.springframework.http.ResponseEntity
26 27 28
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono

29
/**
30
 * Extension for [ClientResponse.bodyToMono] providing a `bodyToMono<Foo>()` variant
31 32
 * leveraging Kotlin reified type parameters. This extension is not subject to type
 * erasure and retains actual generic type arguments.
33 34 35 36
 *
 * @author Sebastien Deleuze
 * @since 5.0
 */
37 38
inline fun <reified T : Any> ClientResponse.bodyToMono(): Mono<T> =
		bodyToMono(object : ParameterizedTypeReference<T>() {})
39 40

/**
41
 * Extension for [ClientResponse.bodyToFlux] providing a `bodyToFlux<Foo>()` variant
42 43
 * leveraging Kotlin reified type parameters. This extension is not subject to type
 * erasure and retains actual generic type arguments.
44 45 46 47
 *
 * @author Sebastien Deleuze
 * @since 5.0
 */
48 49
inline fun <reified T : Any> ClientResponse.bodyToFlux(): Flux<T> =
		bodyToFlux(object : ParameterizedTypeReference<T>() {})
50

51 52 53 54 55 56
/**
 * Coroutines [kotlinx.coroutines.flow.Flow] based variant of [ClientResponse.bodyToFlux].
 *
 * @author Sebastien Deleuze
 * @since 5.2
 */
57 58 59
@ExperimentalCoroutinesApi
inline fun <reified T : Any> ClientResponse.bodyToFlow(): Flow<T> =
		bodyToFlux<T>().asFlow()
60

61
/**
62
 * Extension for [ClientResponse.toEntity] providing a `toEntity<Foo>()` variant
63 64
 * leveraging Kotlin reified type parameters. This extension is not subject to type
 * erasure and retains actual generic type arguments.
65 66 67 68
 *
 * @author Sebastien Deleuze
 * @since 5.0
 */
69 70
inline fun <reified T : Any> ClientResponse.toEntity(): Mono<ResponseEntity<T>> =
		toEntity(object : ParameterizedTypeReference<T>() {})
71 72

/**
73
 * Extension for [ClientResponse.toEntityList] providing a `bodyToEntityList<Foo>()` variant
74 75
 * leveraging Kotlin reified type parameters. This extension is not subject to type
 * erasure and retains actual generic type arguments.
76 77 78 79
 *
 * @author Sebastien Deleuze
 * @since 5.0
 */
80 81
inline fun <reified T : Any> ClientResponse.toEntityList(): Mono<ResponseEntity<List<T>>> =
		toEntityList(object : ParameterizedTypeReference<T>() {})
82 83

/**
84
 * Non-nullable Coroutines variant of [ClientResponse.bodyToMono].
85 86 87 88 89 90 91
 *
 * @author Sebastien Deleuze
 * @since 5.2
 */
suspend inline fun <reified T : Any> ClientResponse.awaitBody(): T =
		bodyToMono<T>().awaitSingle()

92 93 94 95 96 97 98 99 100
/**
 * Nullable coroutines variant of [ClientResponse.bodyToMono].
 *
 * @author Sebastien Deleuze
 * @since 5.2
 */
suspend inline fun <reified T : Any> ClientResponse.awaitBodyOrNull(): T? =
		bodyToMono<T>().awaitFirstOrNull()

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/**
 * Coroutines variant of [ClientResponse.toEntity].
 *
 * @author Sebastien Deleuze
 * @since 5.2
 */
suspend inline fun <reified T : Any> ClientResponse.awaitEntity(): ResponseEntity<T> =
		toEntity<T>().awaitSingle()

/**
 * Coroutines variant of [ClientResponse.toEntityList].
 *
 * @author Sebastien Deleuze
 * @since 5.2
 */
suspend inline fun <reified T : Any> ClientResponse.awaitEntityList(): ResponseEntity<List<T>> =
		toEntityList<T>().awaitSingle()