option.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2015 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 cloud
  15. import (
  16. "net/http"
  17. "golang.org/x/oauth2"
  18. "google.golang.org/cloud/internal/opts"
  19. "google.golang.org/grpc"
  20. )
  21. // ClientOption is used when construct clients for each cloud service.
  22. type ClientOption interface {
  23. // Resolve configures the given DialOpts for this option.
  24. Resolve(*opts.DialOpt)
  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) Resolve(o *opts.DialOpt) {
  33. o.TokenSource = w.ts
  34. }
  35. // WithEndpoint returns a ClientOption that overrides the default endpoint
  36. // to be used for a service.
  37. func WithEndpoint(url string) ClientOption {
  38. return withEndpoint(url)
  39. }
  40. type withEndpoint string
  41. func (w withEndpoint) Resolve(o *opts.DialOpt) {
  42. o.Endpoint = string(w)
  43. }
  44. // WithScopes returns a ClientOption that overrides the default OAuth2 scopes
  45. // to be used for a service.
  46. func WithScopes(scope ...string) ClientOption {
  47. return withScopes(scope)
  48. }
  49. type withScopes []string
  50. func (w withScopes) Resolve(o *opts.DialOpt) {
  51. s := make([]string, len(w))
  52. copy(s, w)
  53. o.Scopes = s
  54. }
  55. // WithUserAgent returns a ClientOption that sets the User-Agent.
  56. func WithUserAgent(ua string) ClientOption {
  57. return withUA(ua)
  58. }
  59. type withUA string
  60. func (w withUA) Resolve(o *opts.DialOpt) { o.UserAgent = string(w) }
  61. // WithBaseHTTP returns a ClientOption that specifies the HTTP client to
  62. // use as the basis of communications. This option may only be used with
  63. // services that support HTTP as their communication transport.
  64. func WithBaseHTTP(client *http.Client) ClientOption {
  65. return withBaseHTTP{client}
  66. }
  67. type withBaseHTTP struct{ client *http.Client }
  68. func (w withBaseHTTP) Resolve(o *opts.DialOpt) {
  69. o.HTTPClient = w.client
  70. }
  71. // WithBaseGRPC returns a ClientOption that specifies the gRPC client
  72. // connection to use as the basis of communications. This option many only be
  73. // used with services that support gRPC as their communication transport.
  74. func WithBaseGRPC(client *grpc.ClientConn) ClientOption {
  75. return withBaseGRPC{client}
  76. }
  77. type withBaseGRPC struct{ client *grpc.ClientConn }
  78. func (w withBaseGRPC) Resolve(o *opts.DialOpt) {
  79. o.GRPCClient = w.client
  80. }
  81. // WithGRPCDialOption returns a ClientOption that appends a new grpc.DialOption
  82. // to an underlying gRPC dial. It does not work with WithBaseGRPC.
  83. func WithGRPCDialOption(opt grpc.DialOption) ClientOption {
  84. return withGRPCDialOption{opt}
  85. }
  86. type withGRPCDialOption struct{ opt grpc.DialOption }
  87. func (w withGRPCDialOption) Resolve(o *opts.DialOpt) {
  88. o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt)
  89. }