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

Reduce complexity in RestClient #120

Merged
merged 4 commits into from
Oct 11, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ public static ApplicationTokenCredentials fromFile(File credentialsFile) throws
// Set defaults
Properties authSettings = new Properties();
authSettings.put(CredentialSettings.AUTH_URL.toString(), AzureEnvironment.AZURE.getAuthenticationEndpoint());
authSettings.put(CredentialSettings.BASE_URL.toString(), AzureEnvironment.AZURE.getBaseUrl());
authSettings.put(CredentialSettings.BASE_URL.toString(), AzureEnvironment.AZURE.getResourceManagerEndpoint());
authSettings.put(CredentialSettings.MANAGEMENT_URI.toString(), AzureEnvironment.AZURE.getManagementEndpoint());
authSettings.put(CredentialSettings.GRAPH_URL.toString(), AzureEnvironment.AZURE.getGraphEndpoint());

// Load the credentials from the file
FileInputStream credentialsFileStream = new FileInputStream(credentialsFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

package com.microsoft.azure;

import java.lang.reflect.Field;

/**
* An instance of this class describes an environment in Azure.
*/
public final class AzureEnvironment {
/**
* Base URL for calls to Azure management API.
*/
private final String resourceManagerEndpoint;
private String resourceManagerEndpoint;

/**
* ActiveDirectory Endpoint for the authentications.
Expand Down Expand Up @@ -98,19 +100,10 @@ public AzureEnvironment(
*
* @return the Base URL for the management service.
*/
public String getBaseUrl() {
public String getResourceManagerEndpoint() {
return this.resourceManagerEndpoint;
}

/**
* @return a builder for the rest client.
*/
public RestClient.Builder.Buildable newRestClientBuilder() {
return new RestClient.Builder()
.withDefaultBaseUrl(this)
.withInterceptor(new RequestIdHeaderInterceptor());
}

/**
* @return the ActiveDirectory Endpoint for the Azure Environment.
*/
Expand Down Expand Up @@ -153,4 +146,59 @@ public boolean isValidateAuthority() {
public void setValidateAuthority(boolean validateAuthority) {
this.validateAuthority = validateAuthority;
}

/**
* The enum representing available endpoints in an environment.
*/
public enum Endpoint {
/** Azure Resource Manager endpoint. */
RESOURCE_MANAGER("resourceManagerEndpoint"),
/** Azure Active Directory Graph APIs endpoint. */
GRAPH("graphEndpoint");

private String field;

Endpoint(String value) {
this.field = value;
}

@Override
public String toString() {
return field;
}
}

/**
* Get the endpoint URL for the current environment.
*
* @param endpoint the endpoint
* @return the URL
*/
public String getEndpoint(Endpoint endpoint) {
try {
Field f = AzureEnvironment.class.getDeclaredField(endpoint.toString());
f.setAccessible(true);
return (String) f.get(this);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Unable to reflect on field " + endpoint.toString(), e);
}
}

/**
* Create a builder for rest client from an endpoint.
*
* @param endpoint the endpoint
* @return a RestClient builder
*/
public RestClient.Builder newRestClientBuilder(Endpoint endpoint) {
return new RestClient.Builder().withBaseUrl(this, endpoint);
}

/**
* Create a builder for rest client to Azure Resource Manager.
* @return a RestClient builder
*/
public RestClient.Builder newRestClientBuilder() {
return new RestClient.Builder().withBaseUrl(this, Endpoint.RESOURCE_MANAGER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public abstract class AzureServiceClient {
private RestClient restClient;

protected AzureServiceClient(String baseUrl) {
this(new RestClient.Builder().withBaseUrl(baseUrl)
.withInterceptor(new RequestIdHeaderInterceptor()).build());
this(new RestClient.Builder().withBaseUrl(baseUrl).build());
}

/**
Expand Down
Loading