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

Allow services to avoid setting project ID #346

Merged
merged 2 commits into from
Nov 10, 2015
Merged
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 @@ -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