Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 23 additions & 3 deletions src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public static ManagementAPI.Builder newBuilder(String domain, String apiToken) {
return new ManagementAPI.Builder(domain, apiToken);
}

/**
* Instantiate a new {@link Builder} to configure and build a new ManagementAPI client.
*
* @param domain the tenant's domain. Must be a non-null valid HTTPS domain.
* @param tokenProvider the API Token provider to use when making requests.
* @return a Builder for further configuration.
*/
public static ManagementAPI.Builder newBuilder(String domain, TokenProvider tokenProvider) {
return new ManagementAPI.Builder(domain, tokenProvider);
}

private ManagementAPI(String domain, TokenProvider tokenProvider, Auth0HttpClient httpClient) {
Asserts.assertNotNull(domain, "domain");
Asserts.assertNotNull(tokenProvider, "token provider");
Expand Down Expand Up @@ -409,7 +420,7 @@ public NetworkAclsEntity networkAcls() {
*/
public static class Builder {
private final String domain;
private final String apiToken;
private final TokenProvider tokenProvider;
private Auth0HttpClient httpClient = DefaultHttpClient.newBuilder().build();

/**
Expand All @@ -418,8 +429,17 @@ public static class Builder {
* @param apiToken the API token used to make requests to the Auth0 Management API.
*/
public Builder(String domain, String apiToken) {
this(domain, SimpleTokenProvider.create(apiToken));
}

/**
* Create a new Builder
* @param domain the domain of the tenant.
* @param tokenProvider the API Token provider to use when making requests.
*/
public Builder(String domain, TokenProvider tokenProvider) {
this.domain = domain;
this.apiToken = apiToken;
this.tokenProvider = tokenProvider;
}

/**
Expand All @@ -438,7 +458,7 @@ public Builder withHttpClient(Auth0HttpClient httpClient) {
* @return the configured {@code ManagementAPI} instance.
*/
public ManagementAPI build() {
return new ManagementAPI(domain, SimpleTokenProvider.create(apiToken), httpClient);
return new ManagementAPI(domain, tokenProvider, httpClient);
}
}
}
36 changes: 34 additions & 2 deletions src/test/java/com/auth0/client/mgmt/ManagementAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void shouldCreateWithDomainAndToken() {
}

@Test
public void shouldCreateWithHttpClient() {
public void shouldCreateWithHttpClientWithApiToken() {
Auth0HttpClient httpClient = new Auth0HttpClient() {
@Override
public Auth0HttpResponse sendRequest(Auth0HttpRequest request) {
Expand All @@ -69,6 +69,31 @@ public CompletableFuture<Auth0HttpResponse> sendRequestAsync(Auth0HttpRequest re

ManagementAPI api = ManagementAPI.newBuilder(DOMAIN, API_TOKEN)
.withHttpClient(httpClient).build();

assertThat(api, is(notNullValue()));
assertThat(api.getHttpClient(), is(httpClient));
}

@Test
public void shouldCreateWithHttpClientWithTokenProvider() {
Auth0HttpClient httpClient = new Auth0HttpClient() {
@Override
public Auth0HttpResponse sendRequest(Auth0HttpRequest request) {
return null;
}

@Override
public CompletableFuture<Auth0HttpResponse> sendRequestAsync(Auth0HttpRequest request) {
return null;
}
};

ManagementAPI api = ManagementAPI.newBuilder(
DOMAIN,
SimpleTokenProvider.create(API_TOKEN)
)
.withHttpClient(httpClient)
.build();
assertThat(api, is(notNullValue()));
assertThat(api.getHttpClient(), is(httpClient));
}
Expand Down Expand Up @@ -98,10 +123,17 @@ public void shouldThrowWhenDomainIsNull() {
@Test
public void shouldThrowWhenApiTokenIsNull() {
verifyThrows(IllegalArgumentException.class,
() -> ManagementAPI.newBuilder(DOMAIN, null).build(),
() -> ManagementAPI.newBuilder(DOMAIN, (String) null).build(),
"'api token' cannot be null!");
}

@Test
public void shouldThrowWhenTokenProviderIsNull() {
verifyThrows(IllegalArgumentException.class,
() -> ManagementAPI.newBuilder(DOMAIN, (TokenProvider) null).build(),
"'token provider' cannot be null!");
}

@Test
public void shouldThrowOnUpdateWhenApiTokenIsNull() {
ManagementAPI api = ManagementAPI.newBuilder(DOMAIN, API_TOKEN).build();
Expand Down