aboutsummaryrefslogtreecommitdiff
path: root/vendor/google.golang.org/api/gensupport
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/google.golang.org/api/gensupport')
-rw-r--r--vendor/google.golang.org/api/gensupport/resumable.go5
-rw-r--r--vendor/google.golang.org/api/gensupport/send.go55
2 files changed, 58 insertions, 2 deletions
diff --git a/vendor/google.golang.org/api/gensupport/resumable.go b/vendor/google.golang.org/api/gensupport/resumable.go
index ad16943..695e365 100644
--- a/vendor/google.golang.org/api/gensupport/resumable.go
+++ b/vendor/google.golang.org/api/gensupport/resumable.go
@@ -12,7 +12,6 @@ import (
"time"
"golang.org/x/net/context"
- "golang.org/x/net/context/ctxhttp"
)
const (
@@ -80,7 +79,7 @@ func (rx *ResumableUpload) doUploadRequest(ctx context.Context, data io.Reader,
req.Header.Set("Content-Range", contentRange)
req.Header.Set("Content-Type", rx.MediaType)
req.Header.Set("User-Agent", rx.UserAgent)
- return ctxhttp.Do(ctx, rx.Client, req)
+ return SendRequest(ctx, rx.Client, req)
}
@@ -135,6 +134,8 @@ func contextDone(ctx context.Context) bool {
// It retries using the provided back off strategy until cancelled or the
// strategy indicates to stop retrying.
// It is called from the auto-generated API code and is not visible to the user.
+// Before sending an HTTP request, Upload calls any registered hook functions,
+// and calls the returned functions after the request returns (see send.go).
// rx is private to the auto-generated API code.
// Exactly one of resp or err will be nil. If resp is non-nil, the caller must call resp.Body.Close.
func (rx *ResumableUpload) Upload(ctx context.Context) (resp *http.Response, err error) {
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..3d22f63
--- /dev/null
+++ b/vendor/google.golang.org/api/gensupport/send.go
@@ -0,0 +1,55 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package gensupport
+
+import (
+ "net/http"
+
+ "golang.org/x/net/context"
+ "golang.org/x/net/context/ctxhttp"
+)
+
+// Hook is the type of a function that is called once before each HTTP request
+// that is sent by a generated API. It returns a function that is called after
+// the request returns.
+// Hooks are not called if the context is nil.
+type Hook func(ctx context.Context, req *http.Request) func(resp *http.Response)
+
+var hooks []Hook
+
+// RegisterHook registers a Hook to be called before each HTTP request by a
+// generated API. Hooks are called in the order they are registered. Each
+// hook can return a function; if it is non-nil, it is called after the HTTP
+// request returns. These functions are called in the reverse order.
+// RegisterHook should not be called concurrently with itself or SendRequest.
+func RegisterHook(h Hook) {
+ hooks = append(hooks, h)
+}
+
+// SendRequest sends a single HTTP request using the given client.
+// If ctx is non-nil, it calls all hooks, then sends the request with
+// ctxhttp.Do, then calls any functions returned by the hooks in reverse order.
+func SendRequest(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
+ if ctx == nil {
+ return client.Do(req)
+ }
+ // Call hooks in order of registration, store returned funcs.
+ post := make([]func(resp *http.Response), len(hooks))
+ for i, h := range hooks {
+ fn := h(ctx, req)
+ post[i] = fn
+ }
+
+ // Send request.
+ resp, err := ctxhttp.Do(ctx, client, req)
+
+ // Call returned funcs in reverse order.
+ for i := len(post) - 1; i >= 0; i-- {
+ if fn := post[i]; fn != nil {
+ fn(resp)
+ }
+ }
+ return resp, err
+}