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
5package gensupport
6
7import (
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.
18type Hook func(ctx context.Context, req *http.Request) func(resp *http.Response)
19
20var 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.
27func 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.
35func 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
63func 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.
82func 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}
88