googleapi

package
v0.0.0-...-bb6c6ed Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2015 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package googleapi contains the common code shared by all Google API libraries.

Index

Constants

View Source
const (
	Version = "0.5"
)

Variables

View Source
var (
	// NoContext provides a default context for use by the apis
	NoContext = context.Background()
)

Functions

func BatchClient

func BatchClient() *http.Client

BatchClient returns an *http.Client for use by an api Serivce. Any Do() calls using a Service containing the Bactch client will return a Call struct that may be used by a Batch service.

func Callback

func Callback(cb ProgressUpdater) option

Callback sets the ProgressUpdater.

func CancelRequest

func CancelRequest(client *http.Client, req *http.Request)

CancelRequest attempts to checks whether the client's Transport has a CancelRequest method and attempts to cancel the request.

func CheckResponse

func CheckResponse(res *http.Response) error

CheckResponse returns an error (of type *Error) if the response status code is not 2xx.

func ChunkSize

func ChunkSize(n int64) option

ChunkSize sets the chunk size of the upload. Must be divisible by 256KB.

func CloseBody

func CloseBody(res *http.Response)

CloseBody is used to close res.Body. Prior to calling Close, it also tries to Read a small amount to see an EOF. Not seeing an EOF can prevent HTTP Transports from reusing connections.

func CombineFields

func CombineFields(s []Field) string

CombineFields combines fields into a single string.

func ConvertVariant

func ConvertVariant(v map[string]interface{}, dst interface{}) bool

ConvertVariant uses the JSON encoder/decoder to fill in the struct 'dst' with the fields found in variant 'v'. This is used to support "variant" APIs that can return one of a number of different types. It reports whether the conversion was successful.

func Expand

func Expand(baseURL *url.URL, path string, expansions map[string]string) (u *url.URL)

Expand subsitutes any {encoded} strings in the URL passed in using the map supplied.

This calls SetOpaque to avoid encoding of the parameters in the URL path.

func MaxRetries

func MaxRetries(n int) option

Retries sets the number of resumable max.

func MediaSize

func MediaSize(n int64) option

MediaSize set the content length.

func ResumableUrl

func ResumableUrl(url string) option

ResumableUrl sets the url.

func SetOpaque

func SetOpaque(u *url.URL)

SetOpaque sets u.Opaque from u.Path such that HTTP requests to it don't alter any hex-escaped characters in u.Path.

func VariantType

func VariantType(t map[string]interface{}) string

VariantType returns the type name of the given variant. If the map doesn't contain the named key or the value is not a []interface{}, "" is returned. This is used to support "variant" APIs that can return one of a number of different types.

func Wrap

func Wrap(d interface{}) interface{}

Wrap returns a struct with passed interface as a field named Data.

Types

type Call

type Call struct {
	// Payload is the value to be marshalled into
	// the request body.
	Payload interface{}

	// http method for request.
	Method string

	// URL for request, not including querystring.
	URL *url.URL

	// Params contain the values for the call's querystring.
	Params url.Values

	// Result is used to return data back to the calling function.
	// It may contain a pointer to a struct for unmarshalling json
	// in the response, or an *http.Response for a MediaDownload call.
	Result interface{}
}

Call contains data needed by Caller. The client api calls create the struct in their Do() commands. The developer would not use this directly.

func (Call) Error

func (c Call) Error() string

Error so that Call fulfills the error interface.

func (Call) GetParams

func (c Call) GetParams() url.Values

GetParams checks for nil value in params field. Returns empty map if nil.

func (Call) PayloadReader

func (c Call) PayloadReader() (io.Reader, error)

PayloadReader returns an io reader containing the results of a marshaled payload. If payload is nil then a nil reader is returned.

type Caller

type Caller interface {
	Do(context.Context, *http.Client, *Call) error
}

Caller is the interface used to execute api calls

*http.Client parameter should perform oauth2.0 authorization *Call contains url and payload data inteface{} parameter should be a ptr to a struct that the function will unmarshal the http.Response into. If the value is an *http.Response, the function will set the value to the response.

type ContentTyper

type ContentTyper interface {
	ContentType() string
}

ContentTyper is an interface for Readers which know (or would like to override) their Content-Type. If a media body doesn't implement ContentTyper, the type is sniffed from the content using http.DetectContentType.

type Error

type Error struct {
	// Code is the HTTP response status code and will always be populated.
	Code int `json:"code"`
	// Message is the server response message and is only populated when
	// explicitly referenced by the JSON server response.
	Message string `json:"message"`
	// Body is the raw response returned by the server.
	// It is often but not always JSON, depending on how the request fails.
	Body string

	Errors []ErrorItem
}

Error contains an error response from the server.

func (*Error) Error

func (e *Error) Error() string

type ErrorItem

type ErrorItem struct {
	// Reason is the typed error code. For example: "some_example".
	Reason string `json:"reason"`
	// Message is the human-readable description of the error.
	Message string `json:"message"`
}

ErrorItem is a detailed error code & message from the Google API frontend.

type Field

type Field string

A Field names a field to be retrieved with a partial response. See https://developers.google.com/gdata/docs/2.0/basics#PartialResponse

Partial responses can dramatically reduce the amount of data that must be sent to your application. In order to request partial responses, you can specify the full list of fields that your application needs by adding the Fields option to your request.

Field strings use camelCase with leading lower-case characters to identify fields within the response.

For example, if your response has a "NextPageToken" and a slice of "Items" with "Id" fields, you could request just those fields like this:

svc.Events.List().Fields("nextPageToken", "items/id").Do()

