Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Communication] - Common - Addressing ApiView Comments #19959

Merged
merged 5 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.function.Supplier;

import com.azure.communication.common.implementation.TokenParser;

Expand All @@ -28,7 +29,7 @@ public final class CommunicationTokenCredential implements AutoCloseable {

private AccessToken accessToken;
private final TokenParser tokenParser = new TokenParser();
private TokenRefresher refresher;
private Supplier<Mono<String>> refresher;
private FetchingTask fetchingTask;
private boolean isClosed = false;

Expand All @@ -52,7 +53,7 @@ public CommunicationTokenCredential(String token) {
* @param tokenRefreshOptions implementation to supply fresh token when reqested
*/
public CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefreshOptions) {
TokenRefresher tokenRefresher = tokenRefreshOptions.getTokenRefresher();
Supplier<Mono<String>> tokenRefresher = tokenRefreshOptions.getTokenRefresher();
Objects.requireNonNull(tokenRefresher, "'tokenRefresher' cannot be null.");
refresher = tokenRefresher;
if (tokenRefreshOptions.getToken() != null) {
Expand Down Expand Up @@ -114,10 +115,10 @@ private void setToken(String freshToken) {
}

private Mono<String> fetchFreshToken() {
Mono<String> tokenAsync = refresher.getToken();
Mono<String> tokenAsync = refresher.get();
if (tokenAsync == null) {
return FluxUtil.monoError(logger,
new RuntimeException("TokenRefresher returned null when getTokenAsync is called"));
new RuntimeException("get() function for token refresher is null"));
}
return tokenAsync;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// Licensed under the MIT License.
package com.azure.communication.common;

import java.util.function.Supplier;
import reactor.core.publisher.Mono;

/**
* Options for refreshing CommunicationTokenCredential
*/
public final class CommunicationTokenRefreshOptions {
private final TokenRefresher tokenRefresher;
private final Supplier<Mono<String>> tokenRefresher;
private final boolean refreshProactively;
private final String initialToken;

Expand All @@ -19,7 +22,7 @@ public final class CommunicationTokenRefreshOptions {
* with setCallbackOffsetMinutes or default value of
* two minutes
*/
public CommunicationTokenRefreshOptions(TokenRefresher tokenRefresher, boolean refreshProactively) {
public CommunicationTokenRefreshOptions(Supplier<Mono<String>> tokenRefresher, boolean refreshProactively) {
this.tokenRefresher = tokenRefresher;
this.refreshProactively = refreshProactively;
this.initialToken = null;
Expand All @@ -35,7 +38,7 @@ public CommunicationTokenRefreshOptions(TokenRefresher tokenRefresher, boolean r
* two minutes
* @param initialToken the optional serialized JWT token
*/
public CommunicationTokenRefreshOptions(TokenRefresher tokenRefresher, boolean refreshProactively, String initialToken) {
public CommunicationTokenRefreshOptions(Supplier<Mono<String>> tokenRefresher, boolean refreshProactively, String initialToken) {
this.tokenRefresher = tokenRefresher;
this.refreshProactively = refreshProactively;
this.initialToken = initialToken;
Expand All @@ -44,7 +47,7 @@ public CommunicationTokenRefreshOptions(TokenRefresher tokenRefresher, boolean r
/**
* @return the token refresher to provide capacity to fetch fresh token
*/
public TokenRefresher getTokenRefresher() {
public Supplier<Mono<String>> getTokenRefresher() {
return tokenRefresher;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;

public class CommunicationTokenCredentialTests {
private final JwtTokenMocker tokenMocker = new JwtTokenMocker();
Expand Down Expand Up @@ -55,7 +56,7 @@ public void constructWithExpiredTokenWithoutRefresher()
tokenCredential.close();
}

class MockImmediateRefresher implements TokenRefresher {
class MockImmediateRefresher implements Supplier<Mono<String>> {
private int numCalls = 0;
private Runnable onCallReturn;

Expand All @@ -72,7 +73,7 @@ public void resetCallCount() {
}

@Override
public Mono<String> getToken() {
public Mono<String> get() {
numCalls++;
if (this.onCallReturn != null) {
this.onCallReturn.run();
Expand Down Expand Up @@ -237,7 +238,7 @@ public void shouldStopRefreshTimerWhenClosed() throws InterruptedException, Exec
assertFalse(tokenCredential.hasProactiveFetcher());
}

class ExceptionRefresher implements TokenRefresher {
class ExceptionRefresher implements Supplier<Mono<String>> {
private int numCalls;
private Runnable onCallReturn;

Expand All @@ -254,7 +255,7 @@ public void resetCallCount() {
}

@Override
public Mono<String> getToken() {
public Mono<String> get() {
numCalls++;
if (this.onCallReturn != null) {
this.onCallReturn.run();
Expand Down