Skip to content

Commit

Permalink
feat: add method to set emulator host programmatically (#319) (#336)
Browse files Browse the repository at this point in the history
See for initial discussion: #319


Fixes #319, #210.
Relates to #190.
Similar issues in other SDKs: googleapis/google-cloud-go#1978, firebase/firebase-admin-node#776.
  • Loading branch information
Gerschtli committed Aug 27, 2020
1 parent 4c2329b commit 97037f4
Showing 1 changed file with 23 additions and 1 deletion.
Expand Up @@ -60,6 +60,7 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
private final String databaseId;
private final TransportChannelProvider channelProvider;
private final CredentialsProvider credentialsProvider;
private final String emulatorHost;

public static class DefaultFirestoreFactory implements FirestoreFactory {

Expand Down Expand Up @@ -114,11 +115,16 @@ public TransportChannelProvider getTransportChannelProvider() {
return channelProvider;
}

public String getEmulatorHost() {
return emulatorHost;
}

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

@Nullable private String databaseId = null;
@Nullable private TransportChannelProvider channelProvider = null;
@Nullable private CredentialsProvider credentialsProvider = null;
@Nullable private String emulatorHost = null;

private Builder() {}

Expand All @@ -127,6 +133,7 @@ private Builder(FirestoreOptions options) {
this.databaseId = options.databaseId;
this.channelProvider = options.channelProvider;
this.credentialsProvider = options.credentialsProvider;
this.emulatorHost = options.emulatorHost;
}

/**
Expand Down Expand Up @@ -174,6 +181,17 @@ public Builder setCredentialsProvider(@Nonnull CredentialsProvider credentialsPr
return this;
}

/**
* Sets the emulator host to use with this Firestore client. The value passed to this method
* will take precedent if the {@code FIRESTORE_EMULATOR_HOST} environment variable is also set.
*
* @param emulatorHost The Firestore emulator host to use with this client.
*/
public Builder setEmulatorHost(@Nonnull String emulatorHost) {
this.emulatorHost = emulatorHost;
return this;
}

/**
* Sets the database ID to use with this Firestore client.
*
Expand All @@ -196,7 +214,9 @@ public FirestoreOptions build() {
}

// Override credentials and channel provider if we are using the emulator.
String emulatorHost = System.getenv(FIRESTORE_EMULATOR_SYSTEM_VARIABLE);
if (emulatorHost == null) {
emulatorHost = System.getenv(FIRESTORE_EMULATOR_SYSTEM_VARIABLE);
}
if (emulatorHost != null) {
// Try creating a host in order to validate that the host name is valid.
try {
Expand Down Expand Up @@ -280,6 +300,8 @@ protected FirestoreOptions(Builder builder) {
builder.credentialsProvider != null
? builder.credentialsProvider
: GrpcTransportOptions.setUpCredentialsProvider(this);

this.emulatorHost = builder.emulatorHost;
}

private static class FirestoreDefaults implements ServiceDefaults<Firestore, FirestoreOptions> {
Expand Down

0 comments on commit 97037f4

Please sign in to comment.