// NewHTTPClient returns an HTTP client for use communicating with a Google cloud // service, configured with the given ClientOptions. It also returns the endpoint // for the service as specified in the options. func NewHTTPClient(ctx context.Context, opts ...option.ClientOption) (*http.Client, string, error) { var o internal.DialSettings for _, opt := range opts { opt.Apply(&o) } if o.GRPCConn != nil { return nil, "", errors.New("unsupported gRPC connection specified") } // TODO(djd): Set UserAgent on all outgoing requests. if o.HTTPClient != nil { return o.HTTPClient, o.Endpoint, nil } if o.ServiceAccountJSONFilename != "" { ts, err := serviceAcctTokenSource(ctx, o.ServiceAccountJSONFilename, o.Scopes...) if err != nil { return nil, "", err } o.TokenSource = ts } if o.TokenSource == nil { var err error o.TokenSource, err = google.DefaultTokenSource(ctx, o.Scopes...) if err != nil { return nil, "", fmt.Errorf("google.DefaultTokenSource: %v", err) } } return oauth2.NewClient(ctx, o.TokenSource), o.Endpoint, nil }
// DialGRPC returns a GRPC connection for use communicating with a Google cloud // service, configured with the given ClientOptions. func DialGRPC(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, error) { var o internal.DialSettings for _, opt := range opts { opt.Apply(&o) } if o.HTTPClient != nil { return nil, errors.New("unsupported HTTP client specified") } if o.GRPCConn != nil { return o.GRPCConn, nil } if o.ServiceAccountJSONFilename != "" { ts, err := serviceAcctTokenSource(ctx, o.ServiceAccountJSONFilename, o.Scopes...) if err != nil { return nil, err } o.TokenSource = ts } if o.TokenSource == nil { var err error o.TokenSource, err = google.DefaultTokenSource(ctx, o.Scopes...) if err != nil { return nil, fmt.Errorf("google.DefaultTokenSource: %v", err) } } grpcOpts := []grpc.DialOption{ grpc.WithPerRPCCredentials(oauth.TokenSource{o.TokenSource}), grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), } if appengineDialerHook != nil { // Use the Socket API on App Engine. grpcOpts = append(grpcOpts, appengineDialerHook(ctx)) } grpcOpts = append(grpcOpts, o.GRPCDialOpts...) if o.UserAgent != "" { grpcOpts = append(grpcOpts, grpc.WithUserAgent(o.UserAgent)) } return grpc.DialContext(ctx, o.Endpoint, grpcOpts...) }
func (w withHTTPClient) Apply(o *internal.DialSettings) { o.HTTPClient = w.client }
func (w withUA) Apply(o *internal.DialSettings) { o.UserAgent = string(w) }
func (w withScopes) Apply(o *internal.DialSettings) { s := make([]string, len(w)) copy(s, w) o.Scopes = s }
func (w withEndpoint) Apply(o *internal.DialSettings) { o.Endpoint = string(w) }
func (w withServiceAccountFile) Apply(o *internal.DialSettings) { o.ServiceAccountJSONFilename = string(w) }
func (w withTokenSource) Apply(o *internal.DialSettings) { o.TokenSource = w.ts }
func (w withGRPCConnectionPool) Apply(o *internal.DialSettings) { balancer := grpc.RoundRobin(internal.NewPoolResolver(int(w), o)) o.GRPCDialOpts = append(o.GRPCDialOpts, grpc.WithBalancer(balancer)) }
func (w withGRPCDialOption) Apply(o *internal.DialSettings) { o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt) }
func (w withGRPCConn) Apply(o *internal.DialSettings) { o.GRPCConn = w.conn }
func (w withAPIKey) Apply(o *internal.DialSettings) { o.APIKey = string(w) }