aboutsummaryrefslogtreecommitdiff
path: root/vendor/google.golang.org/api/transport
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/google.golang.org/api/transport')
-rw-r--r--vendor/google.golang.org/api/transport/dial.go53
-rw-r--r--vendor/google.golang.org/api/transport/dial_appengine.go34
2 files changed, 49 insertions, 38 deletions
diff --git a/vendor/google.golang.org/api/transport/dial.go b/vendor/google.golang.org/api/transport/dial.go
index 9971eb8..a41ed0c 100644
--- a/vendor/google.golang.org/api/transport/dial.go
+++ b/vendor/google.golang.org/api/transport/dial.go
@@ -46,15 +46,18 @@ func NewHTTPClient(ctx context.Context, opts ...option.ClientOption) (*http.Clie
if o.GRPCConn != nil {
return nil, "", errors.New("unsupported gRPC connection specified")
}
- // TODO(djd): Set UserAgent on all outgoing requests.
+ // TODO(cbro): consider injecting the User-Agent even if an explicit HTTP client is provided?
if o.HTTPClient != nil {
return o.HTTPClient, o.Endpoint, nil
}
if o.APIKey != "" {
hc := &http.Client{
Transport: &gtransport.APIKey{
- Key: o.APIKey,
- Transport: http.DefaultTransport,
+ Key: o.APIKey,
+ Transport: userAgentTransport{
+ base: baseTransport(ctx),
+ userAgent: o.UserAgent,
+ },
},
}
return hc, o.Endpoint, nil
@@ -73,11 +76,53 @@ func NewHTTPClient(ctx context.Context, opts ...option.ClientOption) (*http.Clie
return nil, "", fmt.Errorf("google.DefaultTokenSource: %v", err)
}
}
- return oauth2.NewClient(ctx, o.TokenSource), o.Endpoint, nil
+ hc := &http.Client{
+ Transport: &oauth2.Transport{
+ Source: o.TokenSource,
+ Base: userAgentTransport{
+ base: baseTransport(ctx),
+ userAgent: o.UserAgent,
+ },
+ },
+ }
+ return hc, o.Endpoint, nil
+}
+
+type userAgentTransport struct {
+ userAgent string
+ base http.RoundTripper
+}
+
+func (t userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+ rt := t.base
+ if rt == nil {
+ return nil, errors.New("transport: no Transport specified")
+ }
+ if t.userAgent == "" {
+ return rt.RoundTrip(req)
+ }
+ newReq := *req
+ newReq.Header = make(http.Header)
+ for k, vv := range req.Header {
+ newReq.Header[k] = vv
+ }
+ // TODO(cbro): append to existing User-Agent header?
+ newReq.Header["User-Agent"] = []string{t.userAgent}
+ return rt.RoundTrip(&newReq)
}
// Set at init time by dial_appengine.go. If nil, we're not on App Engine.
var appengineDialerHook func(context.Context) grpc.DialOption
+var appengineUrlfetchHook func(context.Context) http.RoundTripper
+
+// baseTransport returns the base HTTP transport.
+// On App Engine, this is urlfetch.Transport, otherwise it's http.DefaultTransport.
+func baseTransport(ctx context.Context) http.RoundTripper {
+ if appengineUrlfetchHook != nil {
+ return appengineUrlfetchHook(ctx)
+ }
+ return http.DefaultTransport
+}
// DialGRPC returns a GRPC connection for use communicating with a Google cloud
// service, configured with the given ClientOptions.
diff --git a/vendor/google.golang.org/api/transport/dial_appengine.go b/vendor/google.golang.org/api/transport/dial_appengine.go
deleted file mode 100644
index 201244d..0000000
--- a/vendor/google.golang.org/api/transport/dial_appengine.go
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyright 2016 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-// +build appengine
-
-package transport
-
-import (
- "net"
- "time"
-
- "golang.org/x/net/context"
- "google.golang.org/appengine/socket"
- "google.golang.org/grpc"
-)
-
-func init() {
- appengineDialerHook = func(ctx context.Context) grpc.DialOption {
- return grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
- return socket.DialTimeout(ctx, "tcp", addr, timeout)
- })
- }
-}