settings.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package internal supports the options and transport packages.
  15. package internal
  16. import (
  17. "errors"
  18. "net/http"
  19. "golang.org/x/oauth2"
  20. "google.golang.org/grpc"
  21. )
  22. // DialSettings holds information needed to establish a connection with a
  23. // Google API service.
  24. type DialSettings struct {
  25. Endpoint string
  26. Scopes []string
  27. TokenSource oauth2.TokenSource
  28. CredentialsFile string // if set, Token Source is ignored.
  29. UserAgent string
  30. APIKey string
  31. HTTPClient *http.Client
  32. GRPCDialOpts []grpc.DialOption
  33. GRPCConn *grpc.ClientConn
  34. NoAuth bool
  35. }
  36. // Validate reports an error if ds is invalid.
  37. func (ds *DialSettings) Validate() error {
  38. hasCreds := ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != ""
  39. if ds.NoAuth && hasCreds {
  40. return errors.New("options.WithoutAuthentication is incompatible with any option that provides credentials")
  41. }
  42. if ds.HTTPClient != nil && ds.GRPCConn != nil {
  43. return errors.New("WithHTTPClient is incompatible with WithGRPCConn")
  44. }
  45. if ds.HTTPClient != nil && ds.GRPCDialOpts != nil {
  46. return errors.New("WithHTTPClient is incompatible with gRPC dial options")
  47. }
  48. return nil
  49. }