How can I flatMapMany with multiple web calls and conditionally signal complete?

249 Views Asked by At

I need to write a method which does

  • Gets a Location header form an endpoint
  • Generates a series of Some which each should retrieved from the Location fetched from the first endpoint.
  • Need to return a Flux<Some>

Which looks like this.

private WebClient client;

Flux<Some> getSome() {

    // Get the Location header from an endpoint
    client
            .post()
            .exchange()
            .map(r -> r.headers().header("Location"))

    // Now I need to keep invoking the Location endpoint for Some.class
    // And Some.class has an attribute for pending/completion status.

    // e.g.
    while (true)
        final Some some = client
            .get()
            .url(...) // the Location header value above
            .retrieve()
            .bodyToMono(Some.class)
            .block()
            ;
    }

}

How can I map the Mono<String> to a Flux<Some> while using the Some#status attribute for completion?

    // should return Flux<Some>
    client
            .post()
            .exchange()
            .map(r -> r.headers().header("Location"))
            .flatMapMany(location -> {
                // invokes location while retrieved Some#status is not `completed`
                // @@?
            })
            ;

1

There are 1 best solutions below

1
On

inside the flatMapMany, perform the call that retrieves the Location. apply a repeat().takeUntil(loc -> loc.complete()) to it (still inside the flatMapMany lambda)