Skip to content

Commit

Permalink
Expose static methods to get the default project ID and credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Sep 12, 2016
1 parent 4d5a86c commit 325d4cd
Showing 1 changed file with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public B clock(Clock clock) {
}

/**
* Sets project id.
* Sets project ID.
*
* @return the builder
*/
Expand Down Expand Up @@ -235,7 +235,18 @@ protected boolean projectIdRequired() {
return true;
}

private static AuthCredentials defaultAuthCredentials() {
/**
* Returns the default authentication credentials, or {@code null} if no default credentials could
* be found. This method returns the first available credentials among the following sources:
* <ol>
* <li>App Engine credentials
* <li>JSON credentials pointed to by the {@code GOOGLE_APPLICATION_CREDENTIALS} environment
* variable
* <li>Google Cloud SDK credentials
* <li>Compute Engine credentials
* </ol>
*/
public static AuthCredentials defaultAuthCredentials() {
// Consider App Engine.
if (appEngineAppId() != null) {
try {
Expand All @@ -261,6 +272,21 @@ protected String defaultHost() {
}

protected String defaultProject() {
return defaultProjectId();
}

/**
* Returns the default project ID, or {@code null} if no default project ID could be found. This
* method returns the first available project ID among the following sources:
* <ol>
* <li>App Engine project ID
* <li>Project ID specified in the JSON credentials file pointed by the
* {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable
* <li>Google Cloud SDK project ID
* <li>Compute Engine project ID
* </ol>
*/
public static String defaultProjectId() {
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
if (projectId == null) {
projectId =
Expand Down Expand Up @@ -402,8 +428,8 @@ public ServiceRpcT rpc() {
}

/**
* Returns the project id. Return value can be null (for services that don't require a project
* id).
* Returns the project ID. Return value can be null (for services that don't require a project
* ID).
*/
public String projectId() {
return projectId;
Expand Down

0 comments on commit 325d4cd

Please sign in to comment.