option.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2017 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 option contains options for Google API clients.
  5. package option
  6. import (
  7. "net/http"
  8. "golang.org/x/oauth2"
  9. "google.golang.org/api/internal"
  10. "google.golang.org/grpc"
  11. )
  12. // A ClientOption is an option for a Google API client.
  13. type ClientOption interface {
  14. Apply(*internal.DialSettings)
  15. }
  16. // WithTokenSource returns a ClientOption that specifies an OAuth2 token
  17. // source to be used as the basis for authentication.
  18. func WithTokenSource(s oauth2.TokenSource) ClientOption {
  19. return withTokenSource{s}
  20. }
  21. type withTokenSource struct{ ts oauth2.TokenSource }
  22. func (w withTokenSource) Apply(o *internal.DialSettings) {
  23. o.TokenSource = w.ts
  24. }
  25. type withCredFile string
  26. func (w withCredFile) Apply(o *internal.DialSettings) {
  27. o.CredentialsFile = string(w)
  28. }
  29. // WithCredentialsFile returns a ClientOption that authenticates
  30. // API calls with the given service account or refresh token JSON
  31. // credentials file.
  32. func WithCredentialsFile(filename string) ClientOption {
  33. return withCredFile(filename)
  34. }
  35. // WithServiceAccountFile returns a ClientOption that uses a Google service
  36. // account credentials file to authenticate.
  37. //
  38. // Deprecated: Use WithCredentialsFile instead.
  39. func WithServiceAccountFile(filename string) ClientOption {
  40. return WithCredentialsFile(filename)
  41. }
  42. // WithCredentialsJSON returns a ClientOption that authenticates
  43. // API calls with the given service account or refresh token JSON
  44. // credentials.
  45. func WithCredentialsJSON(p []byte) ClientOption {
  46. return withCredentialsJSON(p)
  47. }
  48. type withCredentialsJSON []byte
  49. func (w withCredentialsJSON) Apply(o *internal.DialSettings) {
  50. o.CredentialsJSON = make([]byte, len(w))
  51. copy(o.CredentialsJSON, w)
  52. }
  53. // WithEndpoint returns a ClientOption that overrides the default endpoint
  54. // to be used for a service.
  55. func WithEndpoint(url string) ClientOption {
  56. return withEndpoint(url)
  57. }
  58. type withEndpoint string
  59. func (w withEndpoint) Apply(o *internal.DialSettings) {
  60. o.Endpoint = string(w)
  61. }
  62. // WithScopes returns a ClientOption that overrides the default OAuth2 scopes
  63. // to be used for a service.
  64. func WithScopes(scope ...string) ClientOption {
  65. return withScopes(scope)
  66. }
  67. type withScopes []string
  68. func (w withScopes) Apply(o *internal.DialSettings) {
  69. o.Scopes = make([]string, len(w))
  70. copy(o.Scopes, w)
  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 may 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. //
  124. // API Keys can only be used for JSON-over-HTTP APIs, including those under
  125. // the import path google.golang.org/api/....
  126. func WithAPIKey(apiKey string) ClientOption {
  127. return withAPIKey(apiKey)
  128. }
  129. type withAPIKey string
  130. func (w withAPIKey) Apply(o *internal.DialSettings) { o.APIKey = string(w) }
  131. // WithAudiences returns a ClientOption that specifies an audience to be used
  132. // as the audience field ("aud") for the JWT token authentication.
  133. func WithAudiences(audience ...string) ClientOption {
  134. return withAudiences(audience)
  135. }
  136. type withAudiences []string
  137. func (w withAudiences) Apply(o *internal.DialSettings) {
  138. o.Audiences = make([]string, len(w))
  139. copy(o.Audiences, w)
  140. }
  141. // WithoutAuthentication returns a ClientOption that specifies that no
  142. // authentication should be used. It is suitable only for testing and for
  143. // accessing public resources, like public Google Cloud Storage buckets.
  144. // It is an error to provide both WithoutAuthentication and any of WithAPIKey,
  145. // WithTokenSource, WithCredentialsFile or WithServiceAccountFile.
  146. func WithoutAuthentication() ClientOption {
  147. return withoutAuthentication{}
  148. }
  149. type withoutAuthentication struct{}
  150. func (w withoutAuthentication) Apply(o *internal.DialSettings) { o.NoAuth = true }
  151. // WithQuotaProject returns a ClientOption that specifies the project used
  152. // for quota and billing purposes.
  153. //
  154. // For more information please read:
  155. // https://cloud.google.com/apis/docs/system-parameters
  156. func WithQuotaProject(quotaProject string) ClientOption {
  157. return withQuotaProject(quotaProject)
  158. }
  159. type withQuotaProject string
  160. func (w withQuotaProject) Apply(o *internal.DialSettings) {
  161. o.QuotaProject = string(w)
  162. }
  163. // WithRequestReason returns a ClientOption that specifies a reason for
  164. // making the request, which is intended to be recorded in audit logging.
  165. // An example reason would be a support-case ticket number.
  166. //
  167. // For more information please read:
  168. // https://cloud.google.com/apis/docs/system-parameters
  169. func WithRequestReason(requestReason string) ClientOption {
  170. return withRequestReason(requestReason)
  171. }
  172. type withRequestReason string
  173. func (w withRequestReason) Apply(o *internal.DialSettings) {
  174. o.RequestReason = string(w)
  175. }
  176. // WithTelemetryDisabled returns a ClientOption that disables default telemetry (OpenCensus)
  177. // settings on gRPC and HTTP clients.
  178. // An example reason would be to bind custom telemetry that overrides the defaults.
  179. func WithTelemetryDisabled() ClientOption {
  180. return withTelemetryDisabledOption{}
  181. }
  182. type withTelemetryDisabledOption struct{}
  183. func (w withTelemetryDisabledOption) Apply(o *internal.DialSettings) {
  184. o.TelemetryDisabled = true
  185. }