]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/google.golang.org/api/transport/http/dial.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / google.golang.org / api / transport / http / dial.go
1 // Copyright 2015 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 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.
18 package http
19
20 import (
21 "context"
22 "errors"
23 "net/http"
24
25 "go.opencensus.io/plugin/ochttp"
26 "golang.org/x/oauth2"
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"
31 )
32
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)
38 if err != nil {
39 return nil, "", err
40 }
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
44 }
45 trans, err := newTransport(ctx, defaultBaseTransport(ctx), settings)
46 if err != nil {
47 return nil, "", err
48 }
49 return &http.Client{Transport: trans}, settings.Endpoint, nil
50 }
51
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)
56 if err != nil {
57 return nil, err
58 }
59 if settings.HTTPClient != nil {
60 return nil, errors.New("transport/http: WithHTTPClient passed to NewTransport")
61 }
62 return newTransport(ctx, base, settings)
63 }
64
65 func newTransport(ctx context.Context, base http.RoundTripper, settings *internal.DialSettings) (http.RoundTripper, error) {
66 trans := base
67 trans = userAgentTransport{
68 base: trans,
69 userAgent: settings.UserAgent,
70 }
71 trans = addOCTransport(trans)
72 switch {
73 case settings.NoAuth:
74 // Do nothing.
75 case settings.APIKey != "":
76 trans = &transport.APIKey{
77 Transport: trans,
78 Key: settings.APIKey,
79 }
80 default:
81 creds, err := internal.Creds(ctx, settings)
82 if err != nil {
83 return nil, err
84 }
85 trans = &oauth2.Transport{
86 Base: trans,
87 Source: creds.TokenSource,
88 }
89 }
90 return trans, nil
91 }
92
93 func newSettings(opts []option.ClientOption) (*internal.DialSettings, error) {
94 var o internal.DialSettings
95 for _, opt := range opts {
96 opt.Apply(&o)
97 }
98 if err := o.Validate(); err != nil {
99 return nil, err
100 }
101 if o.GRPCConn != nil {
102 return nil, errors.New("unsupported gRPC connection specified")
103 }
104 return &o, nil
105 }
106
107 type userAgentTransport struct {
108 userAgent string
109 base http.RoundTripper
110 }
111
112 func (t userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
113 rt := t.base
114 if rt == nil {
115 return nil, errors.New("transport: no Transport specified")
116 }
117 if t.userAgent == "" {
118 return rt.RoundTrip(req)
119 }
120 newReq := *req
121 newReq.Header = make(http.Header)
122 for k, vv := range req.Header {
123 newReq.Header[k] = vv
124 }
125 // TODO(cbro): append to existing User-Agent header?
126 newReq.Header["User-Agent"] = []string{t.userAgent}
127 return rt.RoundTrip(&newReq)
128 }
129
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
132
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)
138 }
139 return http.DefaultTransport
140 }
141
142 func addOCTransport(trans http.RoundTripper) http.RoundTripper {
143 return &ochttp.Transport{
144 Base: trans,
145 Propagation: &propagation.HTTPFormat{},
146 }
147 }