Skip to content

Commit bfd138a

Browse files
authored
feat(firestore): allow creating REST clients (googleapis#12575)
1 parent 3929405 commit bfd138a

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

firestore/client.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,19 @@ type Client struct {
7777
UsesEmulator bool // a boolean that indicates if the client is using the emulator
7878
}
7979

80-
// NewClient creates a new Firestore client that uses the given project.
81-
func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error) {
80+
// newClient creates a new Firestore client, using the given createClient function to create the underlying client.
81+
func newClient(ctx context.Context, projectID string, createClient func(ctx context.Context, opts ...option.ClientOption) (*vkit.Client, error), supportsEmulator bool, opts ...option.ClientOption) (*Client, error) {
8282
if projectID == "" {
8383
return nil, errors.New("firestore: projectID was empty")
8484
}
8585
var o []option.ClientOption
8686
var usesEmulator bool
8787
// If this environment variable is defined, configure the client to talk to the emulator.
8888
if addr := os.Getenv("FIRESTORE_EMULATOR_HOST"); addr != "" {
89+
if !supportsEmulator {
90+
return nil, fmt.Errorf("firestore: emulator is not supported for this client type")
91+
}
92+
8993
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithPerRPCCredentials(emulatorCreds{}))
9094
if err != nil {
9195
return nil, fmt.Errorf("firestore: dialing address from env var FIRESTORE_EMULATOR_HOST: %s", err)
@@ -105,7 +109,7 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
105109
return nil, err
106110
}
107111

108-
vc, err := vkit.NewClient(ctx, o...)
112+
vc, err := createClient(ctx, o...)
109113
if err != nil {
110114
return nil, err
111115
}
@@ -120,6 +124,16 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
120124
return c, nil
121125
}
122126

127+
// NewClient creates a new Firestore client that uses the given project.
128+
func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error) {
129+
return newClient(ctx, projectID, vkit.NewClient, true, opts...)
130+
}
131+
132+
// NewRESTClient creates a new Firestore client that uses the REST API.
133+
func NewRESTClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error) {
134+
return newClient(ctx, projectID, vkit.NewRESTClient, false, opts...)
135+
}
136+
123137
// NewClientWithDatabase creates a new Firestore client that accesses the
124138
// specified database.
125139
func NewClientWithDatabase(ctx context.Context, projectID string, databaseID string, opts ...option.ClientOption) (*Client, error) {

0 commit comments

Comments
 (0)