• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 Google Inc. All Rights Reserved.
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// WithEndpoint returns a ClientOption that overrides the default endpoint
65// to be used for a service.
66func WithEndpoint(url string) ClientOption {
67	return withEndpoint(url)
68}
69
70type withEndpoint string
71
72func (w withEndpoint) Apply(o *internal.DialSettings) {
73	o.Endpoint = string(w)
74}
75
76// WithScopes returns a ClientOption that overrides the default OAuth2 scopes
77// to be used for a service.
78func WithScopes(scope ...string) ClientOption {
79	return withScopes(scope)
80}
81
82type withScopes []string
83
84func (w withScopes) Apply(o *internal.DialSettings) {
85	s := make([]string, len(w))
86	copy(s, w)
87	o.Scopes = s
88}
89
90// WithUserAgent returns a ClientOption that sets the User-Agent.
91func WithUserAgent(ua string) ClientOption {
92	return withUA(ua)
93}
94
95type withUA string
96
97func (w withUA) Apply(o *internal.DialSettings) { o.UserAgent = string(w) }
98
99// WithHTTPClient returns a ClientOption that specifies the HTTP client to use
100// as the basis of communications. This option may only be used with services
101// that support HTTP as their communication transport. When used, the
102// WithHTTPClient option takes precedent over all other supplied options.
103func WithHTTPClient(client *http.Client) ClientOption {
104	return withHTTPClient{client}
105}
106
107type withHTTPClient struct{ client *http.Client }
108
109func (w withHTTPClient) Apply(o *internal.DialSettings) {
110	o.HTTPClient = w.client
111}
112
113// WithGRPCConn returns a ClientOption that specifies the gRPC client
114// connection to use as the basis of communications. This option many only be
115// used with services that support gRPC as their communication transport. When
116// used, the WithGRPCConn option takes precedent over all other supplied
117// options.
118func WithGRPCConn(conn *grpc.ClientConn) ClientOption {
119	return withGRPCConn{conn}
120}
121
122type withGRPCConn struct{ conn *grpc.ClientConn }
123
124func (w withGRPCConn) Apply(o *internal.DialSettings) {
125	o.GRPCConn = w.conn
126}
127
128// WithGRPCDialOption returns a ClientOption that appends a new grpc.DialOption
129// to an underlying gRPC dial. It does not work with WithGRPCConn.
130func WithGRPCDialOption(opt grpc.DialOption) ClientOption {
131	return withGRPCDialOption{opt}
132}
133
134type withGRPCDialOption struct{ opt grpc.DialOption }
135
136func (w withGRPCDialOption) Apply(o *internal.DialSettings) {
137	o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt)
138}
139
140// WithGRPCConnectionPool returns a ClientOption that creates a pool of gRPC
141// connections that requests will be balanced between.
142// This is an EXPERIMENTAL API and may be changed or removed in the future.
143func WithGRPCConnectionPool(size int) ClientOption {
144	return withGRPCConnectionPool(size)
145}
146
147type withGRPCConnectionPool int
148
149func (w withGRPCConnectionPool) Apply(o *internal.DialSettings) {
150	balancer := grpc.RoundRobin(internal.NewPoolResolver(int(w), o))
151	o.GRPCDialOpts = append(o.GRPCDialOpts, grpc.WithBalancer(balancer))
152}
153
154// WithAPIKey returns a ClientOption that specifies an API key to be used
155// as the basis for authentication.
156func WithAPIKey(apiKey string) ClientOption {
157	return withAPIKey(apiKey)
158}
159
160type withAPIKey string
161
162func (w withAPIKey) Apply(o *internal.DialSettings) { o.APIKey = string(w) }
163