internaloption.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2020 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 internaloption contains options used internally by Google client code.
  5. package internaloption
  6. import (
  7. "golang.org/x/oauth2/google"
  8. "google.golang.org/api/internal"
  9. "google.golang.org/api/option"
  10. )
  11. type defaultEndpointOption string
  12. func (o defaultEndpointOption) Apply(settings *internal.DialSettings) {
  13. settings.DefaultEndpoint = string(o)
  14. }
  15. // WithDefaultEndpoint is an option that indicates the default endpoint.
  16. //
  17. // It should only be used internally by generated clients.
  18. //
  19. // This is similar to WithEndpoint, but allows us to determine whether the user has overridden the default endpoint.
  20. func WithDefaultEndpoint(url string) option.ClientOption {
  21. return defaultEndpointOption(url)
  22. }
  23. type defaultMTLSEndpointOption string
  24. func (o defaultMTLSEndpointOption) Apply(settings *internal.DialSettings) {
  25. settings.DefaultMTLSEndpoint = string(o)
  26. }
  27. // WithDefaultMTLSEndpoint is an option that indicates the default mTLS endpoint.
  28. //
  29. // It should only be used internally by generated clients.
  30. func WithDefaultMTLSEndpoint(url string) option.ClientOption {
  31. return defaultMTLSEndpointOption(url)
  32. }
  33. // SkipDialSettingsValidation bypasses validation on ClientOptions.
  34. //
  35. // It should only be used internally.
  36. func SkipDialSettingsValidation() option.ClientOption {
  37. return skipDialSettingsValidation{}
  38. }
  39. type skipDialSettingsValidation struct{}
  40. func (s skipDialSettingsValidation) Apply(settings *internal.DialSettings) {
  41. settings.SkipValidation = true
  42. }
  43. // EnableDirectPath returns a ClientOption that overrides the default
  44. // attempt to use DirectPath.
  45. //
  46. // It should only be used internally by generated clients.
  47. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  48. func EnableDirectPath(dp bool) option.ClientOption {
  49. return enableDirectPath(dp)
  50. }
  51. type enableDirectPath bool
  52. func (e enableDirectPath) Apply(o *internal.DialSettings) {
  53. o.EnableDirectPath = bool(e)
  54. }
  55. // EnableDirectPathXds returns a ClientOption that overrides the default
  56. // DirectPath type. It is only valid when DirectPath is enabled.
  57. //
  58. // It should only be used internally by generated clients.
  59. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  60. func EnableDirectPathXds() option.ClientOption {
  61. return enableDirectPathXds(true)
  62. }
  63. type enableDirectPathXds bool
  64. func (x enableDirectPathXds) Apply(o *internal.DialSettings) {
  65. o.EnableDirectPathXds = bool(x)
  66. }
  67. // AllowNonDefaultServiceAccount returns a ClientOption that overrides the default
  68. // requirement for using the default service account for DirectPath.
  69. //
  70. // It should only be used internally by generated clients.
  71. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  72. func AllowNonDefaultServiceAccount(nd bool) option.ClientOption {
  73. return allowNonDefaultServiceAccount(nd)
  74. }
  75. type allowNonDefaultServiceAccount bool
  76. func (a allowNonDefaultServiceAccount) Apply(o *internal.DialSettings) {
  77. o.AllowNonDefaultServiceAccount = bool(a)
  78. }
  79. // WithDefaultAudience returns a ClientOption that specifies a default audience
  80. // to be used as the audience field ("aud") for the JWT token authentication.
  81. //
  82. // It should only be used internally by generated clients.
  83. func WithDefaultAudience(audience string) option.ClientOption {
  84. return withDefaultAudience(audience)
  85. }
  86. type withDefaultAudience string
  87. func (w withDefaultAudience) Apply(o *internal.DialSettings) {
  88. o.DefaultAudience = string(w)
  89. }
  90. // WithDefaultScopes returns a ClientOption that overrides the default OAuth2
  91. // scopes to be used for a service.
  92. //
  93. // It should only be used internally by generated clients.
  94. func WithDefaultScopes(scope ...string) option.ClientOption {
  95. return withDefaultScopes(scope)
  96. }
  97. type withDefaultScopes []string
  98. func (w withDefaultScopes) Apply(o *internal.DialSettings) {
  99. o.DefaultScopes = make([]string, len(w))
  100. copy(o.DefaultScopes, w)
  101. }
  102. // EnableJwtWithScope returns a ClientOption that specifies if scope can be used
  103. // with self-signed JWT.
  104. func EnableJwtWithScope() option.ClientOption {
  105. return enableJwtWithScope(true)
  106. }
  107. type enableJwtWithScope bool
  108. func (w enableJwtWithScope) Apply(o *internal.DialSettings) {
  109. o.EnableJwtWithScope = bool(w)
  110. }
  111. // WithCredentials returns a client option to specify credentials which will be used to authenticate API calls.
  112. // This credential takes precedence over all other credential options.
  113. func WithCredentials(creds *google.Credentials) option.ClientOption {
  114. return (*withCreds)(creds)
  115. }
  116. type withCreds google.Credentials
  117. func (w *withCreds) Apply(o *internal.DialSettings) {
  118. o.InternalCredentials = (*google.Credentials)(w)
  119. }
  120. // EmbeddableAdapter is a no-op option.ClientOption that allow libraries to
  121. // create their own client options by embedding this type into their own
  122. // client-specific option wrapper. See example for usage.
  123. type EmbeddableAdapter struct{}
  124. func (*EmbeddableAdapter) Apply(_ *internal.DialSettings) {}