option.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 option contains options for Google API clients.
  15. package option
  16. import (
  17. "net/http"
  18. "golang.org/x/oauth2"
  19. "google.golang.org/api/internal"
  20. "google.golang.org/grpc"
  21. )
  22. // A ClientOption is an option for a Google API client.
  23. type ClientOption interface {
  24. Apply(*internal.DialSettings)
  25. }
  26. // WithTokenSource returns a ClientOption that specifies an OAuth2 token
  27. // source to be used as the basis for authentication.
  28. func WithTokenSource(s oauth2.TokenSource) ClientOption {
  29. return withTokenSource{s}
  30. }
  31. type withTokenSource struct{ ts oauth2.TokenSource }
  32. func (w withTokenSource) Apply(o *internal.DialSettings) {
  33. o.TokenSource = w.ts
  34. }
  35. type withCredFile string
  36. func (w withCredFile) Apply(o *internal.DialSettings) {
  37. o.CredentialsFile = string(w)
  38. }
  39. // WithCredentialsFile returns a ClientOption that authenticates
  40. // API calls with the given service account or refresh token JSON
  41. // credentials file.
  42. func WithCredentialsFile(filename string) ClientOption {
  43. return withCredFile(filename)
  44. }
  45. // WithServiceAccountFile returns a ClientOption that uses a Google service
  46. // account credentials file to authenticate.
  47. //
  48. // Deprecated: Use WithCredentialsFile instead.
  49. func WithServiceAccountFile(filename string) ClientOption {
  50. return WithCredentialsFile(filename)
  51. }
  52. // WithEndpoint returns a ClientOption that overrides the default endpoint
  53. // to be used for a service.
  54. func WithEndpoint(url string) ClientOption {
  55. return withEndpoint(url)
  56. }
  57. type withEndpoint string
  58. func (w withEndpoint) Apply(o *internal.DialSettings) {
  59. o.Endpoint = string(w)
  60. }
  61. // WithScopes returns a ClientOption that overrides the default OAuth2 scopes
  62. // to be used for a service.
  63. func WithScopes(scope ...string) ClientOption {
  64. return withScopes(scope)
  65. }
  66. type withScopes []string
  67. func (w withScopes) Apply(o *internal.DialSettings) {
  68. s := make([]string, len(w))
  69. copy(s, w)
  70. o.Scopes = s
  71. }
  72. // WithUserAgent returns a ClientOption that sets the User-Agent.
  73. func WithUserAgent(ua string) ClientOption {
  74. return withUA(ua)
  75. }
  76. type withUA string
  77. func (w withUA) Apply(o *internal.DialSettings) { o.UserAgent = string(w) }
  78. // WithHTTPClient returns a ClientOption that specifies the HTTP client to use
  79. // as the basis of communications. This option may only be used with services
  80. // that support HTTP as their communication transport. When used, the
  81. // WithHTTPClient option takes precedent over all other supplied options.
  82. func WithHTTPClient(client *http.Client) ClientOption {
  83. return withHTTPClient{client}
  84. }
  85. type withHTTPClient struct{ client *http.Client }
  86. func (w withHTTPClient) Apply(o *internal.DialSettings) {
  87. o.HTTPClient = w.client
  88. }
  89. // WithGRPCConn returns a ClientOption that specifies the gRPC client
  90. // connection to use as the basis of communications. This option many only be
  91. // used with services that support gRPC as their communication transport. When
  92. // used, the WithGRPCConn option takes precedent over all other supplied
  93. // options.
  94. func WithGRPCConn(conn *grpc.ClientConn) ClientOption {
  95. return withGRPCConn{conn}
  96. }
  97. type withGRPCConn struct{ conn *grpc.ClientConn }
  98. func (w withGRPCConn) Apply(o *internal.DialSettings) {
  99. o.GRPCConn = w.conn
  100. }
  101. // WithGRPCDialOption returns a ClientOption that appends a new grpc.DialOption
  102. // to an underlying gRPC dial. It does not work with WithGRPCConn.
  103. func WithGRPCDialOption(opt grpc.DialOption) ClientOption {
  104. return withGRPCDialOption{opt}
  105. }
  106. type withGRPCDialOption struct{ opt grpc.DialOption }
  107. func (w withGRPCDialOption) Apply(o *internal.DialSettings) {
  108. o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt)
  109. }
  110. // WithGRPCConnectionPool returns a ClientOption that creates a pool of gRPC
  111. // connections that requests will be balanced between.
  112. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  113. func WithGRPCConnectionPool(size int) ClientOption {
  114. return withGRPCConnectionPool(size)
  115. }
  116. type withGRPCConnectionPool int
  117. func (w withGRPCConnectionPool) Apply(o *internal.DialSettings) {
  118. balancer := grpc.RoundRobin(internal.NewPoolResolver(int(w), o))
  119. o.GRPCDialOpts = append(o.GRPCDialOpts, grpc.WithBalancer(balancer))
  120. }
  121. // WithAPIKey returns a ClientOption that specifies an API key to be used
  122. // as the basis for authentication.
  123. func WithAPIKey(apiKey string) ClientOption {
  124. return withAPIKey(apiKey)
  125. }
  126. type withAPIKey string
  127. func (w withAPIKey) Apply(o *internal.DialSettings) { o.APIKey = string(w) }
  128. // WithoutAuthentication returns a ClientOption that specifies that no
  129. // authentication should be used. It is suitable only for testing and for
  130. // accessing public resources, like public Google Cloud Storage buckets.
  131. // It is an error to provide both WithoutAuthentication and any of WithAPIKey,
  132. // WithTokenSource, WithCredentialsFile or WithServiceAccountFile.
  133. func WithoutAuthentication() ClientOption {
  134. return withoutAuthentication{}
  135. }
  136. type withoutAuthentication struct{}
  137. func (w withoutAuthentication) Apply(o *internal.DialSettings) { o.NoAuth = true }