gensupport

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2020 License: BSD-3-Clause Imports: 21 Imported by: 0

Documentation

Overview

Package gensupport is an internal implementation detail used by code generated by the google-api-go-generator tool.

This package may be modified at any time without regard for backwards compatibility. It should not be used directly by API users.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombineBodyMedia

func CombineBodyMedia(body io.Reader, bodyContentType string, media io.Reader, mediaContentType string) (io.ReadCloser, string)

CombineBodyMedia combines a json body with media content to create a multipart/related HTTP body. It returns a ReadCloser containing the combined body, and the overall "multipart/related" content type, with random boundary.

The caller must call Close on the returned ReadCloser if reads are abandoned before reaching EOF.

func DecodeResponse

func DecodeResponse(target interface{}, res *http.Response) error

DecodeResponse decodes the body of res into target. If there is no body, target is unchanged.

func DetermineContentType

func DetermineContentType(media io.Reader, ctype string) (io.Reader, string)

DetermineContentType determines the content type of the supplied reader. If the content type is already known, it can be specified via ctype. Otherwise, the content of media will be sniffed to determine the content type. If media implements googleapi.ContentTyper (deprecated), this will be used instead of sniffing the content. After calling DetectContentType the caller must not perform further reads on media, but rather read from the Reader that is returned.

func GoogleClientHeader

func GoogleClientHeader(generatorVersion, clientElement string) string

GoogleClientHeader returns the value to use for the x-goog-api-client header, which is used internally by Google.

func MarshalJSON

func MarshalJSON(schema interface{}, forceSendFields, nullFields []string) ([]byte, error)

MarshalJSON returns a JSON encoding of schema containing only selected fields. A field is selected if any of the following is true:

  • it has a non-empty value
  • its field name is present in forceSendFields and it is not a nil pointer or nil interface
  • its field name is present in nullFields.

The JSON key for each selected field is taken from the field's json: struct tag.

func ReaderAtToReader

func ReaderAtToReader(ra io.ReaderAt, size int64) io.Reader

ReaderAtToReader adapts a ReaderAt to be used as a Reader. If ra implements googleapi.ContentTyper, then the returned reader will also implement googleapi.ContentTyper, delegating to ra.

func RegisterHook