or if you were also interested in each Item's "Updated" field, you can combine them like this:

svc.Events.List().Fields("nextPageToken", "items(id,updated)").Do()

More information about field formatting can be found here: https://developers.google.com/+/api/#fields-syntax

Another way to find field names is through the Google API explorer: https://developers.google.com/apis-explorer/#p/

type Float64s

type Float64s []float64

Float64s is a slice of float64s that marshal as quoted strings in JSON.

func (Float64s) MarshalJSON

func (s Float64s) MarshalJSON() ([]byte, error)

func (*Float64s) UnmarshalJSON

func (q *Float64s) UnmarshalJSON(raw []byte) error

type Int32s

type Int32s []int32

Int32s is a slice of int32s that marshal as quoted strings in JSON.

func (Int32s) MarshalJSON

func (s Int32s) MarshalJSON() ([]byte, error)

func (*Int32s) UnmarshalJSON

func (q *Int32s) UnmarshalJSON(raw []byte) error

type Int64s

type Int64s []int64

Int64s is a slice of int64s that marshal as quoted strings in JSON.

func (Int64s) MarshalJSON

func (s Int64s) MarshalJSON() ([]byte, error)

func (*Int64s) UnmarshalJSON

func (q *Int64s) UnmarshalJSON(raw []byte) error

type JSONCall

type JSONCall struct{}

JSONCall is the default caller used by apis. The developer does not use it directly.

func (JSONCall) Do

func (p JSONCall) Do(ctx context.Context, cl *http.Client, c *Call) error

Do executes a call with an optional json body and process the optional json response.

type MediaUpload

type MediaUpload struct {
	Media     io.Reader
	MediaType string
}

MediaUpload is a caller that executes simple and multipart uploads

func (*MediaUpload) Do

func (m *MediaUpload) Do(ctx context.Context, cl *http.Client, c *Call) error

type ProgressUpdater

type ProgressUpdater func(current, total int64, err error)

ProgressUpdater is a function that is called upon every progress update of a resumable upload. This is the only part of a resumable upload (from googleapi) that is usable by the developer.

type ResumableUpload

type ResumableUpload struct {
	// URL is the resumable resource destination provided by the server after specifying "&uploadType=resumable".
	URL string
	// Media is the object being uploaded.
	Media io.ReaderAt
	// MediaType defines the media type, e.g. "image/jpeg".
	MediaType string
	// ContentLength is the full size of the object being uploaded.
	ContentLength int64
	// ChunkSize is the size of the chunks created during a resumable upload and should be a power of two.
	// 1<<18 is the minimum size supported by the Google uploader, and there is no maximum.
	// If ChunkSize is zero, chunking is ignored.
	ChunkSize int64
	// MaxRetries is the number of times the upload will retry after 5XX errors.
	MaxRetries int

	// Callback is an optional function that will be called upon every progress update.
	Callback ProgressUpdater
	// Error causing the upload to stop.  If set to
	Err error
	// contains filtered or unexported fields
}

ResumableUpload is used by the generated APIs to provide resumable uploads.

func NewResumableUpload

func NewResumableUpload(r io.ReaderAt, mediaType string, size int64, opts ...option) (*ResumableUpload, error)

NewResumableUpload initializes a ResumableUplod struct.

func (*ResumableUpload) Do

func (rx *ResumableUpload) Do(ctx context.Context, cl *http.Client, c *Call) error

func (*ResumableUpload) Error

func (rx *ResumableUpload) Error() string

Error implements the error interface. This allows the ResumableUpload to be passed via an error and then restarted.

func (*ResumableUpload) Resume

func (rx *ResumableUpload) Resume(ctx context.Context, client *http.Client, v interface{}) error

Resume restarts an existing upload.

func (*ResumableUpload) Upload

func (rx *ResumableUpload) Upload(ctx context.Context, client *http.Client, v interface{}) error

Upload starts the process of a resumable upload with a cancellable context. If succesful it returns nil, otherwise it returns itself so the upload resume.

type SizeReaderAt

type SizeReaderAt interface {
	io.Reader
	io.ReaderAt
	Size() int64
}

A SizeReaderAt is a ReaderAt with a Size method. An io.SectionReader implements SizeReaderAt.

type Uint32s

type Uint32s []uint32

Uint32s is a slice of uint32s that marshal as quoted strings in JSON.

func (Uint32s) MarshalJSON

func (s Uint32s) MarshalJSON() ([]byte, error)

func (*Uint32s) UnmarshalJSON

func (q *Uint32s) UnmarshalJSON(raw []byte) error

type Uint64s

type Uint64s []uint64

Uint64s is a slice of uint64s that marshal as quoted strings in JSON.

func (Uint64s) MarshalJSON

func (s Uint64s) MarshalJSON() ([]byte, error)

func (*Uint64s) UnmarshalJSON

func (q *Uint64s) UnmarshalJSON(raw []byte) error

type UploadCaller

type UploadCaller interface {
	Caller
	// contains filtered or unexported methods
}

UploadCaller is a Caller that can also upload media.

Directories

Path Synopsis
internal
uritemplates
Package uritemplates is a level 4 implementation of RFC 6570 (URI Template, http://tools.ietf.org/html/rfc6570).
Package uritemplates is a level 4 implementation of RFC 6570 (URI Template, http://tools.ietf.org/html/rfc6570).
Package transport contains HTTP transports used to make authenticated API requests.
Package transport contains HTTP transports used to make authenticated API requests.

Jump to

Keyboard shortcuts

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