Last active
November 12, 2019 10:40
-
-
Save pettan93/e0915a062e638cee08f6e1ce5a04119b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
@AllArgsConstructor | |
@Slf4j | |
public class TokenService { | |
private final TokenRepository tokenRepository; | |
private final TokenObtainer tokenObtainer; | |
public void refreshTokens() { | |
Arrays.stream(RemoteService.values()) | |
.forEach(remoteService -> | |
tokenRepository.findById(remoteService) | |
.ifPresentOrElse( | |
apiToken -> { | |
log.info("Refreshing token for API [" + remoteService + "]"); | |
tokenRepository.save( | |
tokenObtainer.obtainAccessToken(remoteService, apiToken)); | |
}, | |
() -> { | |
log.warn("Token for API [" + remoteService + "] wasn't found"); | |
log.info("Obtaining new token for API [" + remoteService + "]"); | |
tokenRepository.save( | |
tokenObtainer.obtainAccessToken(remoteService) | |
); | |
} | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment