Skip to content

Commit

Permalink
[Communication] - Common - Addressing ApiView Comments (#19959)
Browse files Browse the repository at this point in the history
* Addressing Common ApiView Comments

* Addressing Common ApiView Comments

* Address comments

* Address more comments

* Updating javadoc as per comment

Co-authored-by: Minnie Liu <peiliu@microsoft.com>
  • Loading branch information
minnieliu and Minnie Liu authored Mar 19, 2021
1 parent a028adc commit 4c4f4cc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 33 deletions.
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,11 +53,11 @@ 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) {
setToken(tokenRefreshOptions.getToken());
if (tokenRefreshOptions.getInitialToken() != null) {
setToken(tokenRefreshOptions.getInitialToken());
if (tokenRefreshOptions.isRefreshProactively()) {
OffsetDateTime nextFetchTime = accessToken.getExpiresAt().minusMinutes(DEFAULT_EXPIRING_OFFSET_MINUTES);
fetchingTask = new FetchingTask(this, nextFetchTime);
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 of the token refresher should not return 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 All @@ -56,9 +59,9 @@ public boolean isRefreshProactively() {
}

/**
* @return the serialized JWT token
* @return the initial token
*/
public String getToken() {
public String getInitialToken() {
return initialToken;
}
}

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

0 comments on commit 4c4f4cc

Please sign in to comment.