Skip to content

Commit

Permalink
chore(firestore): use internal/detect (googleapis#10561)
Browse files Browse the repository at this point in the history
  • Loading branch information
jba authored Dec 10, 2024
1 parent bf1ea64 commit 798c7e6
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions firestore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import (
vkit "cloud.google.com/go/firestore/apiv1"
pb "cloud.google.com/go/firestore/apiv1/firestorepb"
"cloud.google.com/go/firestore/internal"
"cloud.google.com/go/internal/detect"
"cloud.google.com/go/internal/trace"
gax "github.com/googleapis/gax-go/v2"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/api/transport"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -62,7 +62,7 @@ func reqParamsHeaderVal(dbPath string) string {
// if no credentials were provided. When providing credentials, not all
// options will allow NewClient to extract the project ID. Specifically a JWT
// does not have the project ID encoded.
const DetectProjectID = "*detect-project-id*"
const DetectProjectID = detect.ProjectIDSentinel

// DefaultDatabaseID is name of the default database
const DefaultDatabaseID = "(default)"
Expand All @@ -88,21 +88,17 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
return nil, fmt.Errorf("firestore: dialing address from env var FIRESTORE_EMULATOR_HOST: %s", err)
}
o = []option.ClientOption{option.WithGRPCConn(conn)}
if projectID == DetectProjectID {
projectID, _ = detectProjectID(ctx, opts...)
if projectID == "" {
projectID = "dummy-emulator-firestore-project"
}
projectID, _ = detect.ProjectID(ctx, projectID, "", opts...)
if projectID == "" {
projectID = "dummy-emulator-firestore-project"
}
}
o = append(o, opts...)

if projectID == DetectProjectID {
detected, err := detectProjectID(ctx, o...)
if err != nil {
return nil, err
}
projectID = detected
// Detect project ID.
projectID, err := detect.ProjectID(ctx, projectID, "", o...)
if err != nil {
return nil, err
}

vc, err := vkit.NewClient(ctx, o...)
Expand Down Expand Up @@ -135,17 +131,6 @@ func NewClientWithDatabase(ctx context.Context, projectID string, databaseID str
return client, nil
}

func detectProjectID(ctx context.Context, opts ...option.ClientOption) (string, error) {
creds, err := transport.Creds(ctx, opts...)
if err != nil {
return "", fmt.Errorf("fetching creds: %w", err)
}
if creds.ProjectID == "" {
return "", errors.New("firestore: see the docs on DetectProjectID")
}
return creds.ProjectID, nil
}

// Close closes any resources held by the client.
//
// Close need not be called at program exit.
Expand Down

0 comments on commit 798c7e6

Please sign in to comment.