http.py 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. # Copyright 2014 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Classes to encapsulate a single HTTP request.
  15. The classes implement a command pattern, with every
  16. object supporting an execute() method that does the
  17. actual HTTP request.
  18. """
  19. from __future__ import absolute_import
  20. __author__ = "jcgregorio@google.com (Joe Gregorio)"
  21. import copy
  22. import http.client as http_client
  23. import io
  24. import json
  25. import logging
  26. import mimetypes
  27. import os
  28. import random
  29. import socket
  30. import time
  31. import urllib
  32. import uuid
  33. import httplib2
  34. # TODO(issue 221): Remove this conditional import jibbajabba.
  35. try:
  36. import ssl
  37. except ImportError:
  38. _ssl_SSLError = object()
  39. else:
  40. _ssl_SSLError = ssl.SSLError
  41. from email.generator import Generator
  42. from email.mime.multipart import MIMEMultipart
  43. from email.mime.nonmultipart import MIMENonMultipart
  44. from email.parser import FeedParser
  45. from googleapiclient import _auth
  46. from googleapiclient import _helpers as util
  47. from googleapiclient.errors import (
  48. BatchError,
  49. HttpError,
  50. InvalidChunkSizeError,
  51. ResumableUploadError,
  52. UnexpectedBodyError,
  53. UnexpectedMethodError,
  54. )
  55. from googleapiclient.model import JsonModel
  56. LOGGER = logging.getLogger(__name__)
  57. DEFAULT_CHUNK_SIZE = 100 * 1024 * 1024
  58. MAX_URI_LENGTH = 2048
  59. MAX_BATCH_LIMIT = 1000
  60. _TOO_MANY_REQUESTS = 429
  61. DEFAULT_HTTP_TIMEOUT_SEC = 60
  62. _LEGACY_BATCH_URI = "https://www.googleapis.com/batch"
  63. def _should_retry_response(resp_status, content):
  64. """Determines whether a response should be retried.
  65. Args:
  66. resp_status: The response status received.
  67. content: The response content body.
  68. Returns:
  69. True if the response should be retried, otherwise False.
  70. """
  71. reason = None
  72. # Retry on 5xx errors.
  73. if resp_status >= 500:
  74. return True
  75. # Retry on 429 errors.
  76. if resp_status == _TOO_MANY_REQUESTS:
  77. return True
  78. # For 403 errors, we have to check for the `reason` in the response to
  79. # determine if we should retry.
  80. if resp_status == http_client.FORBIDDEN:
  81. # If there's no details about the 403 type, don't retry.
  82. if not content:
  83. return False
  84. # Content is in JSON format.
  85. try:
  86. data = json.loads(content.decode("utf-8"))
  87. if isinstance(data, dict):
  88. # There are many variations of the error json so we need
  89. # to determine the keyword which has the error detail. Make sure
  90. # that the order of the keywords below isn't changed as it can
  91. # break user code. If the "errors" key exists, we must use that
  92. # first.
  93. # See Issue #1243
  94. # https://github.com/googleapis/google-api-python-client/issues/1243
  95. error_detail_keyword = next(
  96. (
  97. kw
  98. for kw in ["errors", "status", "message"]
  99. if kw in data["error"]
  100. ),
  101. "",
  102. )
  103. if error_detail_keyword:
  104. reason = data["error"][error_detail_keyword]
  105. if isinstance(reason, list) and len(reason) > 0:
  106. reason = reason[0]
  107. if "reason" in reason:
  108. reason = reason["reason"]
  109. else:
  110. reason = data[0]["error"]["errors"]["reason"]
  111. except (UnicodeDecodeError, ValueError, KeyError):
  112. LOGGER.warning("Invalid JSON content from response: %s", content)
  113. return False
  114. LOGGER.warning('Encountered 403 Forbidden with reason "%s"', reason)
  115. # Only retry on rate limit related failures.
  116. if reason in ("userRateLimitExceeded", "rateLimitExceeded"):
  117. return True
  118. # Everything else is a success or non-retriable so break.
  119. return False
  120. def _retry_request(
  121. http, num_retries, req_type, sleep, rand, uri, method, *args, **kwargs
  122. ):
  123. """Retries an HTTP request multiple times while handling errors.
  124. If after all retries the request still fails, last error is either returned as
  125. return value (for HTTP 5xx errors) or thrown (for ssl.SSLError).
  126. Args:
  127. http: Http object to be used to execute request.
  128. num_retries: Maximum number of retries.
  129. req_type: Type of the request (used for logging retries).
  130. sleep, rand: Functions to sleep for random time between retries.
  131. uri: URI to be requested.
  132. method: HTTP method to be used.
  133. args, kwargs: Additional arguments passed to http.request.
  134. Returns:
  135. resp, content - Response from the http request (may be HTTP 5xx).
  136. """
  137. resp = None
  138. content = None
  139. exception = None
  140. for retry_num in range(num_retries + 1):
  141. if retry_num > 0:
  142. # Sleep before retrying.
  143. sleep_time = rand() * 2**retry_num
  144. LOGGER.warning(
  145. "Sleeping %.2f seconds before retry %d of %d for %s: %s %s, after %s",
  146. sleep_time,
  147. retry_num,
  148. num_retries,
  149. req_type,
  150. method,
  151. uri,
  152. resp.status if resp else exception,
  153. )
  154. sleep(sleep_time)
  155. try:
  156. exception = None
  157. resp, content = http.request(uri, method, *args, **kwargs)
  158. # Retry on SSL errors and socket timeout errors.
  159. except _ssl_SSLError as ssl_error:
  160. exception = ssl_error
  161. except socket.timeout as socket_timeout:
  162. # Needs to be before socket.error as it's a subclass of OSError
  163. # socket.timeout has no errorcode
  164. exception = socket_timeout
  165. except ConnectionError as connection_error:
  166. # Needs to be before socket.error as it's a subclass of OSError
  167. exception = connection_error
  168. except OSError as socket_error:
  169. # errno's contents differ by platform, so we have to match by name.
  170. # Some of these same errors may have been caught above, e.g. ECONNRESET *should* be
  171. # raised as a ConnectionError, but some libraries will raise it as a socket.error
  172. # with an errno corresponding to ECONNRESET
  173. if socket.errno.errorcode.get(socket_error.errno) not in {
  174. "WSAETIMEDOUT",
  175. "ETIMEDOUT",
  176. "EPIPE",
  177. "ECONNABORTED",
  178. "ECONNREFUSED",
  179. "ECONNRESET",
  180. }:
  181. raise
  182. exception = socket_error
  183. except httplib2.ServerNotFoundError as server_not_found_error:
  184. exception = server_not_found_error
  185. if exception:
  186. if retry_num == num_retries:
  187. raise exception
  188. else:
  189. continue
  190. if not _should_retry_response(resp.status, content):
  191. break
  192. return resp, content
  193. class MediaUploadProgress(object):
  194. """Status of a resumable upload."""
  195. def __init__(self, resumable_progress, total_size):
  196. """Constructor.
  197. Args:
  198. resumable_progress: int, bytes sent so far.
  199. total_size: int, total bytes in complete upload, or None if the total
  200. upload size isn't known ahead of time.
  201. """
  202. self.resumable_progress = resumable_progress
  203. self.total_size = total_size
  204. def progress(self):
  205. """Percent of upload completed, as a float.
  206. Returns:
  207. the percentage complete as a float, returning 0.0 if the total size of
  208. the upload is unknown.
  209. """
  210. if self.total_size is not None and self.total_size != 0:
  211. return float(self.resumable_progress) / float(self.total_size)
  212. else:
  213. return 0.0
  214. class MediaDownloadProgress(object):
  215. """Status of a resumable download."""
  216. def __init__(self, resumable_progress, total_size):
  217. """Constructor.
  218. Args:
  219. resumable_progress: int, bytes received so far.
  220. total_size: int, total bytes in complete download.
  221. """
  222. self.resumable_progress = resumable_progress
  223. self.total_size = total_size
  224. def progress(self):
  225. """Percent of download completed, as a float.
  226. Returns:
  227. the percentage complete as a float, returning 0.0 if the total size of
  228. the download is unknown.
  229. """
  230. if self.total_size is not None and self.total_size != 0:
  231. return float(self.resumable_progress) / float(self.total_size)
  232. else:
  233. return 0.0
  234. class MediaUpload(object):
  235. """Describes a media object to upload.
  236. Base class that defines the interface of MediaUpload subclasses.
  237. Note that subclasses of MediaUpload may allow you to control the chunksize
  238. when uploading a media object. It is important to keep the size of the chunk
  239. as large as possible to keep the upload efficient. Other factors may influence
  240. the size of the chunk you use, particularly if you are working in an
  241. environment where individual HTTP requests may have a hardcoded time limit,
  242. such as under certain classes of requests under Google App Engine.
  243. Streams are io.Base compatible objects that support seek(). Some MediaUpload
  244. subclasses support using streams directly to upload data. Support for
  245. streaming may be indicated by a MediaUpload sub-class and if appropriate for a
  246. platform that stream will be used for uploading the media object. The support
  247. for streaming is indicated by has_stream() returning True. The stream() method
  248. should return an io.Base object that supports seek(). On platforms where the
  249. underlying httplib module supports streaming, for example Python 2.6 and
  250. later, the stream will be passed into the http library which will result in
  251. less memory being used and possibly faster uploads.
  252. If you need to upload media that can't be uploaded using any of the existing
  253. MediaUpload sub-class then you can sub-class MediaUpload for your particular
  254. needs.
  255. """
  256. def chunksize(self):
  257. """Chunk size for resumable uploads.
  258. Returns:
  259. Chunk size in bytes.
  260. """
  261. raise NotImplementedError()
  262. def mimetype(self):
  263. """Mime type of the body.
  264. Returns:
  265. Mime type.
  266. """
  267. return "application/octet-stream"
  268. def size(self):
  269. """Size of upload.
  270. Returns:
  271. Size of the body, or None of the size is unknown.
  272. """
  273. return None
  274. def resumable(self):
  275. """Whether this upload is resumable.
  276. Returns:
  277. True if resumable upload or False.
  278. """
  279. return False
  280. def getbytes(self, begin, end):
  281. """Get bytes from the media.
  282. Args:
  283. begin: int, offset from beginning of file.
  284. length: int, number of bytes to read, starting at begin.
  285. Returns:
  286. A string of bytes read. May be shorter than length if EOF was reached
  287. first.
  288. """
  289. raise NotImplementedError()
  290. def has_stream(self):
  291. """Does the underlying upload support a streaming interface.
  292. Streaming means it is an io.IOBase subclass that supports seek, i.e.
  293. seekable() returns True.
  294. Returns:
  295. True if the call to stream() will return an instance of a seekable io.Base
  296. subclass.
  297. """
  298. return False
  299. def stream(self):
  300. """A stream interface to the data being uploaded.
  301. Returns:
  302. The returned value is an io.IOBase subclass that supports seek, i.e.
  303. seekable() returns True.
  304. """
  305. raise NotImplementedError()
  306. @util.positional(1)
  307. def _to_json(self, strip=None):
  308. """Utility function for creating a JSON representation of a MediaUpload.
  309. Args:
  310. strip: array, An array of names of members to not include in the JSON.
  311. Returns:
  312. string, a JSON representation of this instance, suitable to pass to
  313. from_json().
  314. """
  315. t = type(self)
  316. d = copy.copy(self.__dict__)
  317. if strip is not None:
  318. for member in strip:
  319. del d[member]
  320. d["_class"] = t.__name__
  321. d["_module"] = t.__module__
  322. return json.dumps(d)
  323. def to_json(self):
  324. """Create a JSON representation of an instance of MediaUpload.
  325. Returns:
  326. string, a JSON representation of this instance, suitable to pass to
  327. from_json().
  328. """
  329. return self._to_json()
  330. @classmethod
  331. def new_from_json(cls, s):
  332. """Utility class method to instantiate a MediaUpload subclass from a JSON
  333. representation produced by to_json().
  334. Args:
  335. s: string, JSON from to_json().
  336. Returns:
  337. An instance of the subclass of MediaUpload that was serialized with
  338. to_json().
  339. """
  340. data = json.loads(s)
  341. # Find and call the right classmethod from_json() to restore the object.
  342. module = data["_module"]
  343. m = __import__(module, fromlist=module.split(".")[:-1])
  344. kls = getattr(m, data["_class"])
  345. from_json = getattr(kls, "from_json")
  346. return from_json(s)
  347. class MediaIoBaseUpload(MediaUpload):
  348. """A MediaUpload for a io.Base objects.
  349. Note that the Python file object is compatible with io.Base and can be used
  350. with this class also.
  351. fh = BytesIO('...Some data to upload...')
  352. media = MediaIoBaseUpload(fh, mimetype='image/png',
  353. chunksize=1024*1024, resumable=True)
  354. farm.animals().insert(
  355. id='cow',
  356. name='cow.png',
  357. media_body=media).execute()
  358. Depending on the platform you are working on, you may pass -1 as the
  359. chunksize, which indicates that the entire file should be uploaded in a single
  360. request. If the underlying platform supports streams, such as Python 2.6 or
  361. later, then this can be very efficient as it avoids multiple connections, and
  362. also avoids loading the entire file into memory before sending it. Note that
  363. Google App Engine has a 5MB limit on request size, so you should never set
  364. your chunksize larger than 5MB, or to -1.
  365. """
  366. @util.positional(3)
  367. def __init__(self, fd, mimetype, chunksize=DEFAULT_CHUNK_SIZE, resumable=False):
  368. """Constructor.
  369. Args:
  370. fd: io.Base or file object, The source of the bytes to upload. MUST be
  371. opened in blocking mode, do not use streams opened in non-blocking mode.
  372. The given stream must be seekable, that is, it must be able to call
  373. seek() on fd.
  374. mimetype: string, Mime-type of the file.
  375. chunksize: int, File will be uploaded in chunks of this many bytes. Only
  376. used if resumable=True. Pass in a value of -1 if the file is to be
  377. uploaded as a single chunk. Note that Google App Engine has a 5MB limit
  378. on request size, so you should never set your chunksize larger than 5MB,
  379. or to -1.
  380. resumable: bool, True if this is a resumable upload. False means upload
  381. in a single request.
  382. """
  383. super(MediaIoBaseUpload, self).__init__()
  384. self._fd = fd
  385. self._mimetype = mimetype
  386. if not (chunksize == -1 or chunksize > 0):
  387. raise InvalidChunkSizeError()
  388. self._chunksize = chunksize
  389. self._resumable = resumable
  390. self._fd.seek(0, os.SEEK_END)
  391. self._size = self._fd.tell()
  392. def chunksize(self):
  393. """Chunk size for resumable uploads.
  394. Returns:
  395. Chunk size in bytes.
  396. """
  397. return self._chunksize
  398. def mimetype(self):
  399. """Mime type of the body.
  400. Returns:
  401. Mime type.
  402. """
  403. return self._mimetype
  404. def size(self):
  405. """Size of upload.
  406. Returns:
  407. Size of the body, or None of the size is unknown.
  408. """
  409. return self._size
  410. def resumable(self):
  411. """Whether this upload is resumable.
  412. Returns:
  413. True if resumable upload or False.
  414. """
  415. return self._resumable
  416. def getbytes(self, begin, length):
  417. """Get bytes from the media.
  418. Args:
  419. begin: int, offset from beginning of file.
  420. length: int, number of bytes to read, starting at begin.
  421. Returns:
  422. A string of bytes read. May be shorted than length if EOF was reached
  423. first.
  424. """
  425. self._fd.seek(begin)
  426. return self._fd.read(length)
  427. def has_stream(self):
  428. """Does the underlying upload support a streaming interface.
  429. Streaming means it is an io.IOBase subclass that supports seek, i.e.
  430. seekable() returns True.
  431. Returns:
  432. True if the call to stream() will return an instance of a seekable io.Base
  433. subclass.
  434. """
  435. return True
  436. def stream(self):
  437. """A stream interface to the data being uploaded.
  438. Returns:
  439. The returned value is an io.IOBase subclass that supports seek, i.e.
  440. seekable() returns True.
  441. """
  442. return self._fd
  443. def to_json(self):
  444. """This upload type is not serializable."""
  445. raise NotImplementedError("MediaIoBaseUpload is not serializable.")
  446. class MediaFileUpload(MediaIoBaseUpload):
  447. """A MediaUpload for a file.
  448. Construct a MediaFileUpload and pass as the media_body parameter of the
  449. method. For example, if we had a service that allowed uploading images:
  450. media = MediaFileUpload('cow.png', mimetype='image/png',
  451. chunksize=1024*1024, resumable=True)
  452. farm.animals().insert(
  453. id='cow',
  454. name='cow.png',
  455. media_body=media).execute()
  456. Depending on the platform you are working on, you may pass -1 as the
  457. chunksize, which indicates that the entire file should be uploaded in a single
  458. request. If the underlying platform supports streams, such as Python 2.6 or
  459. later, then this can be very efficient as it avoids multiple connections, and
  460. also avoids loading the entire file into memory before sending it. Note that
  461. Google App Engine has a 5MB limit on request size, so you should never set
  462. your chunksize larger than 5MB, or to -1.
  463. """
  464. @util.positional(2)
  465. def __init__(
  466. self, filename, mimetype=None, chunksize=DEFAULT_CHUNK_SIZE, resumable=False
  467. ):
  468. """Constructor.
  469. Args:
  470. filename: string, Name of the file.
  471. mimetype: string, Mime-type of the file. If None then a mime-type will be
  472. guessed from the file extension.
  473. chunksize: int, File will be uploaded in chunks of this many bytes. Only
  474. used if resumable=True. Pass in a value of -1 if the file is to be
  475. uploaded in a single chunk. Note that Google App Engine has a 5MB limit
  476. on request size, so you should never set your chunksize larger than 5MB,
  477. or to -1.
  478. resumable: bool, True if this is a resumable upload. False means upload
  479. in a single request.
  480. """
  481. self._fd = None
  482. self._filename = filename
  483. self._fd = open(self._filename, "rb")
  484. if mimetype is None:
  485. # No mimetype provided, make a guess.
  486. mimetype, _ = mimetypes.guess_type(filename)
  487. if mimetype is None:
  488. # Guess failed, use octet-stream.
  489. mimetype = "application/octet-stream"
  490. super(MediaFileUpload, self).__init__(
  491. self._fd, mimetype, chunksize=chunksize, resumable=resumable
  492. )
  493. def __del__(self):
  494. if self._fd:
  495. self._fd.close()
  496. def to_json(self):
  497. """Creating a JSON representation of an instance of MediaFileUpload.
  498. Returns:
  499. string, a JSON representation of this instance, suitable to pass to
  500. from_json().
  501. """
  502. return self._to_json(strip=["_fd"])
  503. @staticmethod
  504. def from_json(s):
  505. d = json.loads(s)
  506. return MediaFileUpload(
  507. d["_filename"],
  508. mimetype=d["_mimetype"],
  509. chunksize=d["_chunksize"],
  510. resumable=d["_resumable"],
  511. )
  512. class MediaInMemoryUpload(MediaIoBaseUpload):
  513. """MediaUpload for a chunk of bytes.
  514. DEPRECATED: Use MediaIoBaseUpload with either io.TextIOBase or io.StringIO for
  515. the stream.
  516. """
  517. @util.positional(2)
  518. def __init__(
  519. self,
  520. body,
  521. mimetype="application/octet-stream",
  522. chunksize=DEFAULT_CHUNK_SIZE,
  523. resumable=False,
  524. ):
  525. """Create a new MediaInMemoryUpload.
  526. DEPRECATED: Use MediaIoBaseUpload with either io.TextIOBase or io.StringIO for
  527. the stream.
  528. Args:
  529. body: string, Bytes of body content.
  530. mimetype: string, Mime-type of the file or default of
  531. 'application/octet-stream'.
  532. chunksize: int, File will be uploaded in chunks of this many bytes. Only
  533. used if resumable=True.
  534. resumable: bool, True if this is a resumable upload. False means upload
  535. in a single request.
  536. """
  537. fd = io.BytesIO(body)
  538. super(MediaInMemoryUpload, self).__init__(
  539. fd, mimetype, chunksize=chunksize, resumable=resumable
  540. )
  541. class MediaIoBaseDownload(object):
  542. """ "Download media resources.
  543. Note that the Python file object is compatible with io.Base and can be used
  544. with this class also.
  545. Example:
  546. request = farms.animals().get_media(id='cow')
  547. fh = io.FileIO('cow.png', mode='wb')
  548. downloader = MediaIoBaseDownload(fh, request, chunksize=1024*1024)
  549. done = False
  550. while done is False:
  551. status, done = downloader.next_chunk()
  552. if status:
  553. print "Download %d%%." % int(status.progress() * 100)
  554. print "Download Complete!"
  555. """
  556. @util.positional(3)
  557. def __init__(self, fd, request, chunksize=DEFAULT_CHUNK_SIZE):
  558. """Constructor.
  559. Args:
  560. fd: io.Base or file object, The stream in which to write the downloaded
  561. bytes.
  562. request: googleapiclient.http.HttpRequest, the media request to perform in
  563. chunks.
  564. chunksize: int, File will be downloaded in chunks of this many bytes.
  565. """
  566. self._fd = fd
  567. self._request = request
  568. self._uri = request.uri
  569. self._chunksize = chunksize
  570. self._progress = 0
  571. self._total_size = None
  572. self._done = False
  573. # Stubs for testing.
  574. self._sleep = time.sleep
  575. self._rand = random.random
  576. self._headers = {}
  577. for k, v in request.headers.items():
  578. # allow users to supply custom headers by setting them on the request
  579. # but strip out the ones that are set by default on requests generated by
  580. # API methods like Drive's files().get(fileId=...)
  581. if not k.lower() in ("accept", "accept-encoding", "user-agent"):
  582. self._headers[k] = v
  583. @util.positional(1)
  584. def next_chunk(self, num_retries=0):
  585. """Get the next chunk of the download.
  586. Args:
  587. num_retries: Integer, number of times to retry with randomized
  588. exponential backoff. If all retries fail, the raised HttpError
  589. represents the last request. If zero (default), we attempt the
  590. request only once.
  591. Returns:
  592. (status, done): (MediaDownloadProgress, boolean)
  593. The value of 'done' will be True when the media has been fully
  594. downloaded or the total size of the media is unknown.
  595. Raises:
  596. googleapiclient.errors.HttpError if the response was not a 2xx.
  597. httplib2.HttpLib2Error if a transport error has occurred.
  598. """
  599. headers = self._headers.copy()
  600. headers["range"] = "bytes=%d-%d" % (
  601. self._progress,
  602. self._progress + self._chunksize - 1,
  603. )
  604. http = self._request.http
  605. resp, content = _retry_request(
  606. http,
  607. num_retries,
  608. "media download",
  609. self._sleep,
  610. self._rand,
  611. self._uri,
  612. "GET",
  613. headers=headers,
  614. )
  615. if resp.status in [200, 206]:
  616. if "content-location" in resp and resp["content-location"] != self._uri:
  617. self._uri = resp["content-location"]
  618. self._progress += len(content)
  619. self._fd.write(content)
  620. if "content-range" in resp:
  621. content_range = resp["content-range"]
  622. length = content_range.rsplit("/", 1)[1]
  623. self._total_size = int(length)
  624. elif "content-length" in resp:
  625. self._total_size = int(resp["content-length"])
  626. if self._total_size is None or self._progress == self._total_size:
  627. self._done = True
  628. return MediaDownloadProgress(self._progress, self._total_size), self._done
  629. elif resp.status == 416:
  630. # 416 is Range Not Satisfiable
  631. # This typically occurs with a zero byte file
  632. content_range = resp["content-range"]
  633. length = content_range.rsplit("/", 1)[1]
  634. self._total_size = int(length)
  635. if self._total_size == 0:
  636. self._done = True
  637. return (
  638. MediaDownloadProgress(self._progress, self._total_size),
  639. self._done,
  640. )
  641. raise HttpError(resp, content, uri=self._uri)
  642. class _StreamSlice(object):
  643. """Truncated stream.
  644. Takes a stream and presents a stream that is a slice of the original stream.
  645. This is used when uploading media in chunks. In later versions of Python a
  646. stream can be passed to httplib in place of the string of data to send. The
  647. problem is that httplib just blindly reads to the end of the stream. This
  648. wrapper presents a virtual stream that only reads to the end of the chunk.
  649. """
  650. def __init__(self, stream, begin, chunksize):
  651. """Constructor.
  652. Args:
  653. stream: (io.Base, file object), the stream to wrap.
  654. begin: int, the seek position the chunk begins at.
  655. chunksize: int, the size of the chunk.
  656. """
  657. self._stream = stream
  658. self._begin = begin
  659. self._chunksize = chunksize
  660. self._stream.seek(begin)
  661. def read(self, n=-1):
  662. """Read n bytes.
  663. Args:
  664. n, int, the number of bytes to read.
  665. Returns:
  666. A string of length 'n', or less if EOF is reached.
  667. """
  668. # The data left available to read sits in [cur, end)
  669. cur = self._stream.tell()
  670. end = self._begin + self._chunksize
  671. if n == -1 or cur + n > end:
  672. n = end - cur
  673. return self._stream.read(n)
  674. class HttpRequest(object):
  675. """Encapsulates a single HTTP request."""
  676. @util.positional(4)
  677. def __init__(
  678. self,
  679. http,
  680. postproc,
  681. uri,
  682. method="GET",
  683. body=None,
  684. headers=None,
  685. methodId=None,
  686. resumable=None,
  687. ):
  688. """Constructor for an HttpRequest.
  689. Args:
  690. http: httplib2.Http, the transport object to use to make a request
  691. postproc: callable, called on the HTTP response and content to transform
  692. it into a data object before returning, or raising an exception
  693. on an error.
  694. uri: string, the absolute URI to send the request to
  695. method: string, the HTTP method to use
  696. body: string, the request body of the HTTP request,
  697. headers: dict, the HTTP request headers
  698. methodId: string, a unique identifier for the API method being called.
  699. resumable: MediaUpload, None if this is not a resumbale request.
  700. """
  701. self.uri = uri
  702. self.method = method
  703. self.body = body
  704. self.headers = headers or {}
  705. self.methodId = methodId
  706. self.http = http
  707. self.postproc = postproc
  708. self.resumable = resumable
  709. self.response_callbacks = []
  710. self._in_error_state = False
  711. # The size of the non-media part of the request.
  712. self.body_size = len(self.body or "")
  713. # The resumable URI to send chunks to.
  714. self.resumable_uri = None
  715. # The bytes that have been uploaded.
  716. self.resumable_progress = 0
  717. # Stubs for testing.
  718. self._rand = random.random
  719. self._sleep = time.sleep
  720. @util.positional(1)
  721. def execute(self, http=None, num_retries=0):
  722. """Execute the request.
  723. Args:
  724. http: httplib2.Http, an http object to be used in place of the
  725. one the HttpRequest request object was constructed with.
  726. num_retries: Integer, number of times to retry with randomized
  727. exponential backoff. If all retries fail, the raised HttpError
  728. represents the last request. If zero (default), we attempt the
  729. request only once.
  730. Returns:
  731. A deserialized object model of the response body as determined
  732. by the postproc.
  733. Raises:
  734. googleapiclient.errors.HttpError if the response was not a 2xx.
  735. httplib2.HttpLib2Error if a transport error has occurred.
  736. """
  737. if http is None:
  738. http = self.http
  739. if self.resumable:
  740. body = None
  741. while body is None:
  742. _, body = self.next_chunk(http=http, num_retries=num_retries)
  743. return body
  744. # Non-resumable case.
  745. if "content-length" not in self.headers:
  746. self.headers["content-length"] = str(self.body_size)
  747. # If the request URI is too long then turn it into a POST request.
  748. # Assume that a GET request never contains a request body.
  749. if len(self.uri) > MAX_URI_LENGTH and self.method == "GET":
  750. self.method = "POST"
  751. self.headers["x-http-method-override"] = "GET"
  752. self.headers["content-type"] = "application/x-www-form-urlencoded"
  753. parsed = urllib.parse.urlparse(self.uri)
  754. self.uri = urllib.parse.urlunparse(
  755. (parsed.scheme, parsed.netloc, parsed.path, parsed.params, None, None)
  756. )
  757. self.body = parsed.query
  758. self.headers["content-length"] = str(len(self.body))
  759. # Handle retries for server-side errors.
  760. resp, content = _retry_request(
  761. http,
  762. num_retries,
  763. "request",
  764. self._sleep,
  765. self._rand,
  766. str(self.uri),
  767. method=str(self.method),
  768. body=self.body,
  769. headers=self.headers,
  770. )
  771. for callback in self.response_callbacks:
  772. callback(resp)
  773. if resp.status >= 300:
  774. raise HttpError(resp, content, uri=self.uri)
  775. return self.postproc(resp, content)
  776. @util.positional(2)
  777. def add_response_callback(self, cb):
  778. """add_response_headers_callback
  779. Args:
  780. cb: Callback to be called on receiving the response headers, of signature:
  781. def cb(resp):
  782. # Where resp is an instance of httplib2.Response
  783. """
  784. self.response_callbacks.append(cb)
  785. @util.positional(1)
  786. def next_chunk(self, http=None, num_retries=0):
  787. """Execute the next step of a resumable upload.
  788. Can only be used if the method being executed supports media uploads and
  789. the MediaUpload object passed in was flagged as using resumable upload.
  790. Example:
  791. media = MediaFileUpload('cow.png', mimetype='image/png',
  792. chunksize=1000, resumable=True)
  793. request = farm.animals().insert(
  794. id='cow',
  795. name='cow.png',
  796. media_body=media)
  797. response = None
  798. while response is None:
  799. status, response = request.next_chunk()
  800. if status:
  801. print "Upload %d%% complete." % int(status.progress() * 100)
  802. Args:
  803. http: httplib2.Http, an http object to be used in place of the
  804. one the HttpRequest request object was constructed with.
  805. num_retries: Integer, number of times to retry with randomized
  806. exponential backoff. If all retries fail, the raised HttpError
  807. represents the last request. If zero (default), we attempt the
  808. request only once.
  809. Returns:
  810. (status, body): (ResumableMediaStatus, object)
  811. The body will be None until the resumable media is fully uploaded.
  812. Raises:
  813. googleapiclient.errors.HttpError if the response was not a 2xx.
  814. httplib2.HttpLib2Error if a transport error has occurred.
  815. """
  816. if http is None:
  817. http = self.http
  818. if self.resumable.size() is None:
  819. size = "*"
  820. else:
  821. size = str(self.resumable.size())
  822. if self.resumable_uri is None:
  823. start_headers = copy.copy(self.headers)
  824. start_headers["X-Upload-Content-Type"] = self.resumable.mimetype()
  825. if size != "*":
  826. start_headers["X-Upload-Content-Length"] = size
  827. start_headers["content-length"] = str(self.body_size)
  828. resp, content = _retry_request(
  829. http,
  830. num_retries,
  831. "resumable URI request",
  832. self._sleep,
  833. self._rand,
  834. self.uri,
  835. method=self.method,
  836. body=self.body,
  837. headers=start_headers,
  838. )
  839. if resp.status == 200 and "location" in resp:
  840. self.resumable_uri = resp["location"]
  841. else:
  842. raise ResumableUploadError(resp, content)
  843. elif self._in_error_state:
  844. # If we are in an error state then query the server for current state of
  845. # the upload by sending an empty PUT and reading the 'range' header in
  846. # the response.
  847. headers = {"Content-Range": "bytes */%s" % size, "content-length": "0"}
  848. resp, content = http.request(self.resumable_uri, "PUT", headers=headers)
  849. status, body = self._process_response(resp, content)
  850. if body:
  851. # The upload was complete.
  852. return (status, body)
  853. if self.resumable.has_stream():
  854. data = self.resumable.stream()
  855. if self.resumable.chunksize() == -1:
  856. data.seek(self.resumable_progress)
  857. chunk_end = self.resumable.size() - self.resumable_progress - 1
  858. else:
  859. # Doing chunking with a stream, so wrap a slice of the stream.
  860. data = _StreamSlice(
  861. data, self.resumable_progress, self.resumable.chunksize()
  862. )
  863. chunk_end = min(
  864. self.resumable_progress + self.resumable.chunksize() - 1,
  865. self.resumable.size() - 1,
  866. )
  867. else:
  868. data = self.resumable.getbytes(
  869. self.resumable_progress, self.resumable.chunksize()
  870. )
  871. # A short read implies that we are at EOF, so finish the upload.
  872. if len(data) < self.resumable.chunksize():
  873. size = str(self.resumable_progress + len(data))
  874. chunk_end = self.resumable_progress + len(data) - 1
  875. headers = {
  876. # Must set the content-length header here because httplib can't
  877. # calculate the size when working with _StreamSlice.
  878. "Content-Length": str(chunk_end - self.resumable_progress + 1),
  879. }
  880. # An empty file results in chunk_end = -1 and size = 0
  881. # sending "bytes 0--1/0" results in an invalid request
  882. # Only add header "Content-Range" if chunk_end != -1
  883. if chunk_end != -1:
  884. headers["Content-Range"] = "bytes %d-%d/%s" % (
  885. self.resumable_progress,
  886. chunk_end,
  887. size,
  888. )
  889. for retry_num in range(num_retries + 1):
  890. if retry_num > 0:
  891. self._sleep(self._rand() * 2**retry_num)
  892. LOGGER.warning(
  893. "Retry #%d for media upload: %s %s, following status: %d"
  894. % (retry_num, self.method, self.uri, resp.status)
  895. )
  896. try:
  897. resp, content = http.request(
  898. self.resumable_uri, method="PUT", body=data, headers=headers
  899. )
  900. except:
  901. self._in_error_state = True
  902. raise
  903. if not _should_retry_response(resp.status, content):
  904. break
  905. return self._process_response(resp, content)
  906. def _process_response(self, resp, content):
  907. """Process the response from a single chunk upload.
  908. Args:
  909. resp: httplib2.Response, the response object.
  910. content: string, the content of the response.
  911. Returns:
  912. (status, body): (ResumableMediaStatus, object)
  913. The body will be None until the resumable media is fully uploaded.
  914. Raises:
  915. googleapiclient.errors.HttpError if the response was not a 2xx or a 308.
  916. """
  917. if resp.status in [200, 201]:
  918. self._in_error_state = False
  919. return None, self.postproc(resp, content)
  920. elif resp.status == 308:
  921. self._in_error_state = False
  922. # A "308 Resume Incomplete" indicates we are not done.
  923. try:
  924. self.resumable_progress = int(resp["range"].split("-")[1]) + 1
  925. except KeyError:
  926. # If resp doesn't contain range header, resumable progress is 0
  927. self.resumable_progress = 0
  928. if "location" in resp:
  929. self.resumable_uri = resp["location"]
  930. else:
  931. self._in_error_state = True
  932. raise HttpError(resp, content, uri=self.uri)
  933. return (
  934. MediaUploadProgress(self.resumable_progress, self.resumable.size()),
  935. None,
  936. )
  937. def to_json(self):
  938. """Returns a JSON representation of the HttpRequest."""
  939. d = copy.copy(self.__dict__)
  940. if d["resumable"] is not None:
  941. d["resumable"] = self.resumable.to_json()
  942. del d["http"]
  943. del d["postproc"]
  944. del d["_sleep"]
  945. del d["_rand"]
  946. return json.dumps(d)
  947. @staticmethod
  948. def from_json(s, http, postproc):
  949. """Returns an HttpRequest populated with info from a JSON object."""
  950. d = json.loads(s)
  951. if d["resumable"] is not None:
  952. d["resumable"] = MediaUpload.new_from_json(d["resumable"])
  953. return HttpRequest(
  954. http,
  955. postproc,
  956. uri=d["uri"],
  957. method=d["method"],
  958. body=d["body"],
  959. headers=d["headers"],
  960. methodId=d["methodId"],
  961. resumable=d["resumable"],
  962. )
  963. @staticmethod
  964. def null_postproc(resp, contents):
  965. return resp, contents
  966. class BatchHttpRequest(object):
  967. """Batches multiple HttpRequest objects into a single HTTP request.
  968. Example:
  969. from googleapiclient.http import BatchHttpRequest
  970. def list_animals(request_id, response, exception):
  971. \"\"\"Do something with the animals list response.\"\"\"
  972. if exception is not None:
  973. # Do something with the exception.
  974. pass
  975. else:
  976. # Do something with the response.
  977. pass
  978. def list_farmers(request_id, response, exception):
  979. \"\"\"Do something with the farmers list response.\"\"\"
  980. if exception is not None:
  981. # Do something with the exception.
  982. pass
  983. else:
  984. # Do something with the response.
  985. pass
  986. service = build('farm', 'v2')
  987. batch = BatchHttpRequest()
  988. batch.add(service.animals().list(), list_animals)
  989. batch.add(service.farmers().list(), list_farmers)
  990. batch.execute(http=http)
  991. """
  992. @util.positional(1)
  993. def __init__(self, callback=None, batch_uri=None):
  994. """Constructor for a BatchHttpRequest.
  995. Args:
  996. callback: callable, A callback to be called for each response, of the
  997. form callback(id, response, exception). The first parameter is the
  998. request id, and the second is the deserialized response object. The
  999. third is an googleapiclient.errors.HttpError exception object if an HTTP error
  1000. occurred while processing the request, or None if no error occurred.
  1001. batch_uri: string, URI to send batch requests to.
  1002. """
  1003. if batch_uri is None:
  1004. batch_uri = _LEGACY_BATCH_URI
  1005. if batch_uri == _LEGACY_BATCH_URI:
  1006. LOGGER.warning(
  1007. "You have constructed a BatchHttpRequest using the legacy batch "
  1008. "endpoint %s. This endpoint will be turned down on August 12, 2020. "
  1009. "Please provide the API-specific endpoint or use "
  1010. "service.new_batch_http_request(). For more details see "
  1011. "https://developers.googleblog.com/2018/03/discontinuing-support-for-json-rpc-and.html"
  1012. "and https://developers.google.com/api-client-library/python/guide/batch.",
  1013. _LEGACY_BATCH_URI,
  1014. )
  1015. self._batch_uri = batch_uri
  1016. # Global callback to be called for each individual response in the batch.
  1017. self._callback = callback
  1018. # A map from id to request.
  1019. self._requests = {}
  1020. # A map from id to callback.
  1021. self._callbacks = {}
  1022. # List of request ids, in the order in which they were added.
  1023. self._order = []
  1024. # The last auto generated id.
  1025. self._last_auto_id = 0
  1026. # Unique ID on which to base the Content-ID headers.
  1027. self._base_id = None
  1028. # A map from request id to (httplib2.Response, content) response pairs
  1029. self._responses = {}
  1030. # A map of id(Credentials) that have been refreshed.
  1031. self._refreshed_credentials = {}
  1032. def _refresh_and_apply_credentials(self, request, http):
  1033. """Refresh the credentials and apply to the request.
  1034. Args:
  1035. request: HttpRequest, the request.
  1036. http: httplib2.Http, the global http object for the batch.
  1037. """
  1038. # For the credentials to refresh, but only once per refresh_token
  1039. # If there is no http per the request then refresh the http passed in
  1040. # via execute()
  1041. creds = None
  1042. request_credentials = False
  1043. if request.http is not None:
  1044. creds = _auth.get_credentials_from_http(request.http)
  1045. request_credentials = True
  1046. if creds is None and http is not None:
  1047. creds = _auth.get_credentials_from_http(http)
  1048. if creds is not None:
  1049. if id(creds) not in self._refreshed_credentials:
  1050. _auth.refresh_credentials(creds)
  1051. self._refreshed_credentials[id(creds)] = 1
  1052. # Only apply the credentials if we are using the http object passed in,
  1053. # otherwise apply() will get called during _serialize_request().
  1054. if request.http is None or not request_credentials:
  1055. _auth.apply_credentials(creds, request.headers)
  1056. def _id_to_header(self, id_):
  1057. """Convert an id to a Content-ID header value.
  1058. Args:
  1059. id_: string, identifier of individual request.
  1060. Returns:
  1061. A Content-ID header with the id_ encoded into it. A UUID is prepended to
  1062. the value because Content-ID headers are supposed to be universally
  1063. unique.
  1064. """
  1065. if self._base_id is None:
  1066. self._base_id = uuid.uuid4()
  1067. # NB: we intentionally leave whitespace between base/id and '+', so RFC2822
  1068. # line folding works properly on Python 3; see
  1069. # https://github.com/googleapis/google-api-python-client/issues/164
  1070. return "<%s + %s>" % (self._base_id, urllib.parse.quote(id_))
  1071. def _header_to_id(self, header):
  1072. """Convert a Content-ID header value to an id.
  1073. Presumes the Content-ID header conforms to the format that _id_to_header()
  1074. returns.
  1075. Args:
  1076. header: string, Content-ID header value.
  1077. Returns:
  1078. The extracted id value.
  1079. Raises:
  1080. BatchError if the header is not in the expected format.
  1081. """
  1082. if header[0] != "<" or header[-1] != ">":
  1083. raise BatchError("Invalid value for Content-ID: %s" % header)
  1084. if "+" not in header:
  1085. raise BatchError("Invalid value for Content-ID: %s" % header)
  1086. base, id_ = header[1:-1].split(" + ", 1)
  1087. return urllib.parse.unquote(id_)
  1088. def _serialize_request(self, request):
  1089. """Convert an HttpRequest object into a string.
  1090. Args:
  1091. request: HttpRequest, the request to serialize.
  1092. Returns:
  1093. The request as a string in application/http format.
  1094. """
  1095. # Construct status line
  1096. parsed = urllib.parse.urlparse(request.uri)
  1097. request_line = urllib.parse.urlunparse(
  1098. ("", "", parsed.path, parsed.params, parsed.query, "")
  1099. )
  1100. status_line = request.method + " " + request_line + " HTTP/1.1\n"
  1101. major, minor = request.headers.get("content-type", "application/json").split(
  1102. "/"
  1103. )
  1104. msg = MIMENonMultipart(major, minor)
  1105. headers = request.headers.copy()
  1106. if request.http is not None:
  1107. credentials = _auth.get_credentials_from_http(request.http)
  1108. if credentials is not None:
  1109. _auth.apply_credentials(credentials, headers)
  1110. # MIMENonMultipart adds its own Content-Type header.
  1111. if "content-type" in headers:
  1112. del headers["content-type"]
  1113. for key, value in headers.items():
  1114. msg[key] = value
  1115. msg["Host"] = parsed.netloc
  1116. msg.set_unixfrom(None)
  1117. if request.body is not None:
  1118. msg.set_payload(request.body)
  1119. msg["content-length"] = str(len(request.body))
  1120. # Serialize the mime message.
  1121. fp = io.StringIO()
  1122. # maxheaderlen=0 means don't line wrap headers.
  1123. g = Generator(fp, maxheaderlen=0)
  1124. g.flatten(msg, unixfrom=False)
  1125. body = fp.getvalue()
  1126. return status_line + body
  1127. def _deserialize_response(self, payload):
  1128. """Convert string into httplib2 response and content.
  1129. Args:
  1130. payload: string, headers and body as a string.
  1131. Returns:
  1132. A pair (resp, content), such as would be returned from httplib2.request.
  1133. """
  1134. # Strip off the status line
  1135. status_line, payload = payload.split("\n", 1)
  1136. protocol, status, reason = status_line.split(" ", 2)
  1137. # Parse the rest of the response
  1138. parser = FeedParser()
  1139. parser.feed(payload)
  1140. msg = parser.close()
  1141. msg["status"] = status
  1142. # Create httplib2.Response from the parsed headers.
  1143. resp = httplib2.Response(msg)
  1144. resp.reason = reason
  1145. resp.version = int(protocol.split("/", 1)[1].replace(".", ""))
  1146. content = payload.split("\r\n\r\n", 1)[1]
  1147. return resp, content
  1148. def _new_id(self):
  1149. """Create a new id.
  1150. Auto incrementing number that avoids conflicts with ids already used.
  1151. Returns:
  1152. string, a new unique id.
  1153. """
  1154. self._last_auto_id += 1
  1155. while str(self._last_auto_id) in self._requests:
  1156. self._last_auto_id += 1
  1157. return str(self._last_auto_id)
  1158. @util.positional(2)
  1159. def add(self, request, callback=None, request_id=None):
  1160. """Add a new request.
  1161. Every callback added will be paired with a unique id, the request_id. That
  1162. unique id will be passed back to the callback when the response comes back
  1163. from the server. The default behavior is to have the library generate it's
  1164. own unique id. If the caller passes in a request_id then they must ensure
  1165. uniqueness for each request_id, and if they are not an exception is
  1166. raised. Callers should either supply all request_ids or never supply a
  1167. request id, to avoid such an error.
  1168. Args:
  1169. request: HttpRequest, Request to add to the batch.
  1170. callback: callable, A callback to be called for this response, of the
  1171. form callback(id, response, exception). The first parameter is the
  1172. request id, and the second is the deserialized response object. The
  1173. third is an googleapiclient.errors.HttpError exception object if an HTTP error
  1174. occurred while processing the request, or None if no errors occurred.
  1175. request_id: string, A unique id for the request. The id will be passed
  1176. to the callback with the response.
  1177. Returns:
  1178. None
  1179. Raises:
  1180. BatchError if a media request is added to a batch.
  1181. KeyError is the request_id is not unique.
  1182. """
  1183. if len(self._order) >= MAX_BATCH_LIMIT:
  1184. raise BatchError(
  1185. "Exceeded the maximum calls(%d) in a single batch request."
  1186. % MAX_BATCH_LIMIT
  1187. )
  1188. if request_id is None:
  1189. request_id = self._new_id()
  1190. if request.resumable is not None:
  1191. raise BatchError("Media requests cannot be used in a batch request.")
  1192. if request_id in self._requests:
  1193. raise KeyError("A request with this ID already exists: %s" % request_id)
  1194. self._requests[request_id] = request
  1195. self._callbacks[request_id] = callback
  1196. self._order.append(request_id)
  1197. def _execute(self, http, order, requests):
  1198. """Serialize batch request, send to server, process response.
  1199. Args:
  1200. http: httplib2.Http, an http object to be used to make the request with.
  1201. order: list, list of request ids in the order they were added to the
  1202. batch.
  1203. requests: list, list of request objects to send.
  1204. Raises:
  1205. httplib2.HttpLib2Error if a transport error has occurred.
  1206. googleapiclient.errors.BatchError if the response is the wrong format.
  1207. """
  1208. message = MIMEMultipart("mixed")
  1209. # Message should not write out it's own headers.
  1210. setattr(message, "_write_headers", lambda self: None)
  1211. # Add all the individual requests.
  1212. for request_id in order:
  1213. request = requests[request_id]
  1214. msg = MIMENonMultipart("application", "http")
  1215. msg["Content-Transfer-Encoding"] = "binary"
  1216. msg["Content-ID"] = self._id_to_header(request_id)
  1217. body = self._serialize_request(request)
  1218. msg.set_payload(body)
  1219. message.attach(msg)
  1220. # encode the body: note that we can't use `as_string`, because
  1221. # it plays games with `From ` lines.
  1222. fp = io.StringIO()
  1223. g = Generator(fp, mangle_from_=False)
  1224. g.flatten(message, unixfrom=False)
  1225. body = fp.getvalue()
  1226. headers = {}
  1227. headers["content-type"] = (
  1228. "multipart/mixed; " 'boundary="%s"'
  1229. ) % message.get_boundary()
  1230. resp, content = http.request(
  1231. self._batch_uri, method="POST", body=body, headers=headers
  1232. )
  1233. if resp.status >= 300:
  1234. raise HttpError(resp, content, uri=self._batch_uri)
  1235. # Prepend with a content-type header so FeedParser can handle it.
  1236. header = "content-type: %s\r\n\r\n" % resp["content-type"]
  1237. # PY3's FeedParser only accepts unicode. So we should decode content
  1238. # here, and encode each payload again.
  1239. content = content.decode("utf-8")
  1240. for_parser = header + content
  1241. parser = FeedParser()
  1242. parser.feed(for_parser)
  1243. mime_response = parser.close()
  1244. if not mime_response.is_multipart():
  1245. raise BatchError(
  1246. "Response not in multipart/mixed format.", resp=resp, content=content
  1247. )
  1248. for part in mime_response.get_payload():
  1249. request_id = self._header_to_id(part["Content-ID"])
  1250. response, content = self._deserialize_response(part.get_payload())
  1251. # We encode content here to emulate normal http response.
  1252. if isinstance(content, str):
  1253. content = content.encode("utf-8")
  1254. self._responses[request_id] = (response, content)
  1255. @util.positional(1)
  1256. def execute(self, http=None):
  1257. """Execute all the requests as a single batched HTTP request.
  1258. Args:
  1259. http: httplib2.Http, an http object to be used in place of the one the
  1260. HttpRequest request object was constructed with. If one isn't supplied
  1261. then use a http object from the requests in this batch.
  1262. Returns:
  1263. None
  1264. Raises:
  1265. httplib2.HttpLib2Error if a transport error has occurred.
  1266. googleapiclient.errors.BatchError if the response is the wrong format.
  1267. """
  1268. # If we have no requests return
  1269. if len(self._order) == 0:
  1270. return None
  1271. # If http is not supplied use the first valid one given in the requests.
  1272. if http is None:
  1273. for request_id in self._order:
  1274. request = self._requests[request_id]
  1275. if request is not None:
  1276. http = request.http
  1277. break
  1278. if http is None:
  1279. raise ValueError("Missing a valid http object.")
  1280. # Special case for OAuth2Credentials-style objects which have not yet been
  1281. # refreshed with an initial access_token.
  1282. creds = _auth.get_credentials_from_http(http)
  1283. if creds is not None:
  1284. if not _auth.is_valid(creds):
  1285. LOGGER.info("Attempting refresh to obtain initial access_token")
  1286. _auth.refresh_credentials(creds)
  1287. self._execute(http, self._order, self._requests)
  1288. # Loop over all the requests and check for 401s. For each 401 request the
  1289. # credentials should be refreshed and then sent again in a separate batch.
  1290. redo_requests = {}
  1291. redo_order = []
  1292. for request_id in self._order:
  1293. resp, content = self._responses[request_id]
  1294. if resp["status"] == "401":
  1295. redo_order.append(request_id)
  1296. request = self._requests[request_id]
  1297. self._refresh_and_apply_credentials(request, http)
  1298. redo_requests[request_id] = request
  1299. if redo_requests:
  1300. self._execute(http, redo_order, redo_requests)
  1301. # Now process all callbacks that are erroring, and raise an exception for
  1302. # ones that return a non-2xx response? Or add extra parameter to callback
  1303. # that contains an HttpError?
  1304. for request_id in self._order:
  1305. resp, content = self._responses[request_id]
  1306. request = self._requests[request_id]
  1307. callback = self._callbacks[request_id]
  1308. response = None
  1309. exception = None
  1310. try:
  1311. if resp.status >= 300:
  1312. raise HttpError(resp, content, uri=request.uri)
  1313. response = request.postproc(resp, content)
  1314. except HttpError as e:
  1315. exception = e
  1316. if callback is not None:
  1317. callback(request_id, response, exception)
  1318. if self._callback is not None:
  1319. self._callback(request_id, response, exception)
  1320. class HttpRequestMock(object):
  1321. """Mock of HttpRequest.
  1322. Do not construct directly, instead use RequestMockBuilder.
  1323. """
  1324. def __init__(self, resp, content, postproc):
  1325. """Constructor for HttpRequestMock
  1326. Args:
  1327. resp: httplib2.Response, the response to emulate coming from the request
  1328. content: string, the response body
  1329. postproc: callable, the post processing function usually supplied by
  1330. the model class. See model.JsonModel.response() as an example.
  1331. """
  1332. self.resp = resp
  1333. self.content = content
  1334. self.postproc = postproc
  1335. if resp is None:
  1336. self.resp = httplib2.Response({"status": 200, "reason": "OK"})
  1337. if "reason" in self.resp:
  1338. self.resp.reason = self.resp["reason"]
  1339. def execute(self, http=None):
  1340. """Execute the request.
  1341. Same behavior as HttpRequest.execute(), but the response is
  1342. mocked and not really from an HTTP request/response.
  1343. """
  1344. return self.postproc(self.resp, self.content)
  1345. class RequestMockBuilder(object):
  1346. """A simple mock of HttpRequest
  1347. Pass in a dictionary to the constructor that maps request methodIds to
  1348. tuples of (httplib2.Response, content, opt_expected_body) that should be
  1349. returned when that method is called. None may also be passed in for the
  1350. httplib2.Response, in which case a 200 OK response will be generated.
  1351. If an opt_expected_body (str or dict) is provided, it will be compared to
  1352. the body and UnexpectedBodyError will be raised on inequality.
  1353. Example:
  1354. response = '{"data": {"id": "tag:google.c...'
  1355. requestBuilder = RequestMockBuilder(
  1356. {
  1357. 'plus.activities.get': (None, response),
  1358. }
  1359. )
  1360. googleapiclient.discovery.build("plus", "v1", requestBuilder=requestBuilder)
  1361. Methods that you do not supply a response for will return a
  1362. 200 OK with an empty string as the response content or raise an excpetion
  1363. if check_unexpected is set to True. The methodId is taken from the rpcName
  1364. in the discovery document.
  1365. For more details see the project wiki.
  1366. """
  1367. def __init__(self, responses, check_unexpected=False):
  1368. """Constructor for RequestMockBuilder
  1369. The constructed object should be a callable object
  1370. that can replace the class HttpResponse.
  1371. responses - A dictionary that maps methodIds into tuples
  1372. of (httplib2.Response, content). The methodId
  1373. comes from the 'rpcName' field in the discovery
  1374. document.
  1375. check_unexpected - A boolean setting whether or not UnexpectedMethodError
  1376. should be raised on unsupplied method.
  1377. """
  1378. self.responses = responses
  1379. self.check_unexpected = check_unexpected
  1380. def __call__(
  1381. self,
  1382. http,
  1383. postproc,
  1384. uri,
  1385. method="GET",
  1386. body=None,
  1387. headers=None,
  1388. methodId=None,
  1389. resumable=None,
  1390. ):
  1391. """Implements the callable interface that discovery.build() expects
  1392. of requestBuilder, which is to build an object compatible with
  1393. HttpRequest.execute(). See that method for the description of the
  1394. parameters and the expected response.
  1395. """
  1396. if methodId in self.responses:
  1397. response = self.responses[methodId]
  1398. resp, content = response[:2]
  1399. if len(response) > 2:
  1400. # Test the body against the supplied expected_body.
  1401. expected_body = response[2]
  1402. if bool(expected_body) != bool(body):
  1403. # Not expecting a body and provided one
  1404. # or expecting a body and not provided one.
  1405. raise UnexpectedBodyError(expected_body, body)
  1406. if isinstance(expected_body, str):
  1407. expected_body = json.loads(expected_body)
  1408. body = json.loads(body)
  1409. if body != expected_body:
  1410. raise UnexpectedBodyError(expected_body, body)
  1411. return HttpRequestMock(resp, content, postproc)
  1412. elif self.check_unexpected:
  1413. raise UnexpectedMethodError(methodId=methodId)
  1414. else:
  1415. model = JsonModel(False)
  1416. return HttpRequestMock(None, "{}", model.response)
  1417. class HttpMock(object):
  1418. """Mock of httplib2.Http"""
  1419. def __init__(self, filename=None, headers=None):
  1420. """
  1421. Args:
  1422. filename: string, absolute filename to read response from
  1423. headers: dict, header to return with response
  1424. """
  1425. if headers is None:
  1426. headers = {"status": "200"}
  1427. if filename:
  1428. with open(filename, "rb") as f:
  1429. self.data = f.read()
  1430. else:
  1431. self.data = None
  1432. self.response_headers = headers
  1433. self.headers = None
  1434. self.uri = None
  1435. self.method = None
  1436. self.body = None
  1437. self.headers = None
  1438. def request(
  1439. self,
  1440. uri,
  1441. method="GET",
  1442. body=None,
  1443. headers=None,
  1444. redirections=1,
  1445. connection_type=None,
  1446. ):
  1447. self.uri = uri
  1448. self.method = method
  1449. self.body = body
  1450. self.headers = headers
  1451. return httplib2.Response(self.response_headers), self.data
  1452. def close(self):
  1453. return None
  1454. class HttpMockSequence(object):
  1455. """Mock of httplib2.Http
  1456. Mocks a sequence of calls to request returning different responses for each
  1457. call. Create an instance initialized with the desired response headers
  1458. and content and then use as if an httplib2.Http instance.
  1459. http = HttpMockSequence([
  1460. ({'status': '401'}, ''),
  1461. ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
  1462. ({'status': '200'}, 'echo_request_headers'),
  1463. ])
  1464. resp, content = http.request("http://examples.com")
  1465. There are special values you can pass in for content to trigger
  1466. behavours that are helpful in testing.
  1467. 'echo_request_headers' means return the request headers in the response body
  1468. 'echo_request_headers_as_json' means return the request headers in
  1469. the response body
  1470. 'echo_request_body' means return the request body in the response body
  1471. 'echo_request_uri' means return the request uri in the response body
  1472. """
  1473. def __init__(self, iterable):
  1474. """
  1475. Args:
  1476. iterable: iterable, a sequence of pairs of (headers, body)
  1477. """
  1478. self._iterable = iterable
  1479. self.follow_redirects = True
  1480. self.request_sequence = list()
  1481. def request(
  1482. self,
  1483. uri,
  1484. method="GET",
  1485. body=None,
  1486. headers=None,
  1487. redirections=1,
  1488. connection_type=None,
  1489. ):
  1490. # Remember the request so after the fact this mock can be examined
  1491. self.request_sequence.append((uri, method, body, headers))
  1492. resp, content = self._iterable.pop(0)
  1493. if isinstance(content, str):
  1494. content = content.encode("utf-8")
  1495. if content == b"echo_request_headers":
  1496. content = headers
  1497. elif content == b"echo_request_headers_as_json":
  1498. content = json.dumps(headers)
  1499. elif content == b"echo_request_body":
  1500. if hasattr(body, "read"):
  1501. content = body.read()
  1502. else:
  1503. content = body
  1504. elif content == b"echo_request_uri":
  1505. content = uri
  1506. if isinstance(content, str):
  1507. content = content.encode("utf-8")
  1508. return httplib2.Response(resp), content
  1509. def set_user_agent(http, user_agent):
  1510. """Set the user-agent on every request.
  1511. Args:
  1512. http - An instance of httplib2.Http
  1513. or something that acts like it.
  1514. user_agent: string, the value for the user-agent header.
  1515. Returns:
  1516. A modified instance of http that was passed in.
  1517. Example:
  1518. h = httplib2.Http()
  1519. h = set_user_agent(h, "my-app-name/6.0")
  1520. Most of the time the user-agent will be set doing auth, this is for the rare
  1521. cases where you are accessing an unauthenticated endpoint.
  1522. """
  1523. request_orig = http.request
  1524. # The closure that will replace 'httplib2.Http.request'.
  1525. def new_request(
  1526. uri,
  1527. method="GET",
  1528. body=None,
  1529. headers=None,
  1530. redirections=httplib2.DEFAULT_MAX_REDIRECTS,
  1531. connection_type=None,
  1532. ):
  1533. """Modify the request headers to add the user-agent."""
  1534. if headers is None:
  1535. headers = {}
  1536. if "user-agent" in headers:
  1537. headers["user-agent"] = user_agent + " " + headers["user-agent"]
  1538. else:
  1539. headers["user-agent"] = user_agent
  1540. resp, content = request_orig(
  1541. uri,
  1542. method=method,
  1543. body=body,
  1544. headers=headers,
  1545. redirections=redirections,
  1546. connection_type=connection_type,
  1547. )
  1548. return resp, content
  1549. http.request = new_request
  1550. return http
  1551. def tunnel_patch(http):
  1552. """Tunnel PATCH requests over POST.
  1553. Args:
  1554. http - An instance of httplib2.Http
  1555. or something that acts like it.
  1556. Returns:
  1557. A modified instance of http that was passed in.
  1558. Example:
  1559. h = httplib2.Http()
  1560. h = tunnel_patch(h, "my-app-name/6.0")
  1561. Useful if you are running on a platform that doesn't support PATCH.
  1562. Apply this last if you are using OAuth 1.0, as changing the method
  1563. will result in a different signature.
  1564. """
  1565. request_orig = http.request
  1566. # The closure that will replace 'httplib2.Http.request'.
  1567. def new_request(
  1568. uri,
  1569. method="GET",
  1570. body=None,
  1571. headers=None,
  1572. redirections=httplib2.DEFAULT_MAX_REDIRECTS,
  1573. connection_type=None,
  1574. ):
  1575. """Modify the request headers to add the user-agent."""
  1576. if headers is None:
  1577. headers = {}
  1578. if method == "PATCH":
  1579. if "oauth_token" in headers.get("authorization", ""):
  1580. LOGGER.warning(
  1581. "OAuth 1.0 request made with Credentials after tunnel_patch."
  1582. )
  1583. headers["x-http-method-override"] = "PATCH"
  1584. method = "POST"
  1585. resp, content = request_orig(
  1586. uri,
  1587. method=method,
  1588. body=body,
  1589. headers=headers,
  1590. redirections=redirections,
  1591. connection_type=connection_type,
  1592. )
  1593. return resp, content
  1594. http.request = new_request
  1595. return http
  1596. def build_http():
  1597. """Builds httplib2.Http object
  1598. Returns:
  1599. A httplib2.Http object, which is used to make http requests, and which has timeout set by default.
  1600. To override default timeout call
  1601. socket.setdefaulttimeout(timeout_in_sec)
  1602. before interacting with this method.
  1603. """
  1604. if socket.getdefaulttimeout() is not None:
  1605. http_timeout = socket.getdefaulttimeout()
  1606. else:
  1607. http_timeout = DEFAULT_HTTP_TIMEOUT_SEC
  1608. http = httplib2.Http(timeout=http_timeout)
  1609. # 308's are used by several Google APIs (Drive, YouTube)
  1610. # for Resumable Uploads rather than Permanent Redirects.
  1611. # This asks httplib2 to exclude 308s from the status codes
  1612. # it treats as redirects
  1613. try:
  1614. http.redirect_codes = http.redirect_codes - {308}
  1615. except AttributeError:
  1616. # Apache Beam tests depend on this library and cannot
  1617. # currently upgrade their httplib2 version
  1618. # http.redirect_codes does not exist in previous versions
  1619. # of httplib2, so pass
  1620. pass
  1621. return http