func RegisterHook(h 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 Retry

func Retry(ctx context.Context, f func() (*http.Response, error), backoff BackoffStrategy) (*http.Response, error)

Retry invokes the given function, retrying it multiple times if the connection failed or the HTTP status response indicates the request should be attempted again. ctx may be nil.

func SendRequest

func SendRequest(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error)

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.

func SetGetBody deprecated

func SetGetBody(req *http.Request, f func() (io.ReadCloser, error))

SetGetBody sets the GetBody field of req to f. This was once needed to gracefully support Go 1.7 and earlier which didn't have that field.

Deprecated: the code generator no longer uses this as of 2019-02-19. Nothing else should be calling this anyway, but we won't delete this immediately; it will be deleted in as early as 6 months.

func SetOptions

func SetOptions(u URLParams, opts ...googleapi.CallOption)

SetOptions sets the URL params and any additional call options.

Types

type BackoffStrategy

type BackoffStrategy interface {
	// Pause returns the duration of the next pause and true if the operation should be
	// retried, or false if no further retries should be attempted.
	Pause() (time.Duration, bool)

	// Reset restores the strategy to its initial state.
	Reset()
}

BackoffStrategy defines the set of functions that a backoff-er must implement.

func DefaultBackoffStrategy

func DefaultBackoffStrategy() BackoffStrategy

DefaultBackoffStrategy returns a default strategy to use for retrying failed upload requests.

type ExponentialBackoff

type ExponentialBackoff struct {
	Base time.Duration
	Max  time.Duration
	// contains filtered or unexported fields
}

ExponentialBackoff performs exponential backoff as per https://en.wikipedia.org/wiki/Exponential_backoff. The initial pause time is given by Base. Once the total pause time exceeds Max, Pause will indicate no further retries.

func (*ExponentialBackoff) Pause

func (eb *ExponentialBackoff) Pause() (time.Duration, bool)

Pause returns the amount of time the caller should wait.

func (*ExponentialBackoff) Reset

func (eb *ExponentialBackoff) Reset()

Reset resets the backoff strategy such that the next Pause call will begin counting from the start. It is not safe to call concurrently with Pause.

type Hook

type Hook func(ctx context.Context, req *http.Request) func(resp *http.Response)

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 JSONFloat64

type JSONFloat64 float64

JSONFloat64 is a float64 that supports proper unmarshaling of special float values in JSON, according to https://developers.google.com/protocol-buffers/docs/proto3#json. Although that is a proto-to-JSON spec, it applies to all Google APIs.

The jsonpb package (https://github.com/golang/protobuf/blob/master/jsonpb/jsonpb.go) has similar functionality, but only for direct translation from proto messages to JSON.

func (*JSONFloat64) UnmarshalJSON

func (f *JSONFloat64) UnmarshalJSON(data []byte) error

type MediaBuffer

type MediaBuffer struct {
	// contains filtered or unexported fields
}

MediaBuffer buffers data from an io.Reader to support uploading media in retryable chunks. It should be created with NewMediaBuffer.

func NewMediaBuffer

func NewMediaBuffer(media io.Reader, chunkSize int) *MediaBuffer

NewMediaBuffer initializes a MediaBuffer.

func PrepareUpload

func PrepareUpload(media io.Reader, chunkSize int) (r io.Reader, mb *MediaBuffer, singleChunk bool)

PrepareUpload determines whether the data in the supplied reader should be uploaded in a single request, or in sequential chunks. chunkSize is the size of the chunk that media should be split into.

If chunkSize is zero, media is returned as the first value, and the other two return values are nil, true.

Otherwise, a MediaBuffer is returned, along with a bool indicating whether the contents of media fit in a single chunk.

After PrepareUpload has been called, media should no longer be used: the media content should be accessed via one of the return values.

func (*MediaBuffer) Chunk

func (mb *MediaBuffer) Chunk() (chunk io.Reader, off int64, size int, err error)

Chunk returns the current buffered chunk, the offset in the underlying media from which the chunk is drawn, and the size of the chunk. Successive calls to Chunk return the same chunk between calls to Next.

func (*MediaBuffer) Next

func (mb *MediaBuffer) Next()

Next advances to the next chunk, which will be returned by the next call to Chunk. Calls to Next without a corresponding prior call to Chunk will have no effect.

type MediaInfo

type MediaInfo struct {
	// contains filtered or unexported fields
}

MediaInfo holds information for media uploads. It is intended for use by generated code only.

func NewInfoFromMedia

func NewInfoFromMedia(r io.Reader, options []googleapi.MediaOption) *MediaInfo

NewInfoFromMedia should be invoked from the Media method of a call. It returns a MediaInfo populated with chunk size and content type, and a reader or MediaBuffer if needed.

func NewInfoFromResumableMedia

func NewInfoFromResumableMedia(r io.ReaderAt, size int64, mediaType string) *MediaInfo

NewInfoFromResumableMedia should be invoked from the ResumableMedia method of a call. It returns a MediaInfo using the given reader, size and media type.

func (*MediaInfo) ResumableUpload

func (mi *MediaInfo) ResumableUpload(locURI string) *ResumableUpload

ResumableUpload returns an appropriately configured ResumableUpload value if the upload is resumable, or nil otherwise.

func (*MediaInfo) SetProgressUpdater

func (mi *MediaInfo) SetProgressUpdater(pu googleapi.ProgressUpdater)

SetProgressUpdater sets the progress updater for the media info.

func (*MediaInfo) UploadRequest

func (mi *MediaInfo) UploadRequest(reqHeaders http.Header, body io.Reader) (newBody io.Reader, getBody func() (io.ReadCloser, error), cleanup func())

UploadRequest sets up an HTTP request for media upload. It adds headers as necessary, and returns a replacement for the body and a function for http.Request.GetBody.

func (*MediaInfo) UploadType

func (mi *MediaInfo) UploadType() string

UploadType determines the type of upload: a single request, or a resumable series of requests.

type ResumableUpload

type ResumableUpload struct {
	Client *http.Client
	// URI is the resumable resource destination provided by the server after specifying "&uploadType=resumable".
	URI       string
	UserAgent string // User-Agent for header of the request
	// Media is the object being uploaded.
	Media *MediaBuffer
	// MediaType defines the media type, e.g. "image/jpeg".
	MediaType string

	// Callback is an optional function that will be periodically called with the cumulative number of bytes uploaded.
	Callback func(int64)

	// If not specified, a default exponential backoff strategy will be used.
	Backoff BackoffStrategy
	// contains filtered or unexported fields
}

ResumableUpload is used by the generated APIs to provide resumable uploads. It is not used by developers directly.

func (*ResumableUpload) Progress

func (rx *ResumableUpload) Progress() int64

Progress returns the number of bytes uploaded at this point.

func (*ResumableUpload) Upload

func (rx *ResumableUpload) Upload(ctx context.Context) (resp *http.Response, err error)

Upload starts the process of a resumable upload with a cancellable context. 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.

type URLParams

type URLParams map[string][]string

URLParams is a simplified replacement for url.Values that safely builds up URL parameters for encoding.

func (URLParams) Encode

func (u URLParams) Encode() string

Encode encodes the values into “URL encoded” form ("bar=baz&foo=quux") sorted by key.

func (URLParams) Get

func (u URLParams) Get(key string) string

Get returns the first value for the given key, or "".

func (URLParams) Set

func (u URLParams) Set(key, value string)

Set sets the key to value. It replaces any existing values.

func (URLParams) SetMulti

func (u URLParams) SetMulti(key string, values []string)

SetMulti sets the key to an array of values. It replaces any existing values. Note that values must not be modified after calling SetMulti so the caller is responsible for making a copy if necessary.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL