diff options
Diffstat (limited to 'vendor/google.golang.org/api/internal/settings.go')
-rw-r--r-- | vendor/google.golang.org/api/internal/settings.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/vendor/google.golang.org/api/internal/settings.go b/vendor/google.golang.org/api/internal/settings.go new file mode 100644 index 0000000..afabdc4 --- /dev/null +++ b/vendor/google.golang.org/api/internal/settings.go | |||
@@ -0,0 +1,81 @@ | |||
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 internal supports the options and transport packages. | ||
16 | package internal | ||
17 | |||
18 | import ( | ||
19 | "errors" | ||
20 | "net/http" | ||
21 | |||
22 | "golang.org/x/oauth2" | ||
23 | "golang.org/x/oauth2/google" | ||
24 | "google.golang.org/grpc" | ||
25 | ) | ||
26 | |||
27 | // DialSettings holds information needed to establish a connection with a | ||
28 | // Google API service. | ||
29 | type DialSettings struct { | ||
30 | Endpoint string | ||
31 | Scopes []string | ||
32 | TokenSource oauth2.TokenSource | ||
33 | Credentials *google.DefaultCredentials | ||
34 | CredentialsFile string // if set, Token Source is ignored. | ||
35 | CredentialsJSON []byte | ||
36 | UserAgent string | ||
37 | APIKey string | ||
38 | HTTPClient *http.Client | ||
39 | GRPCDialOpts []grpc.DialOption | ||
40 | GRPCConn *grpc.ClientConn | ||
41 | NoAuth bool | ||
42 | } | ||
43 | |||
44 | // Validate reports an error if ds is invalid. | ||
45 | func (ds *DialSettings) Validate() error { | ||
46 | hasCreds := ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != "" || ds.Credentials != nil | ||
47 | if ds.NoAuth && hasCreds { | ||
48 | return errors.New("options.WithoutAuthentication is incompatible with any option that provides credentials") | ||
49 | } | ||
50 | // Credentials should not appear with other options. | ||
51 | // We currently allow TokenSource and CredentialsFile to coexist. | ||
52 | // TODO(jba): make TokenSource & CredentialsFile an error (breaking change). | ||
53 | nCreds := 0 | ||
54 | if ds.Credentials != nil { | ||
55 | nCreds++ | ||
56 | } | ||
57 | if ds.CredentialsJSON != nil { | ||
58 | nCreds++ | ||
59 | } | ||
60 | if ds.CredentialsFile != "" { | ||
61 | nCreds++ | ||
62 | } | ||
63 | if ds.APIKey != "" { | ||
64 | nCreds++ | ||
65 | } | ||
66 | if ds.TokenSource != nil { | ||
67 | nCreds++ | ||
68 | } | ||
69 | // Accept only one form of credentials, except we allow TokenSource and CredentialsFile for backwards compatibility. | ||
70 | if nCreds > 1 && !(nCreds == 2 && ds.TokenSource != nil && ds.CredentialsFile != "") { | ||
71 | return errors.New("multiple credential options provided") | ||
72 | } | ||
73 | if ds.HTTPClient != nil && ds.GRPCConn != nil { | ||
74 | return errors.New("WithHTTPClient is incompatible with WithGRPCConn") | ||
75 | } | ||
76 | if ds.HTTPClient != nil && ds.GRPCDialOpts != nil { | ||
77 | return errors.New("WithHTTPClient is incompatible with gRPC dial options") | ||
78 | } | ||
79 | |||
80 | return nil | ||
81 | } | ||