Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

236 lignes
7.3 KiB

  1. // Copyright 2017 Google LLC
  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. // WithCredentialsJSON returns a ClientOption that authenticates
  53. // API calls with the given service account or refresh token JSON
  54. // credentials.
  55. func WithCredentialsJSON(p []byte) ClientOption {
  56. return withCredentialsJSON(p)
  57. }
  58. type withCredentialsJSON []byte
  59. func (w withCredentialsJSON) Apply(o *internal.DialSettings) {
  60. o.CredentialsJSON = make([]byte, len(w))
  61. copy(o.CredentialsJSON, w)
  62. }
  63. // WithEndpoint returns a ClientOption that overrides the default endpoint
  64. // to be used for a service.
  65. func WithEndpoint(url string) ClientOption {
  66. return withEndpoint(url)
  67. }
  68. type withEndpoint string
  69. func (w withEndpoint) Apply(o *internal.DialSettings) {
  70. o.Endpoint = string(w)
  71. }
  72. // WithScopes returns a ClientOption that overrides the default OAuth2 scopes
  73. // to be used for a service.
  74. func WithScopes(scope ...string) ClientOption {
  75. return withScopes(scope)
  76. }
  77. type withScopes []string
  78. func (w withScopes) Apply(o *internal.DialSettings) {
  79. o.Scopes = make([]string, len(w))
  80. copy(o.Scopes, w)
  81. }
  82. // WithUserAgent returns a ClientOption that sets the User-Agent.
  83. func WithUserAgent(ua string) ClientOption {
  84. return withUA(ua)
  85. }
  86. type withUA string
  87. func (w withUA) Apply(o *internal.DialSettings) { o.UserAgent = string(w) }
  88. // WithHTTPClient returns a ClientOption that specifies the HTTP client to use
  89. // as the basis of communications. This option may only be used with services
  90. // that support HTTP as their communication transport. When used, the
  91. // WithHTTPClient option takes precedent over all other supplied options.
  92. func WithHTTPClient(client *http.Client) ClientOption {
  93. return withHTTPClient{client}
  94. }
  95. type withHTTPClient struct{ client *http.Client }
  96. func (w withHTTPClient) Apply(o *internal.DialSettings) {
  97. o.HTTPClient = w.client
  98. }
  99. // WithGRPCConn returns a ClientOption that specifies the gRPC client
  100. // connection to use as the basis of communications. This option many only be
  101. // used with services that support gRPC as their communication transport. When
  102. // used, the WithGRPCConn option takes precedent over all other supplied
  103. // options.
  104. func WithGRPCConn(conn *grpc.ClientConn) ClientOption {
  105. return withGRPCConn{conn}
  106. }
  107. type withGRPCConn struct{ conn *grpc.ClientConn }
  108. func (w withGRPCConn) Apply(o *internal.DialSettings) {
  109. o.GRPCConn = w.conn
  110. }
  111. // WithGRPCDialOption returns a ClientOption that appends a new grpc.DialOption
  112. // to an underlying gRPC dial. It does not work with WithGRPCConn.
  113. func WithGRPCDialOption(opt grpc.DialOption) ClientOption {
  114. return withGRPCDialOption{opt}
  115. }
  116. type withGRPCDialOption struct{ opt grpc.DialOption }
  117. func (w withGRPCDialOption) Apply(o *internal.DialSettings) {
  118. o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt)
  119. }
  120. // WithGRPCConnectionPool returns a ClientOption that creates a pool of gRPC
  121. // connections that requests will be balanced between.
  122. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  123. func WithGRPCConnectionPool(size int) ClientOption {
  124. return withGRPCConnectionPool(size)
  125. }
  126. type withGRPCConnectionPool int
  127. func (w withGRPCConnectionPool) Apply(o *internal.DialSettings) {
  128. balancer := grpc.RoundRobin(internal.NewPoolResolver(int(w), o))
  129. o.GRPCDialOpts = append(o.GRPCDialOpts, grpc.WithBalancer(balancer))
  130. }
  131. // WithAPIKey returns a ClientOption that specifies an API key to be used
  132. // as the basis for authentication.
  133. //
  134. // API Keys can only be used for JSON-over-HTTP APIs, including those under
  135. // the import path google.golang.org/api/....
  136. func WithAPIKey(apiKey string) ClientOption {
  137. return withAPIKey(apiKey)
  138. }
  139. type withAPIKey string
  140. func (w withAPIKey) Apply(o *internal.DialSettings) { o.APIKey = string(w) }
  141. // WithAudiences returns a ClientOption that specifies an audience to be used
  142. // as the audience field ("aud") for the JWT token authentication.
  143. func WithAudiences(audience ...string) ClientOption {
  144. return withAudiences(audience)
  145. }
  146. type withAudiences []string
  147. func (w withAudiences) Apply(o *internal.DialSettings) {
  148. o.Audiences = make([]string, len(w))
  149. copy(o.Audiences, w)
  150. }
  151. // WithoutAuthentication returns a ClientOption that specifies that no
  152. // authentication should be used. It is suitable only for testing and for
  153. // accessing public resources, like public Google Cloud Storage buckets.
  154. // It is an error to provide both WithoutAuthentication and any of WithAPIKey,
  155. // WithTokenSource, WithCredentialsFile or WithServiceAccountFile.
  156. func WithoutAuthentication() ClientOption {
  157. return withoutAuthentication{}
  158. }
  159. type withoutAuthentication struct{}
  160. func (w withoutAuthentication) Apply(o *internal.DialSettings) { o.NoAuth = true }
  161. // WithQuotaProject returns a ClientOption that specifies the project used
  162. // for quota and billing purposes.
  163. //
  164. // For more information please read:
  165. // https://cloud.google.com/apis/docs/system-parameters
  166. func WithQuotaProject(quotaProject string) ClientOption {
  167. return withQuotaProject(quotaProject)
  168. }
  169. type withQuotaProject string
  170. func (w withQuotaProject) Apply(o *internal.DialSettings) {
  171. o.QuotaProject = string(w)
  172. }
  173. // WithRequestReason returns a ClientOption that specifies a reason for
  174. // making the request, which is intended to be recorded in audit logging.
  175. // An example reason would be a support-case ticket number.
  176. //
  177. // For more information please read:
  178. // https://cloud.google.com/apis/docs/system-parameters
  179. func WithRequestReason(requestReason string) ClientOption {
  180. return withRequestReason(requestReason)
  181. }
  182. type withRequestReason string
  183. func (w withRequestReason) Apply(o *internal.DialSettings) {
  184. o.RequestReason = string(w)
  185. }