diff options
author | Nathan Dench <ndenc2@gmail.com> | 2019-05-24 15:16:44 +1000 |
---|---|---|
committer | Nathan Dench <ndenc2@gmail.com> | 2019-05-24 15:16:44 +1000 |
commit | 107c1cdb09c575aa2f61d97f48d8587eb6bada4c (patch) | |
tree | ca7d008643efc555c388baeaf1d986e0b6b3e28c /vendor/google.golang.org/api/gensupport/send.go | |
parent | 844b5a68d8af4791755b8f0ad293cc99f5959183 (diff) | |
download | terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.gz terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.zst terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.zip |
Upgrade to 0.12
Diffstat (limited to 'vendor/google.golang.org/api/gensupport/send.go')
-rw-r--r-- | vendor/google.golang.org/api/gensupport/send.go | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/vendor/google.golang.org/api/gensupport/send.go b/vendor/google.golang.org/api/gensupport/send.go new file mode 100644 index 0000000..5799393 --- /dev/null +++ b/vendor/google.golang.org/api/gensupport/send.go | |||
@@ -0,0 +1,87 @@ | |||
1 | // Copyright 2016 The Go Authors. All rights reserved. | ||
2 | // Use of this source code is governed by a BSD-style | ||
3 | // license that can be found in the LICENSE file. | ||
4 | |||
5 | package gensupport | ||
6 | |||
7 | import ( | ||
8 | "context" | ||
9 | "encoding/json" | ||
10 | "errors" | ||
11 | "net/http" | ||
12 | ) | ||
13 | |||
14 | // Hook is the type of a function that is called once before each HTTP request | ||
15 | // that is sent by a generated API. It returns a function that is called after | ||
16 | // the request returns. | ||
17 | // Hooks are not called if the context is nil. | ||
18 | type Hook func(ctx context.Context, req *http.Request) func(resp *http.Response) | ||
19 | |||
20 | var hooks []Hook | ||
21 | |||
22 | // RegisterHook registers a Hook to be called before each HTTP request by a | ||
23 | // generated API. Hooks are called in the order they are registered. Each | ||
24 | // hook can return a function; if it is non-nil, it is called after the HTTP | ||
25 | // request returns. These functions are called in the reverse order. | ||
26 | // RegisterHook should not be called concurrently with itself or SendRequest. | ||
27 | func RegisterHook(h Hook) { | ||
28 | hooks = append(hooks, h) | ||
29 | } | ||
30 | |||
31 | // SendRequest sends a single HTTP request using the given client. | ||
32 | // If ctx is non-nil, it calls all hooks, then sends the request with | ||
33 | // req.WithContext, then calls any functions returned by the hooks in | ||
34 | // reverse order. | ||
35 | func SendRequest(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) { | ||
36 | // Disallow Accept-Encoding because it interferes with the automatic gzip handling | ||
37 | // done by the default http.Transport. See https://github.com/google/google-api-go-client/issues/219. | ||
38 | if _, ok := req.Header["Accept-Encoding"]; ok { | ||
39 | return nil, errors.New("google api: custom Accept-Encoding headers not allowed") | ||
40 | } | ||
41 | if ctx == nil { | ||
42 | return client.Do(req) | ||
43 | } | ||
44 | // Call hooks in order of registration, store returned funcs. | ||
45 | post := make([]func(resp *http.Response), len(hooks)) | ||
46 | for i, h := range hooks { | ||
47 | fn := h(ctx, req) | ||
48 | post[i] = fn | ||
49 | } | ||
50 | |||
51 | // Send request. | ||
52 | resp, err := send(ctx, client, req) | ||
53 | |||
54 | // Call returned funcs in reverse order. | ||
55 | for i := len(post) - 1; i >= 0; i-- { | ||
56 | if fn := post[i]; fn != nil { | ||
57 | fn(resp) | ||
58 | } | ||
59 | } | ||
60 | return resp, err | ||
61 | } | ||
62 | |||
63 | func send(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) { | ||
64 | if client == nil { | ||
65 | client = http.DefaultClient | ||
66 | } | ||
67 | resp, err := client.Do(req.WithContext(ctx)) | ||
68 | // If we got an error, and the context has been canceled, | ||
69 | // the context's error is probably more useful. | ||
70 | if err != nil { | ||
71 | select { | ||
72 | case <-ctx.Done(): | ||
73 | err = ctx.Err() | ||
74 | default: | ||
75 | } | ||
76 | } | ||
77 | return resp, err | ||
78 | } | ||
79 | |||
80 | // DecodeResponse decodes the body of res into target. If there is no body, | ||
81 | // target is unchanged. | ||
82 | func DecodeResponse(target interface{}, res *http.Response) error { | ||
83 | if res.StatusCode == http.StatusNoContent { | ||
84 | return nil | ||
85 | } | ||
86 | return json.NewDecoder(res.Body).Decode(target) | ||
87 | } | ||