|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.messaging.rsocket |
| 18 | + |
| 19 | +import io.rsocket.transport.ClientTransport |
| 20 | +import kotlinx.coroutines.FlowPreview |
| 21 | +import kotlinx.coroutines.flow.Flow |
| 22 | +import kotlinx.coroutines.reactive.awaitFirstOrNull |
| 23 | +import kotlinx.coroutines.reactive.awaitSingle |
| 24 | +import kotlinx.coroutines.reactive.flow.asFlow |
| 25 | +import kotlinx.coroutines.reactive.flow.asPublisher |
| 26 | +import org.springframework.core.ParameterizedTypeReference |
| 27 | +import java.net.URI |
| 28 | + |
| 29 | +/** |
| 30 | + * Coroutines variant of [RSocketRequester.Builder.connect]. |
| 31 | + * |
| 32 | + * @author Sebastien Deleuze |
| 33 | + * @since 5.2 |
| 34 | + */ |
| 35 | +suspend fun RSocketRequester.Builder.connectAndAwait(transport: ClientTransport): RSocketRequester = |
| 36 | + connect(transport).awaitSingle() |
| 37 | + |
| 38 | +/** |
| 39 | + * Coroutines variant of [RSocketRequester.Builder.connectTcp]. |
| 40 | + * |
| 41 | + * @author Sebastien Deleuze |
| 42 | + * @since 5.2 |
| 43 | + */ |
| 44 | +suspend fun RSocketRequester.Builder.connectTcpAndAwait(host: String, port: Int): RSocketRequester = |
| 45 | + connectTcp(host, port).awaitSingle() |
| 46 | + |
| 47 | +/** |
| 48 | + * Coroutines variant of [RSocketRequester.Builder.connectWebSocket]. |
| 49 | + * |
| 50 | + * @author Sebastien Deleuze |
| 51 | + * @since 5.2 |
| 52 | + */ |
| 53 | +suspend fun RSocketRequester.Builder.connectWebSocketAndAwait(uri: URI): RSocketRequester = |
| 54 | + connectWebSocket(uri).awaitSingle() |
| 55 | + |
| 56 | + |
| 57 | +/** |
| 58 | + * Kotlin [Flow] variant of [RSocketRequester.RequestSpec.data]. |
| 59 | + * |
| 60 | + * @author Sebastien Deleuze |
| 61 | + * @since 5.2 |
| 62 | + */ |
| 63 | +@FlowPreview |
| 64 | +fun <T : Any> RSocketRequester.RequestSpec.dataFlow(data: Flow<T>): RSocketRequester.ResponseSpec = |
| 65 | + data(data.asPublisher(), object : ParameterizedTypeReference<T>() {}) |
| 66 | + |
| 67 | +/** |
| 68 | + * Coroutines variant of [RSocketRequester.ResponseSpec.send]. |
| 69 | + * |
| 70 | + * @author Sebastien Deleuze |
| 71 | + * @since 5.2 |
| 72 | + */ |
| 73 | +suspend fun RSocketRequester.ResponseSpec.sendAndAwait() { |
| 74 | + send().awaitFirstOrNull() |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Coroutines variant of [RSocketRequester.ResponseSpec.retrieveMono]. |
| 79 | + * |
| 80 | + * @author Sebastien Deleuze |
| 81 | + * @since 5.2 |
| 82 | + */ |
| 83 | +suspend fun <T : Any> RSocketRequester.ResponseSpec.retrieveAndAwait(): T = |
| 84 | + retrieveMono(object : ParameterizedTypeReference<T>() {}).awaitSingle() |
| 85 | + |
| 86 | +/** |
| 87 | + * Coroutines variant of [RSocketRequester.ResponseSpec.retrieveFlux]. |
| 88 | + * |
| 89 | + * @author Sebastien Deleuze |
| 90 | + * @since 5.2 |
| 91 | + */ |
| 92 | +@FlowPreview |
| 93 | +fun <T : Any> RSocketRequester.ResponseSpec.retrieveFlow(batchSize: Int = 1): Flow<T> = |
| 94 | + retrieveFlux(object : ParameterizedTypeReference<T>() {}).asFlow(batchSize) |
0 commit comments