I am new to Reactive programming and I would like to make two API calls in parallel and process the results and return a simple array or list of items.
I have two functions, one returns a Flux and the other returns a Mono and I make a very simple filtering logic on the Flux emitted items depending on the result of that Mono.
I tried to use zipWith
but only one item made it to the end no matter what filtering logic. Also I tried with block
but that is not allowed inside the controller :/
@GetMapping("/{id}/offers")
fun viewTaskOffers(
@PathVariable("id") id: String,
@AuthenticationPrincipal user: UserPrincipal
) : Flux<ViewOfferDTO> {
data class TaskOfferPair(
val task: TaskDTO,
val offer: ViewOfferDTO
)
return client.getTaskOffers(id).map {
it.toViewOfferDTO()
}.zipWith(client.getTask(id), BiFunction {
offer: ViewOfferDTO, task: TaskDTO -> TaskOfferPair(task, offer)
}).filter {
it.offer.workerUser.id == user.id || it.task.creatorUser == user.id
}.map {
it.offer
}
}
getTaskOffers
returns a Flux ofOfferDTO
getTask
returns a Mono ofTaskDTO
If you cannot answer my question please tell me atleast how to do multiple API calls in parallel and wait for the results in WebClient