0

I have 3 reactive rest client functions getOne(): Uni<String>, getTwo(reponseFromOne): Uni<List<Two>>, getThree(responsesFromTwo): Uni<List<Three>>

I want to chain them so the result from getOne is passed as argument to getTwo and results from getTwo are passed as arguments to getThree. So that I can construct objects after all calls are passed

Something like .. the following pseudo code..

val someObjects: Uni<List<SomeObject> =
    getOne().onItem().transformToUni { input -> getTwo(input) }
        .onItem().transformToUni { input2 -> getThree(input2) }
        .map {
            SomeObject(getTwo.something, getThree.something)
        }
Nick
  • 1
  • 1

1 Answers1

0

Nick, hello! You can chain such functions with flatMap. I've tried to implement your example in the format of unit-test.

@Test
void chainTest() {
    var someObject = getOne()
            .flatMap(input1 -> getTwo(input1)
                    .flatMap(input2array -> Multi.createFrom().iterable(input2array)
                            .onItem().transformToUniAndConcatenate(input2 -> getThree(input2)
                                    .map(input3 -> new SomeObject(input1, input2, input3)))
                            .collect().asList()
                    ))
            .await().indefinitely();
}
  • Actually what i want is to call getThree(something: String) for each element of the result of getTwo(). And then create objects List(SomeObject(input1, input2.1, input3.1), SomeObject(input1, input2.1, input3.2)) – Nick Jul 13 '23 at 06:42
  • Sure, I've update my piece of code, you need to use Multi to handle results from getTwo(). There is an option for you either use transformToUniAndConcatenate() or transformToUniAndMerge(). The first method keeps order and the second one calls getThree() with some concurrency. – Kirill Byvshev Jul 13 '23 at 18:23