Emmanuel
Emmanuel

Reputation: 53

Is it possible to make subsequent WebClient calls when emitting flux items?

I'm working with an API that only displays an event's venue id in the response when performing an event search API call. I'm looking to see if there's a way to make a Spring WebClient request to fetch the venue information as Flux items are being emitted.

val events =  eventService.fetchEventsByLocation(lat,lon,radius)
            .flatMapIterable { eventResponse ->  EventTransformer.map(eventResponse)}
            .doOnNext { transformedEvent -> this.repository.save(transformedEvent) }


fun fetchEventsByLocation(lat:Double?,lon:Double?,radius:Double?): Mono<EventResponse> {

    val builder = UriComponentsBuilder.fromPath(SEARCH_EVENTS)
            .queryParam("categories", "103")
            .queryParam("location.within", 20.toString() + "mi")
            .queryParam("location.latitude", lat)
            .queryParam("location.longitude", lon)
            .queryParam("token",apiKey)

    return this.webClient.get()
            .uri(builder.toUriString())
            .accept(MediaType.APPLICATION_JSON_UTF8)
            .exchange()
            .flatMap { response -> response.bodyToMono(String::class.java) }
            .map { response -> transform(response) }
}

 fun fetchEventVenue(id:String?): Mono<Venue> {
    val builder = UriComponentsBuilder.fromPath(VENUES + id)
            .queryParam("token",apiKey)

    return this.webClient.get()
            .uri(builder.toUriString())
            .accept(MediaType.APPLICATION_JSON_UTF8)
            .exchange()
            .flatMap { response -> response.bodyToMono(Venue::class.java) }
}

Upvotes: 0

Views: 254

Answers (1)

Michiel
Michiel

Reputation: 3410

doOnNext is an intermediate operation. Without a terminal operation (such as subscribe), the stream isn't consumed.

Upvotes: 1

Related Questions