1 // Copyright 2017 Google LLC
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // Package option contains options for Google API clients.
22 "google.golang.org/api/internal"
23 "google.golang.org/grpc"
26 // A ClientOption is an option for a Google API client.
27 type ClientOption interface {
28 Apply(*internal.DialSettings)
31 // WithTokenSource returns a ClientOption that specifies an OAuth2 token
32 // source to be used as the basis for authentication.
33 func WithTokenSource(s oauth2.TokenSource) ClientOption {
34 return withTokenSource{s}
37 type withTokenSource struct{ ts oauth2.TokenSource }
39 func (w withTokenSource) Apply(o *internal.DialSettings) {
43 type withCredFile string
45 func (w withCredFile) Apply(o *internal.DialSettings) {
46 o.CredentialsFile = string(w)
49 // WithCredentialsFile returns a ClientOption that authenticates
50 // API calls with the given service account or refresh token JSON
52 func WithCredentialsFile(filename string) ClientOption {
53 return withCredFile(filename)
56 // WithServiceAccountFile returns a ClientOption that uses a Google service
57 // account credentials file to authenticate.
59 // Deprecated: Use WithCredentialsFile instead.
60 func WithServiceAccountFile(filename string) ClientOption {
61 return WithCredentialsFile(filename)
64 // WithCredentialsJSON returns a ClientOption that authenticates
65 // API calls with the given service account or refresh token JSON
67 func WithCredentialsJSON(p []byte) ClientOption {
68 return withCredentialsJSON(p)
71 type withCredentialsJSON []byte
73 func (w withCredentialsJSON) Apply(o *internal.DialSettings) {
74 o.CredentialsJSON = make([]byte, len(w))
75 copy(o.CredentialsJSON, w)
78 // WithEndpoint returns a ClientOption that overrides the default endpoint
79 // to be used for a service.
80 func WithEndpoint(url string) ClientOption {
81 return withEndpoint(url)
84 type withEndpoint string
86 func (w withEndpoint) Apply(o *internal.DialSettings) {
87 o.Endpoint = string(w)
90 // WithScopes returns a ClientOption that overrides the default OAuth2 scopes
91 // to be used for a service.
92 func WithScopes(scope ...string) ClientOption {
93 return withScopes(scope)
96 type withScopes []string
98 func (w withScopes) Apply(o *internal.DialSettings) {
99 o.Scopes = make([]string, len(w))
103 // WithUserAgent returns a ClientOption that sets the User-Agent.
104 func WithUserAgent(ua string) ClientOption {
110 func (w withUA) Apply(o *internal.DialSettings) { o.UserAgent = string(w) }
112 // WithHTTPClient returns a ClientOption that specifies the HTTP client to use
113 // as the basis of communications. This option may only be used with services
114 // that support HTTP as their communication transport. When used, the
115 // WithHTTPClient option takes precedent over all other supplied options.
116 func WithHTTPClient(client *http.Client) ClientOption {
117 return withHTTPClient{client}
120 type withHTTPClient struct{ client *http.Client }
122 func (w withHTTPClient) Apply(o *internal.DialSettings) {
123 o.HTTPClient = w.client
126 // WithGRPCConn returns a ClientOption that specifies the gRPC client
127 // connection to use as the basis of communications. This option many only be
128 // used with services that support gRPC as their communication transport. When
129 // used, the WithGRPCConn option takes precedent over all other supplied
131 func WithGRPCConn(conn *grpc.ClientConn) ClientOption {
132 return withGRPCConn{conn}
135 type withGRPCConn struct{ conn *grpc.ClientConn }
137 func (w withGRPCConn) Apply(o *internal.DialSettings) {
141 // WithGRPCDialOption returns a ClientOption that appends a new grpc.DialOption
142 // to an underlying gRPC dial. It does not work with WithGRPCConn.
143 func WithGRPCDialOption(opt grpc.DialOption) ClientOption {
144 return withGRPCDialOption{opt}
147 type withGRPCDialOption struct{ opt grpc.DialOption }
149 func (w withGRPCDialOption) Apply(o *internal.DialSettings) {
150 o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt)
153 // WithGRPCConnectionPool returns a ClientOption that creates a pool of gRPC
154 // connections that requests will be balanced between.
155 // This is an EXPERIMENTAL API and may be changed or removed in the future.
156 func WithGRPCConnectionPool(size int) ClientOption {
157 return withGRPCConnectionPool(size)
160 type withGRPCConnectionPool int
162 func (w withGRPCConnectionPool) Apply(o *internal.DialSettings) {
163 balancer := grpc.RoundRobin(internal.NewPoolResolver(int(w), o))
164 o.GRPCDialOpts = append(o.GRPCDialOpts, grpc.WithBalancer(balancer))
167 // WithAPIKey returns a ClientOption that specifies an API key to be used
168 // as the basis for authentication.
170 // API Keys can only be used for JSON-over-HTTP APIs, including those under
171 // the import path google.golang.org/api/....
172 func WithAPIKey(apiKey string) ClientOption {
173 return withAPIKey(apiKey)
176 type withAPIKey string
178 func (w withAPIKey) Apply(o *internal.DialSettings) { o.APIKey = string(w) }
180 // WithoutAuthentication returns a ClientOption that specifies that no
181 // authentication should be used. It is suitable only for testing and for
182 // accessing public resources, like public Google Cloud Storage buckets.
183 // It is an error to provide both WithoutAuthentication and any of WithAPIKey,
184 // WithTokenSource, WithCredentialsFile or WithServiceAccountFile.
185 func WithoutAuthentication() ClientOption {
186 return withoutAuthentication{}
189 type withoutAuthentication struct{}
191 func (w withoutAuthentication) Apply(o *internal.DialSettings) { o.NoAuth = true }