1 // Copyright 2015 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 http supports network connections to HTTP servers.
16 // This package is not intended for use by end developers. Use the
17 // google.golang.org/api/option package to configure API clients.
25 "go.opencensus.io/plugin/ochttp"
27 "google.golang.org/api/googleapi/transport"
28 "google.golang.org/api/internal"
29 "google.golang.org/api/option"
30 "google.golang.org/api/transport/http/internal/propagation"
33 // NewClient returns an HTTP client for use communicating with a Google cloud
34 // service, configured with the given ClientOptions. It also returns the endpoint
35 // for the service as specified in the options.
36 func NewClient(ctx context.Context, opts ...option.ClientOption) (*http.Client, string, error) {
37 settings, err := newSettings(opts)
41 // TODO(cbro): consider injecting the User-Agent even if an explicit HTTP client is provided?
42 if settings.HTTPClient != nil {
43 return settings.HTTPClient, settings.Endpoint, nil
45 trans, err := newTransport(ctx, defaultBaseTransport(ctx), settings)
49 return &http.Client{Transport: trans}, settings.Endpoint, nil
52 // NewTransport creates an http.RoundTripper for use communicating with a Google
53 // cloud service, configured with the given ClientOptions. Its RoundTrip method delegates to base.
54 func NewTransport(ctx context.Context, base http.RoundTripper, opts ...option.ClientOption) (http.RoundTripper, error) {
55 settings, err := newSettings(opts)
59 if settings.HTTPClient != nil {
60 return nil, errors.New("transport/http: WithHTTPClient passed to NewTransport")
62 return newTransport(ctx, base, settings)
65 func newTransport(ctx context.Context, base http.RoundTripper, settings *internal.DialSettings) (http.RoundTripper, error) {
67 trans = userAgentTransport{
69 userAgent: settings.UserAgent,
71 trans = addOCTransport(trans)
75 case settings.APIKey != "":
76 trans = &transport.APIKey{
81 creds, err := internal.Creds(ctx, settings)
85 trans = &oauth2.Transport{
87 Source: creds.TokenSource,
93 func newSettings(opts []option.ClientOption) (*internal.DialSettings, error) {
94 var o internal.DialSettings
95 for _, opt := range opts {
98 if err := o.Validate(); err != nil {
101 if o.GRPCConn != nil {
102 return nil, errors.New("unsupported gRPC connection specified")
107 type userAgentTransport struct {
109 base http.RoundTripper
112 func (t userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
115 return nil, errors.New("transport: no Transport specified")
117 if t.userAgent == "" {
118 return rt.RoundTrip(req)
121 newReq.Header = make(http.Header)
122 for k, vv := range req.Header {
123 newReq.Header[k] = vv
125 // TODO(cbro): append to existing User-Agent header?
126 newReq.Header["User-Agent"] = []string{t.userAgent}
127 return rt.RoundTrip(&newReq)
130 // Set at init time by dial_appengine.go. If nil, we're not on App Engine.
131 var appengineUrlfetchHook func(context.Context) http.RoundTripper
133 // defaultBaseTransport returns the base HTTP transport.
134 // On App Engine, this is urlfetch.Transport, otherwise it's http.DefaultTransport.
135 func defaultBaseTransport(ctx context.Context) http.RoundTripper {
136 if appengineUrlfetchHook != nil {
137 return appengineUrlfetchHook(ctx)
139 return http.DefaultTransport
142 func addOCTransport(trans http.RoundTripper) http.RoundTripper {
143 return &ochttp.Transport{
145 Propagation: &propagation.HTTPFormat{},