dial.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2015 Google LLC.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package transport
  5. import (
  6. "context"
  7. "net/http"
  8. "golang.org/x/oauth2/google"
  9. "google.golang.org/grpc"
  10. "google.golang.org/api/internal"
  11. "google.golang.org/api/option"
  12. gtransport "google.golang.org/api/transport/grpc"
  13. htransport "google.golang.org/api/transport/http"
  14. )
  15. // NewHTTPClient returns an HTTP client for use communicating with a Google cloud
  16. // service, configured with the given ClientOptions. It also returns the endpoint
  17. // for the service as specified in the options.
  18. func NewHTTPClient(ctx context.Context, opts ...option.ClientOption) (*http.Client, string, error) {
  19. return htransport.NewClient(ctx, opts...)
  20. }
  21. // DialGRPC returns a GRPC connection for use communicating with a Google cloud
  22. // service, configured with the given ClientOptions.
  23. func DialGRPC(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, error) {
  24. return gtransport.Dial(ctx, opts...)
  25. }
  26. // DialGRPCInsecure returns an insecure GRPC connection for use communicating
  27. // with fake or mock Google cloud service implementations, such as emulators.
  28. // The connection is configured with the given ClientOptions.
  29. func DialGRPCInsecure(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, error) {
  30. return gtransport.DialInsecure(ctx, opts...)
  31. }
  32. // Creds constructs a google.Credentials from the information in the options,
  33. // or obtains the default credentials in the same way as google.FindDefaultCredentials.
  34. func Creds(ctx context.Context, opts ...option.ClientOption) (*google.Credentials, error) {
  35. var ds internal.DialSettings
  36. for _, opt := range opts {
  37. opt.Apply(&ds)
  38. }
  39. return internal.Creds(ctx, &ds)
  40. }