googleapi.go 15 KB

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