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

Exposing TransportChannelProvider & CredentialsProvider #3320

Merged
merged 4 commits into from
Jun 8, 2018
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 @@ -17,6 +17,10 @@
package com.google.cloud.firestore;

import com.google.api.core.InternalApi;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.GoogleCredentialsProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.ServiceDefaults;
import com.google.cloud.ServiceOptions;
import com.google.cloud.TransportOptions;
Expand All @@ -26,6 +30,7 @@
import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nonnull;

Expand All @@ -38,13 +43,14 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
.add("https://www.googleapis.com/auth/cloud-platform")
.add("https://www.googleapis.com/auth/datastore")
.build();
private static final String DEFAULT_HOST = FirestoreSettings.getDefaultEndpoint();
private static final String DEFAULT_DATABASE_ID = "(default)";
private static final boolean DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED = false;
private static final long serialVersionUID = -5853552236134770088L;

private static final long serialVersionUID = -5853552236134770090L;

private final String databaseId;
private final boolean timestampsInSnapshotsEnabled;
private final TransportChannelProvider channelProvider;
private final CredentialsProvider credentialsProvider;

public static class DefaultFirestoreFactory implements FirestoreFactory {

Expand Down Expand Up @@ -84,36 +90,95 @@ protected boolean projectIdRequired() {

@Override
protected String getDefaultHost() {
return DEFAULT_HOST;
return FirestoreDefaults.INSTANCE.getHost();
}

public String getDatabaseId() {
return databaseId;
}

public CredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}

public TransportChannelProvider getTransportChannelProvider() {
return channelProvider;
}

public static class Builder extends ServiceOptions.Builder<Firestore, FirestoreOptions, Builder> {

private String databaseId = DEFAULT_DATABASE_ID;
private String databaseId = FirestoreDefaults.INSTANCE.getDatabaseId();
private boolean timestampsInSnapshotsEnabled = DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED;
private TransportChannelProvider channelProvider =
FirestoreDefaults.INSTANCE.getDefaultTransportChannelProvider();
private CredentialsProvider credentialsProvider =
FirestoreDefaults.INSTANCE.getDefaultCredentialsProvider();

private Builder() {}

private Builder(FirestoreOptions options) {
super(options);
timestampsInSnapshotsEnabled = options.timestampsInSnapshotsEnabled;
this.databaseId = options.databaseId;
this.timestampsInSnapshotsEnabled = options.timestampsInSnapshotsEnabled;
this.channelProvider = options.channelProvider;
this.credentialsProvider = options.credentialsProvider;
}

/**
* Sets the {@link TransportOptions} to use with this Firestore client.
*
* @param transportOptions A GrpcTransportOptions object that defines the transport options for
* this client.
*/
@Nonnull
@Override
public Builder setTransportOptions(@Nonnull TransportOptions transportOptions) {
if (!(transportOptions instanceof GrpcTransportOptions)) {
throw new IllegalArgumentException(
"Only grpc transport is allowed for " + API_SHORT_NAME + ".");
"Only GRPC transport is allowed for " + API_SHORT_NAME + ".");
}
super.setTransportOptions(transportOptions);
return this;
}

/**
* Sets the {@link TransportChannelProvider} to use with this Firestore client.
*
* @param channelProvider A InstantiatingGrpcChannelProvider object that defines the transport
* provider for this client.
*/
@Nonnull
public Builder setChannelProvider(@Nonnull TransportChannelProvider channelProvider) {
if (!(channelProvider instanceof InstantiatingGrpcChannelProvider)) {
throw new IllegalArgumentException(
"Only GRPC channels are allowed for " + API_SHORT_NAME + ".");
}
this.channelProvider = channelProvider;
return this;
}

/**
* Sets the {@link CredentialsProvider} to use with this Firestore client.
*
* @param credentialsProvider A CredentialsProvider object that defines the credential provider
* for this client.
*/
@Nonnull
public Builder setCredentialsProvider(@Nonnull CredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
return this;
}

/**
* Sets the database ID to use with this Firestore client.
*
* @param databaseId The Firestore database ID to use with this client.
*/
public Builder setDatabaseId(@Nonnull String databaseId) {
this.databaseId = databaseId;
return this;
}

/**
* Enables the use of {@link com.google.cloud.Timestamp Timestamps} for timestamp fields in
* {@link DocumentSnapshot DocumentSnapshots}.
Expand Down Expand Up @@ -141,12 +206,6 @@ public Builder setTimestampsInSnapshotsEnabled(boolean value) {
return this;
}

@Nonnull
public Builder setDatabaseId(@Nonnull String databaseId) {
this.databaseId = databaseId;
return this;
}

@Override
@Nonnull
public FirestoreOptions build() {
Expand All @@ -159,10 +218,31 @@ protected FirestoreOptions(Builder builder) {
super(FirestoreFactory.class, FirestoreRpcFactory.class, builder, new FirestoreDefaults());

this.databaseId = builder.databaseId;
this.channelProvider = builder.channelProvider;
this.credentialsProvider = builder.credentialsProvider;
this.timestampsInSnapshotsEnabled = builder.timestampsInSnapshotsEnabled;
}

private static class FirestoreDefaults implements ServiceDefaults<Firestore, FirestoreOptions> {
private static final FirestoreDefaults INSTANCE = new FirestoreDefaults();

private final String HOST = FirestoreSettings.getDefaultEndpoint();
private final String DATABASE_ID = "(default)";
private final TransportOptions TRANSPORT_OPTIONS = getDefaultTransportOptionsBuilder().build();
private final TransportChannelProvider CHANNEL_PROVIDER =
getDefaultTransportChannelProviderBuilder().build();
private final CredentialsProvider CREDENTIALS_PROVIDER =
getDefaultCredentialsProviderBuilder().build();

@Nonnull
String getHost() {
return HOST;
}

@Nonnull
String getDatabaseId() {
return DATABASE_ID;
}

@Nonnull
@Override
Expand All @@ -179,13 +259,34 @@ public FirestoreRpcFactory getDefaultRpcFactory() {
@Nonnull
@Override
public TransportOptions getDefaultTransportOptions() {
return getDefaultGrpcTransportOptions();
return TRANSPORT_OPTIONS;
}

@Nonnull
TransportChannelProvider getDefaultTransportChannelProvider() {
return CHANNEL_PROVIDER;
}

@Nonnull
CredentialsProvider getDefaultCredentialsProvider() {
return CREDENTIALS_PROVIDER;
}
}

@Nonnull
public static GrpcTransportOptions.Builder getDefaultTransportOptionsBuilder() {
return GrpcTransportOptions.newBuilder();
}

@Nonnull
public static GrpcTransportOptions getDefaultGrpcTransportOptions() {

This comment was marked as spam.

return GrpcTransportOptions.newBuilder().build();
public static InstantiatingGrpcChannelProvider.Builder
getDefaultTransportChannelProviderBuilder() {
return FirestoreSettings.defaultGrpcTransportProviderBuilder();
}

@Nonnull
public static GoogleCredentialsProvider.Builder getDefaultCredentialsProviderBuilder() {
return FirestoreSettings.defaultCredentialsProviderBuilder();
}

/**
Expand All @@ -206,13 +307,22 @@ FirestoreRpc getFirestoreRpc() {
}

@Override
public boolean equals(Object obj) {
return obj instanceof FirestoreOptions && baseEquals((FirestoreOptions) obj);
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FirestoreOptions that = (FirestoreOptions) o;
return Objects.equals(databaseId, that.databaseId)
&& Objects.equals(channelProvider, that.channelProvider)
&& baseEquals(that);
}

@Override
public int hashCode() {
return baseHashCode();
return Objects.hash(databaseId, channelProvider, baseHashCode());
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.grpc.GrpcCallContext;
import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.rpc.ApiClientHeaderProvider;
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.HeaderProvider;
Expand Down Expand Up @@ -105,11 +104,8 @@ public GrpcFirestoreRpc(final FirestoreOptions options) throws IOException {
DatabaseRootName databaseName =
DatabaseRootName.of(options.getProjectId(), options.getDatabaseId());

settingsBuilder.setCredentialsProvider(
GrpcTransportOptions.setUpCredentialsProvider(options));
settingsBuilder.setTransportChannelProvider(
GrpcTransportOptions.setUpChannelProvider(
FirestoreSettings.defaultGrpcTransportProviderBuilder(), options));
settingsBuilder.setCredentialsProvider(options.getCredentialsProvider());
settingsBuilder.setTransportChannelProvider(options.getTransportChannelProvider());

HeaderProvider internalHeaderProvider =
FirestoreSettings.defaultApiClientHeaderProviderBuilder()
Expand Down