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