Skip to content

Commit 18ff070

Browse files
authored
feat(bigquery): switch to centralized project autodetect logic (#4625)
Switches from a local implementation to the logic in internal/detect.
1 parent d8cc9be commit 18ff070

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

Diff for: bigquery/bigquery.go

+5-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package bigquery
1616

1717
import (
1818
"context"
19-
"errors"
2019
"fmt"
2120
"io"
2221
"net/http"
@@ -25,12 +24,12 @@ import (
2524
"time"
2625

2726
"cloud.google.com/go/internal"
27+
"cloud.google.com/go/internal/detect"
2828
"cloud.google.com/go/internal/version"
2929
gax "github.com/googleapis/gax-go/v2"
3030
bq "google.golang.org/api/bigquery/v2"
3131
"google.golang.org/api/googleapi"
3232
"google.golang.org/api/option"
33-
"google.golang.org/api/transport"
3433
)
3534

3635
const (
@@ -83,11 +82,10 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
8382
return nil, fmt.Errorf("bigquery: constructing client: %v", err)
8483
}
8584

86-
if projectID == DetectProjectID {
87-
projectID, err = detectProjectID(ctx, opts...)
88-
if err != nil {
89-
return nil, fmt.Errorf("failed to detect project: %v", err)
90-
}
85+
// Handle project autodetection.
86+
projectID, err = detect.ProjectID(ctx, projectID, "", opts...)
87+
if err != nil {
88+
return nil, err
9189
}
9290

9391
c := &Client{
@@ -110,17 +108,6 @@ func (c *Client) Close() error {
110108
return nil
111109
}
112110

113-
func detectProjectID(ctx context.Context, opts ...option.ClientOption) (string, error) {
114-
creds, err := transport.Creds(ctx, opts...)
115-
if err != nil {
116-
return "", fmt.Errorf("fetching creds: %v", err)
117-
}
118-
if creds.ProjectID == "" {
119-
return "", errors.New("credentials did not provide a valid ProjectID")
120-
}
121-
return creds.ProjectID, nil
122-
}
123-
124111
// Calls the Jobs.Insert RPC and returns a Job.
125112
func (c *Client) insertJob(ctx context.Context, job *bq.Job, media io.Reader) (*Job, error) {
126113
call := c.bqs.Jobs.Insert(c.projectID, job).Context(ctx)

0 commit comments

Comments
 (0)