You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

404 lines
13 KiB

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