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 (
	
	
	
	
	
)
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 ( Hook) {
	hooks = append(hooks, )
}
SendRequest sends a single HTTP request using the given client. If ctx is non-nil, it calls all hooks, then sends the request with req.WithContext, then calls any functions returned by the hooks in reverse order.
Disallow Accept-Encoding because it interferes with the automatic gzip handling done by the default http.Transport. See https://github.com/google/google-api-go-client/issues/219.
	if ,  := .Header["Accept-Encoding"];  {
		return nil, errors.New("google api: custom Accept-Encoding headers not allowed")
	}
	if  == nil {
		return .Do()
Call hooks in order of registration, store returned funcs.
	 := make([]func( *http.Response), len(hooks))
	for ,  := range hooks {
		 := (, )
		[] = 
	}
Send request.
	,  := send(, , )
Call returned funcs in reverse order.
	for  := len() - 1;  >= 0; -- {
		if  := [];  != nil {
			()
		}
	}
	return , 
}

func ( context.Context,  *http.Client,  *http.Request) (*http.Response, error) {
	if  == nil {
		 = http.DefaultClient
	}
If we got an error, and the context has been canceled, the context's error is probably more useful.
	if  != nil {
		select {
		case <-.Done():
			 = .Err()
		default:
		}
	}
	return , 
}
SendRequestWithRetry sends a single HTTP request using the given client, with retries if a retryable error is returned. If ctx is non-nil, it calls all hooks, then sends the request with req.WithContext, then calls any functions returned by the hooks in reverse order.
Disallow Accept-Encoding because it interferes with the automatic gzip handling done by the default http.Transport. See https://github.com/google/google-api-go-client/issues/219.
	if ,  := .Header["Accept-Encoding"];  {
		return nil, errors.New("google api: custom Accept-Encoding headers not allowed")
	}
	if  == nil {
		return .Do()
Call hooks in order of registration, store returned funcs.
	 := make([]func( *http.Response), len(hooks))
	for ,  := range hooks {
		 := (, )
		[] = 
	}
Send request with retry.
	,  := sendAndRetry(, , )
Call returned funcs in reverse order.
	for  := len() - 1;  >= 0; -- {
		if  := [];  != nil {
			()
		}
	}
	return , 
}

func ( context.Context,  *http.Client,  *http.Request) (*http.Response, error) {
	if  == nil {
		 = http.DefaultClient
	}

	var  *http.Response
	var  error
Loop to retry the request, up to the context deadline.
	var  time.Duration
	 := backoff()

	for {
		select {
If we got an error, and the context has been canceled, the context's error is probably more useful.
			if  == nil {
				 = .Err()
			}
			return , 
		case <-time.After():
		}

		,  = .Do(.WithContext())

		var  int
		if  != nil {
			 = .StatusCode
		}
Check if we can retry the request. A retry can only be done if the error is retryable and the request body can be re-created using GetBody (this will not be possible if the body was unbuffered).
		if .GetBody == nil || !shouldRetry(, ) {
			break
		}
		var  error
		.Body,  = .GetBody()
		if  != nil {
			break
		}

		 = .Pause()
		if  != nil && .Body != nil {
			.Body.Close()
		}
	}
	return , 
}
DecodeResponse decodes the body of res into target. If there is no body, target is unchanged.
func ( interface{},  *http.Response) error {
	if .StatusCode == http.StatusNoContent {
		return nil
	}
	return json.NewDecoder(.Body).Decode()