javareactive-programmingspring-webfluxspring-mono

How to solve the, calling for Mono<Token> then the result will be used to another Mono<Collection>, which will then return Mono<collection>?


Hi I just started learning reactive programming

I have this piece of code here and my process here should be I will call tokenRepository to get the token and then, use the token.getAccessToken() to be used as a parameter on the cardRepository.findAllCards()

public class CardService {

    private final CardRepository cardRepository;
    private final TokenRepository tokenRepository;

    public CardService(CardRepositorycardRepository,
                       TokenRepository tokenRepository) {
        this.cardRepository = cardRepository;
        this.tokenRepository = tokenRepository;
    }

    public Mono<CardCollection> findAllCards(MultiValueMap<String, String> queryParams) {
        Mono<Token> token =tokenRepository.requestToken(); 

        // then I would like to use the token.getAccessToken
        return cardRepository.findAllCards(token.getAccessToken, queryParams); // Then this should return Mono<CardCollection>
    }
}

Would like to know if this is possible?


Solution

  • I found the answer although I'm not quite sure if this is the correct way.

    How to pass data down the reactive chain

    This is what I've done with my code.

    public Mono<CardCollection> findAllCards(MultiValueMap<String, String> queryParams) {
      return tokenRepository.requestToken().flatMap(token -> {
          return cardRepository.findAllCards(token.getAccessToken(), queryParams);
      });
    }