Skip to content

Commit

Permalink
Uptaking review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AsabuHere committed Jun 21, 2024
1 parent 8716eb1 commit a4ab362
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 43 deletions.
4 changes: 2 additions & 2 deletions examples/BearerTokenAuthentication.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class BearerTokenAuthenticationExamples {
public static void main {
//Getting access token - Method #1
TwilioBearerTokenAuth.init(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET);
TwilioOrgsTokenAuth.init(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET);

//Getting access token - Method #2
//To provide custom token manager implementation
//Need not call init method if customer token manager is passed
//TwilioBearerTokenAuth.setTokenManager(new OrgsTokenManager(grantType, clientId, clientSecret));
//TwilioOrgsTokenAuth.setTokenManager(new OrgsTokenManager(grantType, clientId, clientSecret));

fetchAccountDetails();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.twilio.http.bearertoken.OrgsTokenManager;

@Preview
public class TwilioBearerTokenAuth {
public class TwilioOrgsTokenAuth {
private static String accessToken;
@Getter
private static List<String> userAgentExtensions;
Expand All @@ -25,7 +25,7 @@ public class TwilioBearerTokenAuth {

private static volatile ExecutorService executorService;

private TwilioBearerTokenAuth() {
private TwilioOrgsTokenAuth() {
}

public static synchronized void init(String grantType, String clientId, String clientSecret) {
Expand All @@ -51,43 +51,45 @@ private static void validateAuthCredentials(String grantType, String clientId, S
}

public static BearerTokenTwilioRestClient getRestClient() {
if (TwilioBearerTokenAuth.restClient == null) {
synchronized (TwilioBearerTokenAuth.class) {
if (TwilioBearerTokenAuth.restClient == null) {
TwilioBearerTokenAuth.restClient = buildOAuthRestClient();
if (TwilioOrgsTokenAuth.restClient == null) {
synchronized (TwilioOrgsTokenAuth.class) {
if (TwilioOrgsTokenAuth.restClient == null) {
TwilioOrgsTokenAuth.restClient = buildOAuthRestClient();
}
}
}
return TwilioBearerTokenAuth.restClient;
return TwilioOrgsTokenAuth.restClient;
}
/**
* Returns the Twilio executor service.
*
* @return the Twilio executor service
*/
public static ExecutorService getExecutorService() {
if (TwilioBearerTokenAuth.executorService == null) {
synchronized (TwilioBearerTokenAuth.class) {
if (TwilioBearerTokenAuth.executorService == null) {
TwilioBearerTokenAuth.executorService = Executors.newCachedThreadPool();
if (TwilioOrgsTokenAuth.executorService == null) {
synchronized (TwilioOrgsTokenAuth.class) {
if (TwilioOrgsTokenAuth.executorService == null) {
TwilioOrgsTokenAuth.executorService = Executors.newCachedThreadPool();
}
}
}
return TwilioBearerTokenAuth.executorService;
return TwilioOrgsTokenAuth.executorService;
}

private static BearerTokenTwilioRestClient buildOAuthRestClient() {

BearerTokenTwilioRestClient.Builder builder = new BearerTokenTwilioRestClient.Builder();

if (userAgentExtensions != null) {
builder.userAgentExtensions(TwilioBearerTokenAuth.userAgentExtensions);
builder.userAgentExtensions(TwilioOrgsTokenAuth.userAgentExtensions);
}

builder.region(TwilioBearerTokenAuth.region);
builder.edge(TwilioBearerTokenAuth.edge);
builder.tokenManager(TwilioBearerTokenAuth.tokenManager);
builder.tokenManager(TwilioBearerTokenAuth.tokenManager);
builder.region(TwilioOrgsTokenAuth.region);
builder.edge(TwilioOrgsTokenAuth.edge);
if(TwilioOrgsTokenAuth.tokenManager == null){
tokenManager = new OrgsTokenManager(grantType, clientId, clientSecret, code, redirectUri, audience, refreshToken, scope);
}
builder.tokenManager(TwilioOrgsTokenAuth.tokenManager);

return builder.build();
}
Expand All @@ -96,7 +98,8 @@ private static BearerTokenTwilioRestClient buildOAuthRestClient() {
* Invalidates the volatile state held in the Twilio singleton.
*/
private static void invalidate() {
TwilioBearerTokenAuth.restClient = null;
TwilioOrgsTokenAuth.restClient = null;
TwilioOrgsTokenAuth.tokenManager = null;
}


Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/twilio/base/bearertoken/Creator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.twilio.base.bearertoken;

import com.twilio.TwilioBearerTokenAuth;
import com.twilio.TwilioOrgsTokenAuth;
import com.twilio.http.bearertoken.BearerTokenTwilioRestClient;

import java.util.concurrent.CompletableFuture;
Expand All @@ -18,7 +18,7 @@ public abstract class Creator<T extends Resource> {
* @return future that resolves to requested object
*/
public CompletableFuture<T> createAsync() {
return createAsync(TwilioBearerTokenAuth.getRestClient());
return createAsync(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand All @@ -28,7 +28,7 @@ public CompletableFuture<T> createAsync() {
* @return future that resolves to requested object
*/
public CompletableFuture<T> createAsync(final BearerTokenTwilioRestClient client) {
return CompletableFuture.supplyAsync(() -> create(client), TwilioBearerTokenAuth.getExecutorService());
return CompletableFuture.supplyAsync(() -> create(client), TwilioOrgsTokenAuth.getExecutorService());
}

/**
Expand All @@ -37,7 +37,7 @@ public CompletableFuture<T> createAsync(final BearerTokenTwilioRestClient client
* @return Requested object
*/
public T create() {
return create(TwilioBearerTokenAuth.getRestClient());
return create(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/twilio/base/bearertoken/Deleter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.twilio.base.bearertoken;

import com.twilio.Twilio;
import com.twilio.TwilioBearerTokenAuth;
import com.twilio.TwilioOrgsTokenAuth;
import com.twilio.http.bearertoken.BearerTokenTwilioRestClient;

import java.util.concurrent.CompletableFuture;
Expand All @@ -19,7 +19,7 @@ public abstract class Deleter<T extends Resource> {
* @return future that resolves to true if the object was deleted
*/
public CompletableFuture<Boolean> deleteAsync() {
return deleteAsync(TwilioBearerTokenAuth.getRestClient());
return deleteAsync(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand All @@ -38,7 +38,7 @@ public CompletableFuture<Boolean> deleteAsync(final BearerTokenTwilioRestClient
* @return true if the object was deleted
*/
public boolean delete() {
return delete(TwilioBearerTokenAuth.getRestClient());
return delete(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/twilio/base/bearertoken/Fetcher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.twilio.base.bearertoken;

import com.twilio.TwilioBearerTokenAuth;
import com.twilio.TwilioOrgsTokenAuth;
import com.twilio.http.bearertoken.BearerTokenTwilioRestClient;

import java.util.concurrent.CompletableFuture;
Expand All @@ -18,7 +18,7 @@ public abstract class Fetcher<T extends Resource> {
* @return future that resolves to requested object
*/
public CompletableFuture<T> fetchAsync() {
return fetchAsync(TwilioBearerTokenAuth.getRestClient());
return fetchAsync(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand All @@ -28,7 +28,7 @@ public CompletableFuture<T> fetchAsync() {
* @return future that resolves to requested object
*/
public CompletableFuture<T> fetchAsync(final BearerTokenTwilioRestClient client) {
return CompletableFuture.supplyAsync(() -> fetch(client), TwilioBearerTokenAuth.getExecutorService());
return CompletableFuture.supplyAsync(() -> fetch(client), TwilioOrgsTokenAuth.getExecutorService());
}

/**
Expand All @@ -37,7 +37,7 @@ public CompletableFuture<T> fetchAsync(final BearerTokenTwilioRestClient client)
* @return Requested object
*/
public T fetch() {
return fetch(TwilioBearerTokenAuth.getRestClient());
return fetch(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/twilio/base/bearertoken/Reader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.twilio.base.bearertoken;

import com.twilio.TwilioBearerTokenAuth;
import com.twilio.TwilioOrgsTokenAuth;
import com.twilio.http.bearertoken.BearerTokenTwilioRestClient;

import java.util.concurrent.CompletableFuture;
Expand All @@ -21,7 +21,7 @@ public abstract class Reader<T extends Resource> {
* @return ResourceSet of objects
*/
public ResourceSet<T> read() {
return read(TwilioBearerTokenAuth.getRestClient());
return read(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand All @@ -38,7 +38,7 @@ public ResourceSet<T> read() {
* @return future that resolves to the ResourceSet of objects
*/
public CompletableFuture<ResourceSet<T>> readAsync() {
return readAsync(TwilioBearerTokenAuth.getRestClient());
return readAsync(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand All @@ -48,7 +48,7 @@ public CompletableFuture<ResourceSet<T>> readAsync() {
* @return future that resolves to the ResourceSet of objects
*/
public CompletableFuture<ResourceSet<T>> readAsync(final BearerTokenTwilioRestClient client) {
return CompletableFuture.supplyAsync(() -> read(client), TwilioBearerTokenAuth.getExecutorService());
return CompletableFuture.supplyAsync(() -> read(client), TwilioOrgsTokenAuth.getExecutorService());
}

/**
Expand All @@ -57,7 +57,7 @@ public CompletableFuture<ResourceSet<T>> readAsync(final BearerTokenTwilioRestCl
* @return Page containing the first pageSize of resources
*/
public Page<T> firstPage() {
return firstPage(TwilioBearerTokenAuth.getRestClient());
return firstPage(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand All @@ -75,7 +75,7 @@ public Page<T> firstPage() {
* @return Page containing the target pageSize of resources
*/
public Page<T> getPage(final String targetUrl) {
return getPage(targetUrl, TwilioBearerTokenAuth.getRestClient());
return getPage(targetUrl, TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand All @@ -94,7 +94,7 @@ public Page<T> getPage(final String targetUrl) {
* @return Page containing the next pageSize of resources
*/
public Page<T> nextPage(final Page<T> page) {
return nextPage(page, TwilioBearerTokenAuth.getRestClient());
return nextPage(page, TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand All @@ -113,7 +113,7 @@ public Page<T> nextPage(final Page<T> page) {
* @return Page containing the previous pageSize of resources
*/
public Page<T> previousPage(final Page<T> page) {
return previousPage(page, TwilioBearerTokenAuth.getRestClient());
return previousPage(page, TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/twilio/base/bearertoken/Updater.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.twilio.base.bearertoken;

import com.twilio.TwilioBearerTokenAuth;
import com.twilio.TwilioOrgsTokenAuth;
import com.twilio.http.bearertoken.BearerTokenTwilioRestClient;

import java.util.concurrent.CompletableFuture;
Expand All @@ -18,7 +18,7 @@ public abstract class Updater<T extends Resource> {
* @return future that resolves to requested object
*/
public CompletableFuture<T> updateAsync() {
return updateAsync(TwilioBearerTokenAuth.getRestClient());
return updateAsync(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand All @@ -28,7 +28,7 @@ public CompletableFuture<T> updateAsync() {
* @return future that resolves to requested object
*/
public CompletableFuture<T> updateAsync(final BearerTokenTwilioRestClient client) {
return CompletableFuture.supplyAsync(() -> update(client), TwilioBearerTokenAuth.getExecutorService());
return CompletableFuture.supplyAsync(() -> update(client), TwilioOrgsTokenAuth.getExecutorService());
}

/**
Expand All @@ -37,7 +37,7 @@ public CompletableFuture<T> updateAsync(final BearerTokenTwilioRestClient client
* @return Requested object
*/
public T update() {
return update(TwilioBearerTokenAuth.getRestClient());
return update(TwilioOrgsTokenAuth.getRestClient());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public synchronized String fetchAccessToken(){
Token token;
try {
token = tokenCreator.create();
if(token == null || token.getAccessToken() == null){
throw new ApiException("Token creation failed");
}
} catch(Exception e){
throw new ApiException("Token creation failed");
}
Expand Down

0 comments on commit a4ab362

Please sign in to comment.