option.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Package option contains options for Google API clients.
  2. package option
  3. import (
  4. "net/http"
  5. "golang.org/x/oauth2"
  6. "google.golang.org/api/internal"
  7. "google.golang.org/grpc"
  8. )
  9. // A ClientOption is an option for a Google API client.
  10. type ClientOption interface {
  11. Apply(*internal.DialSettings)
  12. }
  13. // WithTokenSource returns a ClientOption that specifies an OAuth2 token
  14. // source to be used as the basis for authentication.
  15. func WithTokenSource(s oauth2.TokenSource) ClientOption {
  16. return withTokenSource{s}
  17. }
  18. type withTokenSource struct{ ts oauth2.TokenSource }
  19. func (w withTokenSource) Apply(o *internal.DialSettings) {
  20. o.TokenSource = w.ts
  21. }
  22. // WithServiceAccountFile returns a ClientOption that uses a Google service
  23. // account credentials file to authenticate.
  24. // Use WithTokenSource with a token source created from
  25. // golang.org/x/oauth2/google.JWTConfigFromJSON
  26. // if reading the file from disk is not an option.
  27. func WithServiceAccountFile(filename string) ClientOption {
  28. return withServiceAccountFile(filename)
  29. }
  30. type withServiceAccountFile string
  31. func (w withServiceAccountFile) Apply(o *internal.DialSettings) {
  32. o.ServiceAccountJSONFilename = string(w)
  33. }
  34. // WithEndpoint returns a ClientOption that overrides the default endpoint
  35. // to be used for a service.
  36. func WithEndpoint(url string) ClientOption {
  37. return withEndpoint(url)
  38. }
  39. type withEndpoint string
  40. func (w withEndpoint) Apply(o *internal.DialSettings) {
  41. o.Endpoint = string(w)
  42. }
  43. // WithScopes returns a ClientOption that overrides the default OAuth2 scopes
  44. // to be used for a service.
  45. func WithScopes(scope ...string) ClientOption {
  46. return withScopes(scope)
  47. }
  48. type withScopes []string
  49. func (w withScopes) Apply(o *internal.DialSettings) {
  50. s := make([]string, len(w))
  51. copy(s, w)
  52. o.Scopes = s
  53. }
  54. // WithUserAgent returns a ClientOption that sets the User-Agent.
  55. func WithUserAgent(ua string) ClientOption {
  56. return withUA(ua)
  57. }
  58. type withUA string
  59. func (w withUA) Apply(o *internal.DialSettings) { o.UserAgent = string(w) }
  60. // WithHTTPClient returns a ClientOption that specifies the HTTP client to use
  61. // as the basis of communications. This option may only be used with services
  62. // that support HTTP as their communication transport. When used, the
  63. // WithHTTPClient option takes precedent over all other supplied options.
  64. func WithHTTPClient(client *http.Client) ClientOption {
  65. return withHTTPClient{client}
  66. }
  67. type withHTTPClient struct{ client *http.Client }
  68. func (w withHTTPClient) Apply(o *internal.DialSettings) {
  69. o.HTTPClient = w.client
  70. }
  71. // WithGRPCConn 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. When
  74. // used, the WithGRPCConn option takes precedent over all other supplied
  75. // options.
  76. func WithGRPCConn(conn *grpc.ClientConn) ClientOption {
  77. return withGRPCConn{conn}
  78. }
  79. type withGRPCConn struct{ conn *grpc.ClientConn }
  80. func (w withGRPCConn) Apply(o *internal.DialSettings) {
  81. o.GRPCConn = w.conn
  82. }
  83. // WithGRPCDialOption returns a ClientOption that appends a new grpc.DialOption
  84. // to an underlying gRPC dial. It does not work with WithGRPCConn.
  85. func WithGRPCDialOption(opt grpc.DialOption) ClientOption {
  86. return withGRPCDialOption{opt}
  87. }
  88. type withGRPCDialOption struct{ opt grpc.DialOption }
  89. func (w withGRPCDialOption) Apply(o *internal.DialSettings) {
  90. o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt)
  91. }
  92. // WithGRPCConnectionPool returns a ClientOption that creates a pool of gRPC
  93. // connections that requests will be balanced between.
  94. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  95. func WithGRPCConnectionPool(size int) ClientOption {
  96. return withGRPCConnectionPool(size)
  97. }
  98. type withGRPCConnectionPool int
  99. func (w withGRPCConnectionPool) Apply(o *internal.DialSettings) {
  100. balancer := grpc.RoundRobin(internal.NewPoolResolver(int(w), o))
  101. o.GRPCDialOpts = append(o.GRPCDialOpts, grpc.WithBalancer(balancer))
  102. }
  103. // WithAPIKey returns a ClientOption that specifies an API key to be used
  104. // as the basis for authentication.
  105. func WithAPIKey(apiKey string) ClientOption {
  106. return withAPIKey(apiKey)
  107. }
  108. type withAPIKey string
  109. func (w withAPIKey) Apply(o *internal.DialSettings) { o.APIKey = string(w) }