"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/vendor/google.golang.org/api/googleapi/googleapi.go" (28 May 2021, 13202 Bytes) of package /linux/misc/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2011 Google Inc. 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 
    5 // Package googleapi contains the common code shared by all Google API
    6 // libraries.
    7 package googleapi
    8 
    9 import (
   10     "bytes"
   11     "encoding/json"
   12     "fmt"
   13     "io"
   14     "io/ioutil"
   15     "net/http"
   16     "net/url"
   17     "strings"
   18 
   19     "google.golang.org/api/googleapi/internal/uritemplates"
   20 )
   21 
   22 // ContentTyper is an interface for Readers which know (or would like
   23 // to override) their Content-Type. If a media body doesn't implement
   24 // ContentTyper, the type is sniffed from the content using
   25 // http.DetectContentType.
   26 type ContentTyper interface {
   27     ContentType() string
   28 }
   29 
   30 // A SizeReaderAt is a ReaderAt with a Size method.
   31 // An io.SectionReader implements SizeReaderAt.
   32 type SizeReaderAt interface {
   33     io.ReaderAt
   34     Size() int64
   35 }
   36 
   37 // ServerResponse is embedded in each Do response and
   38 // provides the HTTP status code and header sent by the server.
   39 type ServerResponse struct {
   40     // HTTPStatusCode is the server's response status code.
   41     // When using a resource method's Do call, this will always be in the 2xx range.
   42     HTTPStatusCode int
   43     // Header contains the response header fields from the server.
   44     Header http.Header
   45 }
   46 
   47 const (
   48     Version = "0.5"
   49 
   50     // UserAgent is the header string used to identify this package.
   51     UserAgent = "google-api-go-client/" + Version
   52 
   53     // The default chunk size to use for resumable uplods if not specified by the user.
   54     DefaultUploadChunkSize = 8 * 1024 * 1024
   55 
   56     // The minimum chunk size that can be used for resumable uploads.  All
   57     // user-specified chunk sizes must be multiple of this value.
   58     MinUploadChunkSize = 256 * 1024
   59 )
   60 
   61 // Error contains an error response from the server.
   62 type Error struct {
   63     // Code is the HTTP response status code and will always be populated.
   64     Code int `json:"code"`
   65     // Message is the server response message and is only populated when
   66     // explicitly referenced by the JSON server response.
   67     Message string `json:"message"`
   68     // Body is the raw response returned by the server.
   69     // It is often but not always JSON, depending on how the request fails.
   70     Body string
   71     // Header contains the response header fields from the server.
   72     Header http.Header
   73 
   74     Errors []ErrorItem
   75 }
   76 
   77 // ErrorItem is a detailed error code & message from the Google API frontend.
   78 type ErrorItem struct {
   79     // Reason is the typed error code. For example: "some_example".
   80     Reason string `json:"reason"`
   81     // Message is the human-readable description of the error.
   82     Message string `json:"message"`
   83 }
   84 
   85 func (e *Error) Error() string {
   86     if len(e.Errors) == 0 && e.Message == "" {
   87         return fmt.Sprintf("googleapi: got HTTP response code %d with body: %v", e.Code, e.Body)
   88     }
   89     var buf bytes.Buffer
   90     fmt.Fprintf(&buf, "googleapi: Error %d: ", e.Code)
   91     if e.Message != "" {
   92         fmt.Fprintf(&buf, "%s", e.Message)
   93     }
   94     if len(e.Errors) == 0 {
   95         return strings.TrimSpace(buf.String())
   96     }
   97     if len(e.Errors) == 1 && e.Errors[0].Message == e.Message {
   98         fmt.Fprintf(&buf, ", %s", e.Errors[0].Reason)
   99         return buf.String()
  100     }
  101     fmt.Fprintln(&buf, "\nMore details:")
  102     for _, v := range e.Errors {
  103         fmt.Fprintf(&buf, "Reason: %s, Message: %s\n", v.Reason, v.Message)
  104     }
  105     return buf.String()
  106 }
  107 
  108 type errorReply struct {
  109     Error *Error `json:"error"`
  110 }
  111 
  112 // CheckResponse returns an error (of type *Error) if the response
  113 // status code is not 2xx.
  114 func CheckResponse(res *http.Response) error {
  115     if res.StatusCode >= 200 && res.StatusCode <= 299 {
  116         return nil
  117     }
  118     slurp, err := ioutil.ReadAll(res.Body)
  119     if err == nil {
  120         jerr := new(errorReply)
  121         err = json.Unmarshal(slurp, jerr)
  122         if err == nil && jerr.Error != nil {
  123             if jerr.Error.Code == 0 {
  124                 jerr.Error.Code = res.StatusCode
  125             }
  126             jerr.Error.Body = string(slurp)
  127             return jerr.Error
  128         }
  129     }
  130     return &Error{
  131         Code:   res.StatusCode,
  132         Body:   string(slurp),
  133         Header: res.Header,
  134     }
  135 }
  136 
  137 // IsNotModified reports whether err is the result of the
  138 // server replying with http.StatusNotModified.
  139 // Such error values are sometimes returned by "Do" methods
  140 // on calls when If-None-Match is used.
  141 func IsNotModified(err error) bool {
  142     if err == nil {
  143         return false
  144     }
  145     ae, ok := err.(*Error)
  146     return ok && ae.Code == http.StatusNotModified
  147 }
  148 
  149 // CheckMediaResponse returns an error (of type *Error) if the response
  150 // status code is not 2xx. Unlike CheckResponse it does not assume the
  151 // body is a JSON error document.
  152 func CheckMediaResponse(res *http.Response) error {
  153     if res.StatusCode >= 200 && res.StatusCode <= 299 {
  154         return nil
  155     }
  156     slurp, _ := ioutil.ReadAll(io.LimitReader(res.Body, 1<<20))
  157     res.Body.Close()
  158     return &Error{
  159         Code: res.StatusCode,
  160         Body: string(slurp),
  161     }
  162 }
  163 
  164 type MarshalStyle bool
  165 
  166 var WithDataWrapper = MarshalStyle(true)
  167 var WithoutDataWrapper = MarshalStyle(false)
  168 
  169 func (wrap MarshalStyle) JSONReader(v interface{}) (io.Reader, error) {
  170     buf := new(bytes.Buffer)
  171     if wrap {
  172         buf.Write([]byte(`{"data": `))
  173     }
  174     err := json.NewEncoder(buf).Encode(v)
  175     if err != nil {
  176         return nil, err
  177     }
  178     if wrap {
  179         buf.Write([]byte(`}`))
  180     }
  181     return buf, nil
  182 }
  183 
  184 // endingWithErrorReader from r until it returns an error.  If the
  185 // final error from r is io.EOF and e is non-nil, e is used instead.
  186 type endingWithErrorReader struct {
  187     r io.Reader
  188     e error
  189 }
  190 
  191 func (er endingWithErrorReader) Read(p []byte) (n int, err error) {
  192     n, err = er.r.Read(p)
  193     if err == io.EOF && er.e != nil {
  194         err = er.e
  195     }
  196     return
  197 }
  198 
  199 // countingWriter counts the number of bytes it receives to write, but
  200 // discards them.
  201 type countingWriter struct {
  202     n *int64
  203 }
  204 
  205 func (w countingWriter) Write(p []byte) (int, error) {
  206     *w.n += int64(len(p))
  207     return len(p), nil
  208 }
  209 
  210 // ProgressUpdater is a function that is called upon every progress update of a resumable upload.
  211 // This is the only part of a resumable upload (from googleapi) that is usable by the developer.
  212 // The remaining usable pieces of resumable uploads is exposed in each auto-generated API.
  213 type ProgressUpdater func(current, total int64)
  214 
  215 type MediaOption interface {
  216     setOptions(o *MediaOptions)
  217 }
  218 
  219 type contentTypeOption string
  220 
  221 func (ct contentTypeOption) setOptions(o *MediaOptions) {
  222     o.ContentType = string(ct)
  223     if o.ContentType == "" {
  224         o.ForceEmptyContentType = true
  225     }
  226 }
  227 
  228 // ContentType returns a MediaOption which sets the Content-Type header for media uploads.
  229 // If ctype is empty, the Content-Type header will be omitted.
  230 func ContentType(ctype string) MediaOption {
  231     return contentTypeOption(ctype)
  232 }
  233 
  234 type chunkSizeOption int
  235 
  236 func (cs chunkSizeOption) setOptions(o *MediaOptions) {
  237     size := int(cs)
  238     if size%MinUploadChunkSize != 0 {
  239         size += MinUploadChunkSize - (size % MinUploadChunkSize)
  240     }
  241     o.ChunkSize = size
  242 }
  243 
  244 // ChunkSize returns a MediaOption which sets the chunk size for media uploads.
  245 // size will be rounded up to the nearest multiple of 256K.
  246 // Media which contains fewer than size bytes will be uploaded in a single request.
  247 // Media which contains size bytes or more will be uploaded in separate chunks.
  248 // If size is zero, media will be uploaded in a single request.
  249 func ChunkSize(size int) MediaOption {
  250     return chunkSizeOption(size)
  251 }
  252 
  253 // MediaOptions stores options for customizing media upload.  It is not used by developers directly.
  254 type MediaOptions struct {
  255     ContentType           string
  256     ForceEmptyContentType bool
  257 
  258     ChunkSize int
  259 }
  260 
  261 // ProcessMediaOptions stores options from opts in a MediaOptions.
  262 // It is not used by developers directly.
  263 func ProcessMediaOptions(opts []MediaOption) *MediaOptions {
  264     mo := &MediaOptions{ChunkSize: DefaultUploadChunkSize}
  265     for _, o := range opts {
  266         o.setOptions(mo)
  267     }
  268     return mo
  269 }
  270 
  271 func ResolveRelative(basestr, relstr string) string {
  272     u, _ := url.Parse(basestr)
  273     rel, _ := url.Parse(relstr)
  274     u = u.ResolveReference(rel)
  275     us := u.String()
  276     us = strings.Replace(us, "%7B", "{", -1)
  277     us = strings.Replace(us, "%7D", "}", -1)
  278     return us
  279 }
  280 
  281 // has4860Fix is whether this Go environment contains the fix for
  282 // http://golang.org/issue/4860
  283 var has4860Fix bool
  284 
  285 // init initializes has4860Fix by checking the behavior of the net/http package.
  286 func init() {
  287     r := http.Request{
  288         URL: &url.URL{
  289             Scheme: "http",
  290             Opaque: "//opaque",
  291         },
  292     }
  293     b := &bytes.Buffer{}
  294     r.Write(b)
  295     has4860Fix = bytes.HasPrefix(b.Bytes(), []byte("GET http"))
  296 }
  297 
  298 // SetOpaque sets u.Opaque from u.Path such that HTTP requests to it
  299 // don't alter any hex-escaped characters in u.Path.
  300 func SetOpaque(u *url.URL) {
  301     u.Opaque = "//" + u.Host + u.Path
  302     if !has4860Fix {
  303         u.Opaque = u.Scheme + ":" + u.Opaque
  304     }
  305 }
  306 
  307 // Expand subsitutes any {encoded} strings in the URL passed in using
  308 // the map supplied.
  309 //
  310 // This calls SetOpaque to avoid encoding of the parameters in the URL path.
  311 func Expand(u *url.URL, expansions map[string]string) {
  312     expanded, err := uritemplates.Expand(u.Path, expansions)
  313     if err == nil {
  314         u.Path = expanded
  315         SetOpaque(u)
  316     }
  317 }
  318 
  319 // CloseBody is used to close res.Body.
  320 // Prior to calling Close, it also tries to Read a small amount to see an EOF.
  321 // Not seeing an EOF can prevent HTTP Transports from reusing connections.
  322 func CloseBody(res *http.Response) {
  323     if res == nil || res.Body == nil {
  324         return
  325     }
  326     // Justification for 3 byte reads: two for up to "\r\n" after
  327     // a JSON/XML document, and then 1 to see EOF if we haven't yet.
  328     // TODO(bradfitz): detect Go 1.3+ and skip these reads.
  329     // See https://codereview.appspot.com/58240043
  330     // and https://codereview.appspot.com/49570044
  331     buf := make([]byte, 1)
  332     for i := 0; i < 3; i++ {
  333         _, err := res.Body.Read(buf)
  334         if err != nil {
  335             break
  336         }
  337     }
  338     res.Body.Close()
  339 
  340 }
  341 
  342 // VariantType returns the type name of the given variant.
  343 // If the map doesn't contain the named key or the value is not a []interface{}, "" is returned.
  344 // This is used to support "variant" APIs that can return one of a number of different types.
  345 func VariantType(t map[string]interface{}) string {
  346     s, _ := t["type"].(string)
  347     return s
  348 }
  349 
  350 // ConvertVariant uses the JSON encoder/decoder to fill in the struct 'dst' with the fields found in variant 'v'.
  351 // This is used to support "variant" APIs that can return one of a number of different types.
  352 // It reports whether the conversion was successful.
  353 func ConvertVariant(v map[string]interface{}, dst interface{}) bool {
  354     var buf bytes.Buffer
  355     err := json.NewEncoder(&buf).Encode(v)
  356     if err != nil {
  357         return false
  358     }
  359     return json.Unmarshal(buf.Bytes(), dst) == nil
  360 }
  361 
  362 // A Field names a field to be retrieved with a partial response.
  363 // See https://developers.google.com/gdata/docs/2.0/basics#PartialResponse
  364 //
  365 // Partial responses can dramatically reduce the amount of data that must be sent to your application.
  366 // In order to request partial responses, you can specify the full list of fields
  367 // that your application needs by adding the Fields option to your request.
  368 //
  369 // Field strings use camelCase with leading lower-case characters to identify fields within the response.
  370 //
  371 // For example, if your response has a "NextPageToken" and a slice of "Items" with "Id" fields,
  372 // you could request just those fields like this:
  373 //
  374 //     svc.Events.List().Fields("nextPageToken", "items/id").Do()
  375 //
  376 // or if you were also interested in each Item's "Updated" field, you can combine them like this:
  377 //
  378 //     svc.Events.List().Fields("nextPageToken", "items(id,updated)").Do()
  379 //
  380 // More information about field formatting can be found here:
  381 // https://developers.google.com/+/api/#fields-syntax
  382 //
  383 // Another way to find field names is through the Google API explorer:
  384 // https://developers.google.com/apis-explorer/#p/
  385 type Field string
  386 
  387 // CombineFields combines fields into a single string.
  388 func CombineFields(s []Field) string {
  389     r := make([]string, len(s))
  390     for i, v := range s {
  391         r[i] = string(v)
  392     }
  393     return strings.Join(r, ",")
  394 }
  395 
  396 // A CallOption is an optional argument to an API call.
  397 // It should be treated as an opaque value by users of Google APIs.
  398 //
  399 // A CallOption is something that configures an API call in a way that is
  400 // not specific to that API; for instance, controlling the quota user for
  401 // an API call is common across many APIs, and is thus a CallOption.
  402 type CallOption interface {
  403     Get() (key, value string)
  404 }
  405 
  406 // QuotaUser returns a CallOption that will set the quota user for a call.
  407 // The quota user can be used by server-side applications to control accounting.
  408 // It can be an arbitrary string up to 40 characters, and will override UserIP
  409 // if both are provided.
  410 func QuotaUser(u string) CallOption { return quotaUser(u) }
  411 
  412 type quotaUser string
  413 
  414 func (q quotaUser) Get() (string, string) { return "quotaUser", string(q) }
  415 
  416 // UserIP returns a CallOption that will set the "userIp" parameter of a call.
  417 // This should be the IP address of the originating request.
  418 func UserIP(ip string) CallOption { return userIP(ip) }
  419 
  420 type userIP string
  421 
  422 func (i userIP) Get() (string, string) { return "userIp", string(i) }
  423 
  424 // Trace returns a CallOption that enables diagnostic tracing for a call.
  425 // traceToken is an ID supplied by Google support.
  426 func Trace(traceToken string) CallOption { return traceTok(traceToken) }
  427 
  428 type traceTok string
  429 
  430 func (t traceTok) Get() (string, string) { return "trace", "token:" + string(t) }
  431 
  432 // TODO: Fields too