aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/google.golang.org/api/option/option.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/google.golang.org/api/option/option.go')
-rw-r--r--vendor/google.golang.org/api/option/option.go191
1 files changed, 191 insertions, 0 deletions
diff --git a/vendor/google.golang.org/api/option/option.go b/vendor/google.golang.org/api/option/option.go
new file mode 100644
index 0000000..e7ecfe3
--- /dev/null
+++ b/vendor/google.golang.org/api/option/option.go
@@ -0,0 +1,191 @@
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
15// Package option contains options for Google API clients.
16package option
17
18import (
19 "net/http"
20
21 "golang.org/x/oauth2"
22 "google.golang.org/api/internal"
23 "google.golang.org/grpc"
24)
25
26// A ClientOption is an option for a Google API client.
27type ClientOption interface {
28 Apply(*internal.DialSettings)
29}
30
31// WithTokenSource returns a ClientOption that specifies an OAuth2 token
32// source to be used as the basis for authentication.
33func WithTokenSource(s oauth2.TokenSource) ClientOption {
34 return withTokenSource{s}
35}
36
37type withTokenSource struct{ ts oauth2.TokenSource }
38
39func (w withTokenSource) Apply(o *internal.DialSettings) {
40 o.TokenSource = w.ts
41}
42
43type withCredFile string
44
45func (w withCredFile) Apply(o *internal.DialSettings) {
46 o.CredentialsFile = string(w)
47}
48
49// WithCredentialsFile returns a ClientOption that authenticates
50// API calls with the given service account or refresh token JSON
51// credentials file.
52func WithCredentialsFile(filename string) ClientOption {
53 return withCredFile(filename)
54}
55
56// WithServiceAccountFile returns a ClientOption that uses a Google service
57// account credentials file to authenticate.
58//
59// Deprecated: Use WithCredentialsFile instead.
60func WithServiceAccountFile(filename string) ClientOption {
61 return WithCredentialsFile(filename)
62}
63
64// WithCredentialsJSON returns a ClientOption that authenticates
65// API calls with the given service account or refresh token JSON
66// credentials.
67func WithCredentialsJSON(p []byte) ClientOption {
68 return withCredentialsJSON(p)
69}
70
71type withCredentialsJSON []byte
72
73func (w withCredentialsJSON) Apply(o *internal.DialSettings) {
74 o.CredentialsJSON = make([]byte, len(w))
75 copy(o.CredentialsJSON, w)
76}
77
78// WithEndpoint returns a ClientOption that overrides the default endpoint
79// to be used for a service.
80func WithEndpoint(url string) ClientOption {
81 return withEndpoint(url)
82}
83
84type withEndpoint string
85
86func (w withEndpoint) Apply(o *internal.DialSettings) {
87 o.Endpoint = string(w)
88}
89
90// WithScopes returns a ClientOption that overrides the default OAuth2 scopes
91// to be used for a service.
92func WithScopes(scope ...string) ClientOption {
93 return withScopes(scope)
94}
95
96type withScopes []string
97
98func (w withScopes) Apply(o *internal.DialSettings) {
99 o.Scopes = make([]string, len(w))
100 copy(o.Scopes, w)
101}
102
103// WithUserAgent returns a ClientOption that sets the User-Agent.
104func WithUserAgent(ua string) ClientOption {
105 return withUA(ua)
106}
107
108type withUA string
109
110func (w withUA) Apply(o *internal.DialSettings) { o.UserAgent = string(w) }
111
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.
116func WithHTTPClient(client *http.Client) ClientOption {
117 return withHTTPClient{client}
118}
119
120type withHTTPClient struct{ client *http.Client }
121
122func (w withHTTPClient) Apply(o *internal.DialSettings) {
123 o.HTTPClient = w.client
124}
125
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
130// options.
131func WithGRPCConn(conn *grpc.ClientConn) ClientOption {
132 return withGRPCConn{conn}
133}
134
135type withGRPCConn struct{ conn *grpc.ClientConn }
136
137func (w withGRPCConn) Apply(o *internal.DialSettings) {
138 o.GRPCConn = w.conn
139}
140
141// WithGRPCDialOption returns a ClientOption that appends a new grpc.DialOption
142// to an underlying gRPC dial. It does not work with WithGRPCConn.
143func WithGRPCDialOption(opt grpc.DialOption) ClientOption {
144 return withGRPCDialOption{opt}
145}
146
147type withGRPCDialOption struct{ opt grpc.DialOption }
148
149func (w withGRPCDialOption) Apply(o *internal.DialSettings) {
150 o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt)
151}
152
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.
156func WithGRPCConnectionPool(size int) ClientOption {
157 return withGRPCConnectionPool(size)
158}
159
160type withGRPCConnectionPool int
161
162func (w withGRPCConnectionPool) Apply(o *internal.DialSettings) {
163 balancer := grpc.RoundRobin(internal.NewPoolResolver(int(w), o))
164 o.GRPCDialOpts = append(o.GRPCDialOpts, grpc.WithBalancer(balancer))
165}
166
167// WithAPIKey returns a ClientOption that specifies an API key to be used
168// as the basis for authentication.
169//
170// API Keys can only be used for JSON-over-HTTP APIs, including those under
171// the import path google.golang.org/api/....
172func WithAPIKey(apiKey string) ClientOption {
173 return withAPIKey(apiKey)
174}
175
176type withAPIKey string
177
178func (w withAPIKey) Apply(o *internal.DialSettings) { o.APIKey = string(w) }
179
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.
185func WithoutAuthentication() ClientOption {
186 return withoutAuthentication{}
187}
188
189type withoutAuthentication struct{}
190
191func (w withoutAuthentication) Apply(o *internal.DialSettings) { o.NoAuth = true }