Skip to content

Commit

Permalink
Merge pull request #346 from ajkannan/project-id-optional
Browse files Browse the repository at this point in the history
Allow services to avoid setting project ID
  • Loading branch information
aozarov committed Nov 10, 2015
2 parents 98e9c03 + 6c40825 commit ed1d437
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.gcloud;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkArgument;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
Expand Down Expand Up @@ -306,7 +306,13 @@ public B readTimeout(int readTimeout) {
protected ServiceOptions(Class<? extends ServiceFactory<ServiceT, OptionsT>> serviceFactoryClass,
Class<? extends ServiceRpcFactory<ServiceRpcT, OptionsT>> rpcFactoryClass,
Builder<ServiceT, ServiceRpcT, OptionsT, ?> builder) {
projectId = checkNotNull(builder.projectId != null ? builder.projectId : defaultProject());
projectId = builder.projectId != null ? builder.projectId : defaultProject();
if (projectIdRequired()) {
checkArgument(
projectId != null,
"A project ID is required for this service but could not be determined from the builder or "
+ "the environment. Please set a project ID using the builder.");
}
host = firstNonNull(builder.host, defaultHost());
httpTransportFactory = firstNonNull(builder.httpTransportFactory,
getFromServiceLoader(HttpTransportFactory.class, DefaultHttpTransportFactory.INSTANCE));
Expand All @@ -325,6 +331,16 @@ protected ServiceOptions(Class<? extends ServiceFactory<ServiceT, OptionsT>> ser
clock = firstNonNull(builder.clock, Clock.defaultClock());
}

/**
* Returns whether a service requires a project ID. This method may be overridden in
* service-specific Options objects.
*
* @return true if a project ID is required to use the service, false if not.
*/
protected boolean projectIdRequired() {
return true;
}

private static AuthCredentials defaultAuthCredentials() {
// Consider App Engine. This will not be needed once issue #21 is fixed.
if (appEngineAppId() != null) {
Expand Down Expand Up @@ -462,6 +478,8 @@ public ServiceRpcT rpc() {

/**
* 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 ed1d437

Please sign in to comment.