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