Joe Gregorio | 88f699f | 2012-06-07 13:36:06 -0400 | [diff] [blame] | 1 | # Copyright (C) 2012 Google Inc. |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -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. |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 14 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 15 | """Classes to encapsulate a single HTTP request. |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 16 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 17 | The classes implement a command pattern, with every |
| 18 | object supporting an execute() method that does the |
| 19 | actuall HTTP request. |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 20 | """ |
| 21 | |
| 22 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 23 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 24 | import StringIO |
Ali Afshar | 6f11ea1 | 2012-02-07 10:32:14 -0500 | [diff] [blame] | 25 | import base64 |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 26 | import copy |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 27 | import gzip |
Joe Gregorio | c672246 | 2010-12-20 14:29:28 -0500 | [diff] [blame] | 28 | import httplib2 |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 29 | import mimeparse |
| 30 | import mimetypes |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 31 | import os |
| 32 | import urllib |
| 33 | import urlparse |
| 34 | import uuid |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 35 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 36 | from email.generator import Generator |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 37 | from email.mime.multipart import MIMEMultipart |
| 38 | from email.mime.nonmultipart import MIMENonMultipart |
| 39 | from email.parser import FeedParser |
| 40 | from errors import BatchError |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 41 | from errors import HttpError |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 42 | from errors import ResumableUploadError |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 43 | from errors import UnexpectedBodyError |
| 44 | from errors import UnexpectedMethodError |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 45 | from model import JsonModel |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 46 | from oauth2client.anyjson import simplejson |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 47 | |
| 48 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 49 | DEFAULT_CHUNK_SIZE = 512*1024 |
| 50 | |
| 51 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 52 | class MediaUploadProgress(object): |
| 53 | """Status of a resumable upload.""" |
| 54 | |
| 55 | def __init__(self, resumable_progress, total_size): |
| 56 | """Constructor. |
| 57 | |
| 58 | Args: |
| 59 | resumable_progress: int, bytes sent so far. |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 60 | total_size: int, total bytes in complete upload, or None if the total |
| 61 | upload size isn't known ahead of time. |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 62 | """ |
| 63 | self.resumable_progress = resumable_progress |
| 64 | self.total_size = total_size |
| 65 | |
| 66 | def progress(self): |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 67 | """Percent of upload completed, as a float. |
| 68 | |
| 69 | Returns: |
| 70 | the percentage complete as a float, returning 0.0 if the total size of |
| 71 | the upload is unknown. |
| 72 | """ |
| 73 | if self.total_size is not None: |
| 74 | return float(self.resumable_progress) / float(self.total_size) |
| 75 | else: |
| 76 | return 0.0 |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 77 | |
| 78 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame^] | 79 | class MediaDownloadProgress(object): |
| 80 | """Status of a resumable download.""" |
| 81 | |
| 82 | def __init__(self, resumable_progress, total_size): |
| 83 | """Constructor. |
| 84 | |
| 85 | Args: |
| 86 | resumable_progress: int, bytes received so far. |
| 87 | total_size: int, total bytes in complete download. |
| 88 | """ |
| 89 | self.resumable_progress = resumable_progress |
| 90 | self.total_size = total_size |
| 91 | |
| 92 | def progress(self): |
| 93 | """Percent of download completed, as a float. |
| 94 | |
| 95 | Returns: |
| 96 | the percentage complete as a float, returning 0.0 if the total size of |
| 97 | the download is unknown. |
| 98 | """ |
| 99 | if self.total_size is not None: |
| 100 | return float(self.resumable_progress) / float(self.total_size) |
| 101 | else: |
| 102 | return 0.0 |
| 103 | |
| 104 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 105 | class MediaUpload(object): |
| 106 | """Describes a media object to upload. |
| 107 | |
| 108 | Base class that defines the interface of MediaUpload subclasses. |
Joe Gregorio | 88f699f | 2012-06-07 13:36:06 -0400 | [diff] [blame] | 109 | |
| 110 | Note that subclasses of MediaUpload may allow you to control the chunksize |
| 111 | when upload a media object. It is important to keep the size of the chunk as |
| 112 | large as possible to keep the upload efficient. Other factors may influence |
| 113 | the size of the chunk you use, particularly if you are working in an |
| 114 | environment where individual HTTP requests may have a hardcoded time limit, |
| 115 | such as under certain classes of requests under Google App Engine. |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 116 | """ |
| 117 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 118 | def chunksize(self): |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 119 | """Chunk size for resumable uploads. |
| 120 | |
| 121 | Returns: |
| 122 | Chunk size in bytes. |
| 123 | """ |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 124 | raise NotImplementedError() |
| 125 | |
| 126 | def mimetype(self): |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 127 | """Mime type of the body. |
| 128 | |
| 129 | Returns: |
| 130 | Mime type. |
| 131 | """ |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 132 | return 'application/octet-stream' |
| 133 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 134 | def size(self): |
| 135 | """Size of upload. |
| 136 | |
| 137 | Returns: |
| 138 | Size of the body, or None of the size is unknown. |
| 139 | """ |
| 140 | return None |
| 141 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 142 | def resumable(self): |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 143 | """Whether this upload is resumable. |
| 144 | |
| 145 | Returns: |
| 146 | True if resumable upload or False. |
| 147 | """ |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 148 | return False |
| 149 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 150 | def getbytes(self, begin, end): |
| 151 | """Get bytes from the media. |
| 152 | |
| 153 | Args: |
| 154 | begin: int, offset from beginning of file. |
| 155 | length: int, number of bytes to read, starting at begin. |
| 156 | |
| 157 | Returns: |
| 158 | A string of bytes read. May be shorter than length if EOF was reached |
| 159 | first. |
| 160 | """ |
| 161 | raise NotImplementedError() |
| 162 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 163 | def _to_json(self, strip=None): |
| 164 | """Utility function for creating a JSON representation of a MediaUpload. |
| 165 | |
| 166 | Args: |
| 167 | strip: array, An array of names of members to not include in the JSON. |
| 168 | |
| 169 | Returns: |
| 170 | string, a JSON representation of this instance, suitable to pass to |
| 171 | from_json(). |
| 172 | """ |
| 173 | t = type(self) |
| 174 | d = copy.copy(self.__dict__) |
| 175 | if strip is not None: |
| 176 | for member in strip: |
| 177 | del d[member] |
| 178 | d['_class'] = t.__name__ |
| 179 | d['_module'] = t.__module__ |
| 180 | return simplejson.dumps(d) |
| 181 | |
| 182 | def to_json(self): |
| 183 | """Create a JSON representation of an instance of MediaUpload. |
| 184 | |
| 185 | Returns: |
| 186 | string, a JSON representation of this instance, suitable to pass to |
| 187 | from_json(). |
| 188 | """ |
| 189 | return self._to_json() |
| 190 | |
| 191 | @classmethod |
| 192 | def new_from_json(cls, s): |
| 193 | """Utility class method to instantiate a MediaUpload subclass from a JSON |
| 194 | representation produced by to_json(). |
| 195 | |
| 196 | Args: |
| 197 | s: string, JSON from to_json(). |
| 198 | |
| 199 | Returns: |
| 200 | An instance of the subclass of MediaUpload that was serialized with |
| 201 | to_json(). |
| 202 | """ |
| 203 | data = simplejson.loads(s) |
| 204 | # Find and call the right classmethod from_json() to restore the object. |
| 205 | module = data['_module'] |
| 206 | m = __import__(module, fromlist=module.split('.')[:-1]) |
| 207 | kls = getattr(m, data['_class']) |
| 208 | from_json = getattr(kls, 'from_json') |
| 209 | return from_json(s) |
| 210 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 211 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 212 | class MediaFileUpload(MediaUpload): |
| 213 | """A MediaUpload for a file. |
| 214 | |
| 215 | Construct a MediaFileUpload and pass as the media_body parameter of the |
| 216 | method. For example, if we had a service that allowed uploading images: |
| 217 | |
| 218 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 219 | media = MediaFileUpload('smiley.png', mimetype='image/png', |
| 220 | chunksize=1024*1024, resumable=True) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 221 | service.objects().insert( |
| 222 | bucket=buckets['items'][0]['id'], |
| 223 | name='smiley.png', |
| 224 | media_body=media).execute() |
| 225 | """ |
| 226 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 227 | def __init__(self, filename, mimetype=None, chunksize=DEFAULT_CHUNK_SIZE, resumable=False): |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 228 | """Constructor. |
| 229 | |
| 230 | Args: |
| 231 | filename: string, Name of the file. |
| 232 | mimetype: string, Mime-type of the file. If None then a mime-type will be |
| 233 | guessed from the file extension. |
| 234 | chunksize: int, File will be uploaded in chunks of this many bytes. Only |
| 235 | used if resumable=True. |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 236 | resumable: bool, True if this is a resumable upload. False means upload |
| 237 | in a single request. |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 238 | """ |
| 239 | self._filename = filename |
| 240 | self._size = os.path.getsize(filename) |
| 241 | self._fd = None |
| 242 | if mimetype is None: |
| 243 | (mimetype, encoding) = mimetypes.guess_type(filename) |
| 244 | self._mimetype = mimetype |
| 245 | self._chunksize = chunksize |
| 246 | self._resumable = resumable |
| 247 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 248 | def chunksize(self): |
| 249 | """Chunk size for resumable uploads. |
| 250 | |
| 251 | Returns: |
| 252 | Chunk size in bytes. |
| 253 | """ |
| 254 | return self._chunksize |
| 255 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 256 | def mimetype(self): |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 257 | """Mime type of the body. |
| 258 | |
| 259 | Returns: |
| 260 | Mime type. |
| 261 | """ |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 262 | return self._mimetype |
| 263 | |
| 264 | def size(self): |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 265 | """Size of upload. |
| 266 | |
| 267 | Returns: |
| 268 | Size of the body, or None of the size is unknown. |
| 269 | """ |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 270 | return self._size |
| 271 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 272 | def resumable(self): |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 273 | """Whether this upload is resumable. |
| 274 | |
| 275 | Returns: |
| 276 | True if resumable upload or False. |
| 277 | """ |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 278 | return self._resumable |
| 279 | |
| 280 | def getbytes(self, begin, length): |
| 281 | """Get bytes from the media. |
| 282 | |
| 283 | Args: |
| 284 | begin: int, offset from beginning of file. |
| 285 | length: int, number of bytes to read, starting at begin. |
| 286 | |
| 287 | Returns: |
| 288 | A string of bytes read. May be shorted than length if EOF was reached |
| 289 | first. |
| 290 | """ |
| 291 | if self._fd is None: |
| 292 | self._fd = open(self._filename, 'rb') |
| 293 | self._fd.seek(begin) |
| 294 | return self._fd.read(length) |
| 295 | |
| 296 | def to_json(self): |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame^] | 297 | """Creating a JSON representation of an instance of MediaFileUpload. |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 298 | |
| 299 | Returns: |
| 300 | string, a JSON representation of this instance, suitable to pass to |
| 301 | from_json(). |
| 302 | """ |
| 303 | return self._to_json(['_fd']) |
| 304 | |
| 305 | @staticmethod |
| 306 | def from_json(s): |
| 307 | d = simplejson.loads(s) |
| 308 | return MediaFileUpload( |
| 309 | d['_filename'], d['_mimetype'], d['_chunksize'], d['_resumable']) |
| 310 | |
| 311 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 312 | class MediaIoBaseUpload(MediaUpload): |
| 313 | """A MediaUpload for a io.Base objects. |
| 314 | |
| 315 | Note that the Python file object is compatible with io.Base and can be used |
| 316 | with this class also. |
| 317 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 318 | fh = io.BytesIO('...Some data to upload...') |
| 319 | media = MediaIoBaseUpload(fh, mimetype='image/png', |
| 320 | chunksize=1024*1024, resumable=True) |
| 321 | service.objects().insert( |
| 322 | bucket='a_bucket_id', |
| 323 | name='smiley.png', |
| 324 | media_body=media).execute() |
| 325 | """ |
| 326 | |
| 327 | def __init__(self, fh, mimetype, chunksize=DEFAULT_CHUNK_SIZE, |
| 328 | resumable=False): |
| 329 | """Constructor. |
| 330 | |
| 331 | Args: |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 332 | fh: io.Base or file object, The source of the bytes to upload. MUST be |
| 333 | opened in blocking mode, do not use streams opened in non-blocking mode. |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 334 | mimetype: string, Mime-type of the file. If None then a mime-type will be |
| 335 | guessed from the file extension. |
| 336 | chunksize: int, File will be uploaded in chunks of this many bytes. Only |
| 337 | used if resumable=True. |
| 338 | resumable: bool, True if this is a resumable upload. False means upload |
| 339 | in a single request. |
| 340 | """ |
| 341 | self._fh = fh |
| 342 | self._mimetype = mimetype |
| 343 | self._chunksize = chunksize |
| 344 | self._resumable = resumable |
| 345 | self._size = None |
| 346 | try: |
| 347 | if hasattr(self._fh, 'fileno'): |
| 348 | fileno = self._fh.fileno() |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 349 | |
| 350 | # Pipes and such show up as 0 length files. |
| 351 | size = os.fstat(fileno).st_size |
| 352 | if size: |
| 353 | self._size = os.fstat(fileno).st_size |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 354 | except IOError: |
| 355 | pass |
| 356 | |
| 357 | def chunksize(self): |
| 358 | """Chunk size for resumable uploads. |
| 359 | |
| 360 | Returns: |
| 361 | Chunk size in bytes. |
| 362 | """ |
| 363 | return self._chunksize |
| 364 | |
| 365 | def mimetype(self): |
| 366 | """Mime type of the body. |
| 367 | |
| 368 | Returns: |
| 369 | Mime type. |
| 370 | """ |
| 371 | return self._mimetype |
| 372 | |
| 373 | def size(self): |
| 374 | """Size of upload. |
| 375 | |
| 376 | Returns: |
| 377 | Size of the body, or None of the size is unknown. |
| 378 | """ |
| 379 | return self._size |
| 380 | |
| 381 | def resumable(self): |
| 382 | """Whether this upload is resumable. |
| 383 | |
| 384 | Returns: |
| 385 | True if resumable upload or False. |
| 386 | """ |
| 387 | return self._resumable |
| 388 | |
| 389 | def getbytes(self, begin, length): |
| 390 | """Get bytes from the media. |
| 391 | |
| 392 | Args: |
| 393 | begin: int, offset from beginning of file. |
| 394 | length: int, number of bytes to read, starting at begin. |
| 395 | |
| 396 | Returns: |
| 397 | A string of bytes read. May be shorted than length if EOF was reached |
| 398 | first. |
| 399 | """ |
| 400 | self._fh.seek(begin) |
| 401 | return self._fh.read(length) |
| 402 | |
| 403 | def to_json(self): |
| 404 | """This upload type is not serializable.""" |
| 405 | raise NotImplementedError('MediaIoBaseUpload is not serializable.') |
| 406 | |
| 407 | |
Ali Afshar | 6f11ea1 | 2012-02-07 10:32:14 -0500 | [diff] [blame] | 408 | class MediaInMemoryUpload(MediaUpload): |
| 409 | """MediaUpload for a chunk of bytes. |
| 410 | |
| 411 | Construct a MediaFileUpload and pass as the media_body parameter of the |
| 412 | method. For example, if we had a service that allowed plain text: |
| 413 | """ |
| 414 | |
| 415 | def __init__(self, body, mimetype='application/octet-stream', |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 416 | chunksize=DEFAULT_CHUNK_SIZE, resumable=False): |
Ali Afshar | 6f11ea1 | 2012-02-07 10:32:14 -0500 | [diff] [blame] | 417 | """Create a new MediaBytesUpload. |
| 418 | |
| 419 | Args: |
| 420 | body: string, Bytes of body content. |
| 421 | mimetype: string, Mime-type of the file or default of |
| 422 | 'application/octet-stream'. |
| 423 | chunksize: int, File will be uploaded in chunks of this many bytes. Only |
| 424 | used if resumable=True. |
| 425 | resumable: bool, True if this is a resumable upload. False means upload |
| 426 | in a single request. |
| 427 | """ |
| 428 | self._body = body |
| 429 | self._mimetype = mimetype |
| 430 | self._resumable = resumable |
| 431 | self._chunksize = chunksize |
| 432 | |
| 433 | def chunksize(self): |
| 434 | """Chunk size for resumable uploads. |
| 435 | |
| 436 | Returns: |
| 437 | Chunk size in bytes. |
| 438 | """ |
| 439 | return self._chunksize |
| 440 | |
| 441 | def mimetype(self): |
| 442 | """Mime type of the body. |
| 443 | |
| 444 | Returns: |
| 445 | Mime type. |
| 446 | """ |
| 447 | return self._mimetype |
| 448 | |
| 449 | def size(self): |
| 450 | """Size of upload. |
| 451 | |
| 452 | Returns: |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 453 | Size of the body, or None of the size is unknown. |
Ali Afshar | 6f11ea1 | 2012-02-07 10:32:14 -0500 | [diff] [blame] | 454 | """ |
Ali Afshar | 1cb6b67 | 2012-03-12 08:46:14 -0400 | [diff] [blame] | 455 | return len(self._body) |
Ali Afshar | 6f11ea1 | 2012-02-07 10:32:14 -0500 | [diff] [blame] | 456 | |
| 457 | def resumable(self): |
| 458 | """Whether this upload is resumable. |
| 459 | |
| 460 | Returns: |
| 461 | True if resumable upload or False. |
| 462 | """ |
| 463 | return self._resumable |
| 464 | |
| 465 | def getbytes(self, begin, length): |
| 466 | """Get bytes from the media. |
| 467 | |
| 468 | Args: |
| 469 | begin: int, offset from beginning of file. |
| 470 | length: int, number of bytes to read, starting at begin. |
| 471 | |
| 472 | Returns: |
| 473 | A string of bytes read. May be shorter than length if EOF was reached |
| 474 | first. |
| 475 | """ |
| 476 | return self._body[begin:begin + length] |
| 477 | |
| 478 | def to_json(self): |
| 479 | """Create a JSON representation of a MediaInMemoryUpload. |
| 480 | |
| 481 | Returns: |
| 482 | string, a JSON representation of this instance, suitable to pass to |
| 483 | from_json(). |
| 484 | """ |
| 485 | t = type(self) |
| 486 | d = copy.copy(self.__dict__) |
| 487 | del d['_body'] |
| 488 | d['_class'] = t.__name__ |
| 489 | d['_module'] = t.__module__ |
| 490 | d['_b64body'] = base64.b64encode(self._body) |
| 491 | return simplejson.dumps(d) |
| 492 | |
| 493 | @staticmethod |
| 494 | def from_json(s): |
| 495 | d = simplejson.loads(s) |
| 496 | return MediaInMemoryUpload(base64.b64decode(d['_b64body']), |
| 497 | d['_mimetype'], d['_chunksize'], |
| 498 | d['_resumable']) |
| 499 | |
| 500 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame^] | 501 | class MediaIoBaseDownload(object): |
| 502 | """"Download media resources. |
| 503 | |
| 504 | Note that the Python file object is compatible with io.Base and can be used |
| 505 | with this class also. |
| 506 | |
| 507 | |
| 508 | Example: |
| 509 | request = service.objects().get_media( |
| 510 | bucket='a_bucket_id', |
| 511 | name='smiley.png') |
| 512 | |
| 513 | fh = io.FileIO('image.png', mode='wb') |
| 514 | downloader = MediaIoBaseDownload(fh, request, chunksize=1024*1024) |
| 515 | |
| 516 | done = False |
| 517 | while done is False: |
| 518 | status, done = downloader.next_chunk() |
| 519 | if status: |
| 520 | print "Download %d%%." % int(status.progress() * 100) |
| 521 | print "Download Complete!" |
| 522 | """ |
| 523 | |
| 524 | def __init__(self, fh, request, chunksize=DEFAULT_CHUNK_SIZE): |
| 525 | """Constructor. |
| 526 | |
| 527 | Args: |
| 528 | fh: io.Base or file object, The stream in which to write the downloaded |
| 529 | bytes. |
| 530 | request: apiclient.http.HttpRequest, the media request to perform in |
| 531 | chunks. |
| 532 | chunksize: int, File will be downloaded in chunks of this many bytes. |
| 533 | """ |
| 534 | self.fh_ = fh |
| 535 | self.request_ = request |
| 536 | self.uri_ = request.uri |
| 537 | self.chunksize_ = chunksize |
| 538 | self.progress_ = 0 |
| 539 | self.total_size_ = None |
| 540 | self.done_ = False |
| 541 | |
| 542 | def next_chunk(self): |
| 543 | """Get the next chunk of the download. |
| 544 | |
| 545 | Returns: |
| 546 | (status, done): (MediaDownloadStatus, boolean) |
| 547 | The value of 'done' will be True when the media has been fully |
| 548 | downloaded. |
| 549 | |
| 550 | Raises: |
| 551 | apiclient.errors.HttpError if the response was not a 2xx. |
| 552 | httplib2.Error if a transport error has occured. |
| 553 | """ |
| 554 | headers = { |
| 555 | 'range': 'bytes=%d-%d' % ( |
| 556 | self.progress_, self.progress_ + self.chunksize_) |
| 557 | } |
| 558 | http = self.request_.http |
| 559 | http.follow_redirects = False |
| 560 | |
| 561 | resp, content = http.request(self.uri_, headers=headers) |
| 562 | if resp.status in [301, 302, 303, 307, 308] and 'location' in resp: |
| 563 | self.uri_ = resp['location'] |
| 564 | resp, content = http.request(self.uri_, headers=headers) |
| 565 | if resp.status in [200, 206]: |
| 566 | self.progress_ += len(content) |
| 567 | self.fh_.write(content) |
| 568 | |
| 569 | if 'content-range' in resp: |
| 570 | content_range = resp['content-range'] |
| 571 | length = content_range.rsplit('/', 1)[1] |
| 572 | self.total_size_ = int(length) |
| 573 | |
| 574 | if self.progress_ == self.total_size_: |
| 575 | self.done_ = True |
| 576 | return MediaDownloadProgress(self.progress_, self.total_size_), self.done_ |
| 577 | else: |
| 578 | raise HttpError(resp, content, self.uri_) |
| 579 | |
| 580 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 581 | class HttpRequest(object): |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 582 | """Encapsulates a single HTTP request.""" |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 583 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 584 | def __init__(self, http, postproc, uri, |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 585 | method='GET', |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 586 | body=None, |
| 587 | headers=None, |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 588 | methodId=None, |
| 589 | resumable=None): |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 590 | """Constructor for an HttpRequest. |
| 591 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 592 | Args: |
| 593 | http: httplib2.Http, the transport object to use to make a request |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 594 | postproc: callable, called on the HTTP response and content to transform |
| 595 | it into a data object before returning, or raising an exception |
| 596 | on an error. |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 597 | uri: string, the absolute URI to send the request to |
| 598 | method: string, the HTTP method to use |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 599 | body: string, the request body of the HTTP request, |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 600 | headers: dict, the HTTP request headers |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 601 | methodId: string, a unique identifier for the API method being called. |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 602 | resumable: MediaUpload, None if this is not a resumbale request. |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 603 | """ |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 604 | self.uri = uri |
| 605 | self.method = method |
| 606 | self.body = body |
| 607 | self.headers = headers or {} |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 608 | self.methodId = methodId |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 609 | self.http = http |
| 610 | self.postproc = postproc |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 611 | self.resumable = resumable |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 612 | self._in_error_state = False |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 613 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 614 | # Pull the multipart boundary out of the content-type header. |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 615 | major, minor, params = mimeparse.parse_mime_type( |
| 616 | headers.get('content-type', 'application/json')) |
Joe Gregorio | bd512b5 | 2011-12-06 15:39:26 -0500 | [diff] [blame] | 617 | |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 618 | # The size of the non-media part of the request. |
| 619 | self.body_size = len(self.body or '') |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 620 | |
| 621 | # The resumable URI to send chunks to. |
| 622 | self.resumable_uri = None |
| 623 | |
| 624 | # The bytes that have been uploaded. |
| 625 | self.resumable_progress = 0 |
| 626 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 627 | def execute(self, http=None): |
| 628 | """Execute the request. |
| 629 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 630 | Args: |
| 631 | http: httplib2.Http, an http object to be used in place of the |
| 632 | one the HttpRequest request object was constructed with. |
| 633 | |
| 634 | Returns: |
| 635 | A deserialized object model of the response body as determined |
| 636 | by the postproc. |
| 637 | |
| 638 | Raises: |
| 639 | apiclient.errors.HttpError if the response was not a 2xx. |
| 640 | httplib2.Error if a transport error has occured. |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 641 | """ |
| 642 | if http is None: |
| 643 | http = self.http |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 644 | if self.resumable: |
| 645 | body = None |
| 646 | while body is None: |
| 647 | _, body = self.next_chunk(http) |
| 648 | return body |
| 649 | else: |
Joe Gregorio | 884e2b2 | 2012-02-24 09:37:00 -0500 | [diff] [blame] | 650 | if 'content-length' not in self.headers: |
| 651 | self.headers['content-length'] = str(self.body_size) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 652 | resp, content = http.request(self.uri, self.method, |
| 653 | body=self.body, |
| 654 | headers=self.headers) |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 655 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 656 | if resp.status >= 300: |
| 657 | raise HttpError(resp, content, self.uri) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 658 | return self.postproc(resp, content) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 659 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 660 | def next_chunk(self, http=None): |
| 661 | """Execute the next step of a resumable upload. |
| 662 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 663 | Can only be used if the method being executed supports media uploads and |
| 664 | the MediaUpload object passed in was flagged as using resumable upload. |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 665 | |
| 666 | Example: |
| 667 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 668 | media = MediaFileUpload('smiley.png', mimetype='image/png', |
| 669 | chunksize=1000, resumable=True) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 670 | request = service.objects().insert( |
| 671 | bucket=buckets['items'][0]['id'], |
| 672 | name='smiley.png', |
| 673 | media_body=media) |
| 674 | |
| 675 | response = None |
| 676 | while response is None: |
| 677 | status, response = request.next_chunk() |
| 678 | if status: |
| 679 | print "Upload %d%% complete." % int(status.progress() * 100) |
| 680 | |
| 681 | |
| 682 | Returns: |
| 683 | (status, body): (ResumableMediaStatus, object) |
| 684 | The body will be None until the resumable media is fully uploaded. |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 685 | |
| 686 | Raises: |
| 687 | apiclient.errors.HttpError if the response was not a 2xx. |
| 688 | httplib2.Error if a transport error has occured. |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 689 | """ |
| 690 | if http is None: |
| 691 | http = self.http |
| 692 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 693 | if self.resumable.size() is None: |
| 694 | size = '*' |
| 695 | else: |
| 696 | size = str(self.resumable.size()) |
| 697 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 698 | if self.resumable_uri is None: |
| 699 | start_headers = copy.copy(self.headers) |
| 700 | start_headers['X-Upload-Content-Type'] = self.resumable.mimetype() |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 701 | if size != '*': |
| 702 | start_headers['X-Upload-Content-Length'] = size |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 703 | start_headers['content-length'] = str(self.body_size) |
| 704 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 705 | resp, content = http.request(self.uri, self.method, |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 706 | body=self.body, |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 707 | headers=start_headers) |
| 708 | if resp.status == 200 and 'location' in resp: |
| 709 | self.resumable_uri = resp['location'] |
| 710 | else: |
| 711 | raise ResumableUploadError("Failed to retrieve starting URI.") |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 712 | elif self._in_error_state: |
| 713 | # If we are in an error state then query the server for current state of |
| 714 | # the upload by sending an empty PUT and reading the 'range' header in |
| 715 | # the response. |
| 716 | headers = { |
| 717 | 'Content-Range': 'bytes */%s' % size, |
| 718 | 'content-length': '0' |
| 719 | } |
| 720 | resp, content = http.request(self.resumable_uri, 'PUT', |
| 721 | headers=headers) |
| 722 | status, body = self._process_response(resp, content) |
| 723 | if body: |
| 724 | # The upload was complete. |
| 725 | return (status, body) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 726 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 727 | data = self.resumable.getbytes( |
| 728 | self.resumable_progress, self.resumable.chunksize()) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 729 | |
| 730 | # A short read implies that we are at EOF, so finish the upload. |
| 731 | if len(data) < self.resumable.chunksize(): |
| 732 | size = str(self.resumable_progress + len(data)) |
| 733 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 734 | headers = { |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 735 | 'Content-Range': 'bytes %d-%d/%s' % ( |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 736 | self.resumable_progress, self.resumable_progress + len(data) - 1, |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 737 | size) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 738 | } |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 739 | try: |
| 740 | resp, content = http.request(self.resumable_uri, 'PUT', |
| 741 | body=data, |
| 742 | headers=headers) |
| 743 | except: |
| 744 | self._in_error_state = True |
| 745 | raise |
| 746 | |
| 747 | return self._process_response(resp, content) |
| 748 | |
| 749 | def _process_response(self, resp, content): |
| 750 | """Process the response from a single chunk upload. |
| 751 | |
| 752 | Args: |
| 753 | resp: httplib2.Response, the response object. |
| 754 | content: string, the content of the response. |
| 755 | |
| 756 | Returns: |
| 757 | (status, body): (ResumableMediaStatus, object) |
| 758 | The body will be None until the resumable media is fully uploaded. |
| 759 | |
| 760 | Raises: |
| 761 | apiclient.errors.HttpError if the response was not a 2xx or a 308. |
| 762 | """ |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 763 | if resp.status in [200, 201]: |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 764 | self._in_error_state = False |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 765 | return None, self.postproc(resp, content) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 766 | elif resp.status == 308: |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 767 | self._in_error_state = False |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 768 | # A "308 Resume Incomplete" indicates we are not done. |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 769 | self.resumable_progress = int(resp['range'].split('-')[1]) + 1 |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 770 | if 'location' in resp: |
| 771 | self.resumable_uri = resp['location'] |
| 772 | else: |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 773 | self._in_error_state = True |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 774 | raise HttpError(resp, content, self.uri) |
| 775 | |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 776 | return (MediaUploadProgress(self.resumable_progress, self.resumable.size()), |
| 777 | None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 778 | |
| 779 | def to_json(self): |
| 780 | """Returns a JSON representation of the HttpRequest.""" |
| 781 | d = copy.copy(self.__dict__) |
| 782 | if d['resumable'] is not None: |
| 783 | d['resumable'] = self.resumable.to_json() |
| 784 | del d['http'] |
| 785 | del d['postproc'] |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 786 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 787 | return simplejson.dumps(d) |
| 788 | |
| 789 | @staticmethod |
| 790 | def from_json(s, http, postproc): |
| 791 | """Returns an HttpRequest populated with info from a JSON object.""" |
| 792 | d = simplejson.loads(s) |
| 793 | if d['resumable'] is not None: |
| 794 | d['resumable'] = MediaUpload.new_from_json(d['resumable']) |
| 795 | return HttpRequest( |
| 796 | http, |
| 797 | postproc, |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 798 | uri=d['uri'], |
| 799 | method=d['method'], |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 800 | body=d['body'], |
| 801 | headers=d['headers'], |
| 802 | methodId=d['methodId'], |
| 803 | resumable=d['resumable']) |
| 804 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 805 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 806 | class BatchHttpRequest(object): |
| 807 | """Batches multiple HttpRequest objects into a single HTTP request.""" |
| 808 | |
| 809 | def __init__(self, callback=None, batch_uri=None): |
| 810 | """Constructor for a BatchHttpRequest. |
| 811 | |
| 812 | Args: |
| 813 | callback: callable, A callback to be called for each response, of the |
| 814 | form callback(id, response). The first parameter is the request id, and |
| 815 | the second is the deserialized response object. |
| 816 | batch_uri: string, URI to send batch requests to. |
| 817 | """ |
| 818 | if batch_uri is None: |
| 819 | batch_uri = 'https://www.googleapis.com/batch' |
| 820 | self._batch_uri = batch_uri |
| 821 | |
| 822 | # Global callback to be called for each individual response in the batch. |
| 823 | self._callback = callback |
| 824 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 825 | # A map from id to request. |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 826 | self._requests = {} |
| 827 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 828 | # A map from id to callback. |
| 829 | self._callbacks = {} |
| 830 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 831 | # List of request ids, in the order in which they were added. |
| 832 | self._order = [] |
| 833 | |
| 834 | # The last auto generated id. |
| 835 | self._last_auto_id = 0 |
| 836 | |
| 837 | # Unique ID on which to base the Content-ID headers. |
| 838 | self._base_id = None |
| 839 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 840 | # A map from request id to (headers, content) response pairs |
| 841 | self._responses = {} |
| 842 | |
| 843 | # A map of id(Credentials) that have been refreshed. |
| 844 | self._refreshed_credentials = {} |
| 845 | |
| 846 | def _refresh_and_apply_credentials(self, request, http): |
| 847 | """Refresh the credentials and apply to the request. |
| 848 | |
| 849 | Args: |
| 850 | request: HttpRequest, the request. |
| 851 | http: httplib2.Http, the global http object for the batch. |
| 852 | """ |
| 853 | # For the credentials to refresh, but only once per refresh_token |
| 854 | # If there is no http per the request then refresh the http passed in |
| 855 | # via execute() |
| 856 | creds = None |
| 857 | if request.http is not None and hasattr(request.http.request, |
| 858 | 'credentials'): |
| 859 | creds = request.http.request.credentials |
| 860 | elif http is not None and hasattr(http.request, 'credentials'): |
| 861 | creds = http.request.credentials |
| 862 | if creds is not None: |
| 863 | if id(creds) not in self._refreshed_credentials: |
| 864 | creds.refresh(http) |
| 865 | self._refreshed_credentials[id(creds)] = 1 |
| 866 | |
| 867 | # Only apply the credentials if we are using the http object passed in, |
| 868 | # otherwise apply() will get called during _serialize_request(). |
| 869 | if request.http is None or not hasattr(request.http.request, |
| 870 | 'credentials'): |
| 871 | creds.apply(request.headers) |
| 872 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 873 | def _id_to_header(self, id_): |
| 874 | """Convert an id to a Content-ID header value. |
| 875 | |
| 876 | Args: |
| 877 | id_: string, identifier of individual request. |
| 878 | |
| 879 | Returns: |
| 880 | A Content-ID header with the id_ encoded into it. A UUID is prepended to |
| 881 | the value because Content-ID headers are supposed to be universally |
| 882 | unique. |
| 883 | """ |
| 884 | if self._base_id is None: |
| 885 | self._base_id = uuid.uuid4() |
| 886 | |
| 887 | return '<%s+%s>' % (self._base_id, urllib.quote(id_)) |
| 888 | |
| 889 | def _header_to_id(self, header): |
| 890 | """Convert a Content-ID header value to an id. |
| 891 | |
| 892 | Presumes the Content-ID header conforms to the format that _id_to_header() |
| 893 | returns. |
| 894 | |
| 895 | Args: |
| 896 | header: string, Content-ID header value. |
| 897 | |
| 898 | Returns: |
| 899 | The extracted id value. |
| 900 | |
| 901 | Raises: |
| 902 | BatchError if the header is not in the expected format. |
| 903 | """ |
| 904 | if header[0] != '<' or header[-1] != '>': |
| 905 | raise BatchError("Invalid value for Content-ID: %s" % header) |
| 906 | if '+' not in header: |
| 907 | raise BatchError("Invalid value for Content-ID: %s" % header) |
| 908 | base, id_ = header[1:-1].rsplit('+', 1) |
| 909 | |
| 910 | return urllib.unquote(id_) |
| 911 | |
| 912 | def _serialize_request(self, request): |
| 913 | """Convert an HttpRequest object into a string. |
| 914 | |
| 915 | Args: |
| 916 | request: HttpRequest, the request to serialize. |
| 917 | |
| 918 | Returns: |
| 919 | The request as a string in application/http format. |
| 920 | """ |
| 921 | # Construct status line |
| 922 | parsed = urlparse.urlparse(request.uri) |
| 923 | request_line = urlparse.urlunparse( |
| 924 | (None, None, parsed.path, parsed.params, parsed.query, None) |
| 925 | ) |
| 926 | status_line = request.method + ' ' + request_line + ' HTTP/1.1\n' |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 927 | major, minor = request.headers.get('content-type', 'application/json').split('/') |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 928 | msg = MIMENonMultipart(major, minor) |
| 929 | headers = request.headers.copy() |
| 930 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 931 | if request.http is not None and hasattr(request.http.request, |
| 932 | 'credentials'): |
| 933 | request.http.request.credentials.apply(headers) |
| 934 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 935 | # MIMENonMultipart adds its own Content-Type header. |
| 936 | if 'content-type' in headers: |
| 937 | del headers['content-type'] |
| 938 | |
| 939 | for key, value in headers.iteritems(): |
| 940 | msg[key] = value |
| 941 | msg['Host'] = parsed.netloc |
| 942 | msg.set_unixfrom(None) |
| 943 | |
| 944 | if request.body is not None: |
| 945 | msg.set_payload(request.body) |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 946 | msg['content-length'] = str(len(request.body)) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 947 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 948 | # Serialize the mime message. |
| 949 | fp = StringIO.StringIO() |
| 950 | # maxheaderlen=0 means don't line wrap headers. |
| 951 | g = Generator(fp, maxheaderlen=0) |
| 952 | g.flatten(msg, unixfrom=False) |
| 953 | body = fp.getvalue() |
| 954 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 955 | # Strip off the \n\n that the MIME lib tacks onto the end of the payload. |
| 956 | if request.body is None: |
| 957 | body = body[:-2] |
| 958 | |
Joe Gregorio | dd81382 | 2012-01-25 10:32:47 -0500 | [diff] [blame] | 959 | return status_line.encode('utf-8') + body |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 960 | |
| 961 | def _deserialize_response(self, payload): |
| 962 | """Convert string into httplib2 response and content. |
| 963 | |
| 964 | Args: |
| 965 | payload: string, headers and body as a string. |
| 966 | |
| 967 | Returns: |
| 968 | A pair (resp, content) like would be returned from httplib2.request. |
| 969 | """ |
| 970 | # Strip off the status line |
| 971 | status_line, payload = payload.split('\n', 1) |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 972 | protocol, status, reason = status_line.split(' ', 2) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 973 | |
| 974 | # Parse the rest of the response |
| 975 | parser = FeedParser() |
| 976 | parser.feed(payload) |
| 977 | msg = parser.close() |
| 978 | msg['status'] = status |
| 979 | |
| 980 | # Create httplib2.Response from the parsed headers. |
| 981 | resp = httplib2.Response(msg) |
| 982 | resp.reason = reason |
| 983 | resp.version = int(protocol.split('/', 1)[1].replace('.', '')) |
| 984 | |
| 985 | content = payload.split('\r\n\r\n', 1)[1] |
| 986 | |
| 987 | return resp, content |
| 988 | |
| 989 | def _new_id(self): |
| 990 | """Create a new id. |
| 991 | |
| 992 | Auto incrementing number that avoids conflicts with ids already used. |
| 993 | |
| 994 | Returns: |
| 995 | string, a new unique id. |
| 996 | """ |
| 997 | self._last_auto_id += 1 |
| 998 | while str(self._last_auto_id) in self._requests: |
| 999 | self._last_auto_id += 1 |
| 1000 | return str(self._last_auto_id) |
| 1001 | |
| 1002 | def add(self, request, callback=None, request_id=None): |
| 1003 | """Add a new request. |
| 1004 | |
| 1005 | Every callback added will be paired with a unique id, the request_id. That |
| 1006 | unique id will be passed back to the callback when the response comes back |
| 1007 | from the server. The default behavior is to have the library generate it's |
| 1008 | own unique id. If the caller passes in a request_id then they must ensure |
| 1009 | uniqueness for each request_id, and if they are not an exception is |
| 1010 | raised. Callers should either supply all request_ids or nevery supply a |
| 1011 | request id, to avoid such an error. |
| 1012 | |
| 1013 | Args: |
| 1014 | request: HttpRequest, Request to add to the batch. |
| 1015 | callback: callable, A callback to be called for this response, of the |
| 1016 | form callback(id, response). The first parameter is the request id, and |
| 1017 | the second is the deserialized response object. |
| 1018 | request_id: string, A unique id for the request. The id will be passed to |
| 1019 | the callback with the response. |
| 1020 | |
| 1021 | Returns: |
| 1022 | None |
| 1023 | |
| 1024 | Raises: |
| 1025 | BatchError if a resumable request is added to a batch. |
| 1026 | KeyError is the request_id is not unique. |
| 1027 | """ |
| 1028 | if request_id is None: |
| 1029 | request_id = self._new_id() |
| 1030 | if request.resumable is not None: |
| 1031 | raise BatchError("Resumable requests cannot be used in a batch request.") |
| 1032 | if request_id in self._requests: |
| 1033 | raise KeyError("A request with this ID already exists: %s" % request_id) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1034 | self._requests[request_id] = request |
| 1035 | self._callbacks[request_id] = callback |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1036 | self._order.append(request_id) |
| 1037 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1038 | def _execute(self, http, order, requests): |
| 1039 | """Serialize batch request, send to server, process response. |
| 1040 | |
| 1041 | Args: |
| 1042 | http: httplib2.Http, an http object to be used to make the request with. |
| 1043 | order: list, list of request ids in the order they were added to the |
| 1044 | batch. |
| 1045 | request: list, list of request objects to send. |
| 1046 | |
| 1047 | Raises: |
| 1048 | httplib2.Error if a transport error has occured. |
| 1049 | apiclient.errors.BatchError if the response is the wrong format. |
| 1050 | """ |
| 1051 | message = MIMEMultipart('mixed') |
| 1052 | # Message should not write out it's own headers. |
| 1053 | setattr(message, '_write_headers', lambda self: None) |
| 1054 | |
| 1055 | # Add all the individual requests. |
| 1056 | for request_id in order: |
| 1057 | request = requests[request_id] |
| 1058 | |
| 1059 | msg = MIMENonMultipart('application', 'http') |
| 1060 | msg['Content-Transfer-Encoding'] = 'binary' |
| 1061 | msg['Content-ID'] = self._id_to_header(request_id) |
| 1062 | |
| 1063 | body = self._serialize_request(request) |
| 1064 | msg.set_payload(body) |
| 1065 | message.attach(msg) |
| 1066 | |
| 1067 | body = message.as_string() |
| 1068 | |
| 1069 | headers = {} |
| 1070 | headers['content-type'] = ('multipart/mixed; ' |
| 1071 | 'boundary="%s"') % message.get_boundary() |
| 1072 | |
| 1073 | resp, content = http.request(self._batch_uri, 'POST', body=body, |
| 1074 | headers=headers) |
| 1075 | |
| 1076 | if resp.status >= 300: |
| 1077 | raise HttpError(resp, content, self._batch_uri) |
| 1078 | |
| 1079 | # Now break out the individual responses and store each one. |
| 1080 | boundary, _ = content.split(None, 1) |
| 1081 | |
| 1082 | # Prepend with a content-type header so FeedParser can handle it. |
| 1083 | header = 'content-type: %s\r\n\r\n' % resp['content-type'] |
| 1084 | for_parser = header + content |
| 1085 | |
| 1086 | parser = FeedParser() |
| 1087 | parser.feed(for_parser) |
| 1088 | mime_response = parser.close() |
| 1089 | |
| 1090 | if not mime_response.is_multipart(): |
| 1091 | raise BatchError("Response not in multipart/mixed format.", resp, |
| 1092 | content) |
| 1093 | |
| 1094 | for part in mime_response.get_payload(): |
| 1095 | request_id = self._header_to_id(part['Content-ID']) |
| 1096 | headers, content = self._deserialize_response(part.get_payload()) |
| 1097 | self._responses[request_id] = (headers, content) |
| 1098 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1099 | def execute(self, http=None): |
| 1100 | """Execute all the requests as a single batched HTTP request. |
| 1101 | |
| 1102 | Args: |
| 1103 | http: httplib2.Http, an http object to be used in place of the one the |
| 1104 | HttpRequest request object was constructed with. If one isn't supplied |
| 1105 | then use a http object from the requests in this batch. |
| 1106 | |
| 1107 | Returns: |
| 1108 | None |
| 1109 | |
| 1110 | Raises: |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1111 | httplib2.Error if a transport error has occured. |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 1112 | apiclient.errors.BatchError if the response is the wrong format. |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1113 | """ |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1114 | |
| 1115 | # If http is not supplied use the first valid one given in the requests. |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1116 | if http is None: |
| 1117 | for request_id in self._order: |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1118 | request = self._requests[request_id] |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1119 | if request is not None: |
| 1120 | http = request.http |
| 1121 | break |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1122 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1123 | if http is None: |
| 1124 | raise ValueError("Missing a valid http object.") |
| 1125 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1126 | self._execute(http, self._order, self._requests) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1127 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1128 | # Loop over all the requests and check for 401s. For each 401 request the |
| 1129 | # credentials should be refreshed and then sent again in a separate batch. |
| 1130 | redo_requests = {} |
| 1131 | redo_order = [] |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1132 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1133 | for request_id in self._order: |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1134 | headers, content = self._responses[request_id] |
| 1135 | if headers['status'] == '401': |
| 1136 | redo_order.append(request_id) |
| 1137 | request = self._requests[request_id] |
| 1138 | self._refresh_and_apply_credentials(request, http) |
| 1139 | redo_requests[request_id] = request |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1140 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1141 | if redo_requests: |
| 1142 | self._execute(http, redo_order, redo_requests) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1143 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1144 | # Now process all callbacks that are erroring, and raise an exception for |
| 1145 | # ones that return a non-2xx response? Or add extra parameter to callback |
| 1146 | # that contains an HttpError? |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1147 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1148 | for request_id in self._order: |
| 1149 | headers, content = self._responses[request_id] |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1150 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1151 | request = self._requests[request_id] |
| 1152 | callback = self._callbacks[request_id] |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1153 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1154 | response = None |
| 1155 | exception = None |
| 1156 | try: |
| 1157 | r = httplib2.Response(headers) |
| 1158 | response = request.postproc(r, content) |
| 1159 | except HttpError, e: |
| 1160 | exception = e |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1161 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1162 | if callback is not None: |
| 1163 | callback(request_id, response, exception) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1164 | if self._callback is not None: |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1165 | self._callback(request_id, response, exception) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1166 | |
| 1167 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1168 | class HttpRequestMock(object): |
| 1169 | """Mock of HttpRequest. |
| 1170 | |
| 1171 | Do not construct directly, instead use RequestMockBuilder. |
| 1172 | """ |
| 1173 | |
| 1174 | def __init__(self, resp, content, postproc): |
| 1175 | """Constructor for HttpRequestMock |
| 1176 | |
| 1177 | Args: |
| 1178 | resp: httplib2.Response, the response to emulate coming from the request |
| 1179 | content: string, the response body |
| 1180 | postproc: callable, the post processing function usually supplied by |
| 1181 | the model class. See model.JsonModel.response() as an example. |
| 1182 | """ |
| 1183 | self.resp = resp |
| 1184 | self.content = content |
| 1185 | self.postproc = postproc |
| 1186 | if resp is None: |
Joe Gregorio | c672246 | 2010-12-20 14:29:28 -0500 | [diff] [blame] | 1187 | self.resp = httplib2.Response({'status': 200, 'reason': 'OK'}) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1188 | if 'reason' in self.resp: |
| 1189 | self.resp.reason = self.resp['reason'] |
| 1190 | |
| 1191 | def execute(self, http=None): |
| 1192 | """Execute the request. |
| 1193 | |
| 1194 | Same behavior as HttpRequest.execute(), but the response is |
| 1195 | mocked and not really from an HTTP request/response. |
| 1196 | """ |
| 1197 | return self.postproc(self.resp, self.content) |
| 1198 | |
| 1199 | |
| 1200 | class RequestMockBuilder(object): |
| 1201 | """A simple mock of HttpRequest |
| 1202 | |
| 1203 | Pass in a dictionary to the constructor that maps request methodIds to |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 1204 | tuples of (httplib2.Response, content, opt_expected_body) that should be |
| 1205 | returned when that method is called. None may also be passed in for the |
| 1206 | httplib2.Response, in which case a 200 OK response will be generated. |
| 1207 | If an opt_expected_body (str or dict) is provided, it will be compared to |
| 1208 | the body and UnexpectedBodyError will be raised on inequality. |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1209 | |
| 1210 | Example: |
| 1211 | response = '{"data": {"id": "tag:google.c...' |
| 1212 | requestBuilder = RequestMockBuilder( |
| 1213 | { |
Joe Gregorio | c4fc095 | 2011-11-09 12:21:11 -0500 | [diff] [blame] | 1214 | 'plus.activities.get': (None, response), |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1215 | } |
| 1216 | ) |
Joe Gregorio | c4fc095 | 2011-11-09 12:21:11 -0500 | [diff] [blame] | 1217 | apiclient.discovery.build("plus", "v1", requestBuilder=requestBuilder) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1218 | |
| 1219 | Methods that you do not supply a response for will return a |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1220 | 200 OK with an empty string as the response content or raise an excpetion |
| 1221 | if check_unexpected is set to True. The methodId is taken from the rpcName |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 1222 | in the discovery document. |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1223 | |
| 1224 | For more details see the project wiki. |
| 1225 | """ |
| 1226 | |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 1227 | def __init__(self, responses, check_unexpected=False): |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1228 | """Constructor for RequestMockBuilder |
| 1229 | |
| 1230 | The constructed object should be a callable object |
| 1231 | that can replace the class HttpResponse. |
| 1232 | |
| 1233 | responses - A dictionary that maps methodIds into tuples |
| 1234 | of (httplib2.Response, content). The methodId |
| 1235 | comes from the 'rpcName' field in the discovery |
| 1236 | document. |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 1237 | check_unexpected - A boolean setting whether or not UnexpectedMethodError |
| 1238 | should be raised on unsupplied method. |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1239 | """ |
| 1240 | self.responses = responses |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 1241 | self.check_unexpected = check_unexpected |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1242 | |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 1243 | def __call__(self, http, postproc, uri, method='GET', body=None, |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1244 | headers=None, methodId=None, resumable=None): |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1245 | """Implements the callable interface that discovery.build() expects |
| 1246 | of requestBuilder, which is to build an object compatible with |
| 1247 | HttpRequest.execute(). See that method for the description of the |
| 1248 | parameters and the expected response. |
| 1249 | """ |
| 1250 | if methodId in self.responses: |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 1251 | response = self.responses[methodId] |
| 1252 | resp, content = response[:2] |
| 1253 | if len(response) > 2: |
| 1254 | # Test the body against the supplied expected_body. |
| 1255 | expected_body = response[2] |
| 1256 | if bool(expected_body) != bool(body): |
| 1257 | # Not expecting a body and provided one |
| 1258 | # or expecting a body and not provided one. |
| 1259 | raise UnexpectedBodyError(expected_body, body) |
| 1260 | if isinstance(expected_body, str): |
| 1261 | expected_body = simplejson.loads(expected_body) |
| 1262 | body = simplejson.loads(body) |
| 1263 | if body != expected_body: |
| 1264 | raise UnexpectedBodyError(expected_body, body) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1265 | return HttpRequestMock(resp, content, postproc) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 1266 | elif self.check_unexpected: |
| 1267 | raise UnexpectedMethodError(methodId) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1268 | else: |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 1269 | model = JsonModel(False) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 1270 | return HttpRequestMock(None, '{}', model.response) |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 1271 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 1272 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 1273 | class HttpMock(object): |
| 1274 | """Mock of httplib2.Http""" |
| 1275 | |
Joe Gregorio | ec34365 | 2011-02-16 16:52:51 -0500 | [diff] [blame] | 1276 | def __init__(self, filename, headers=None): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 1277 | """ |
| 1278 | Args: |
| 1279 | filename: string, absolute filename to read response from |
| 1280 | headers: dict, header to return with response |
| 1281 | """ |
Joe Gregorio | ec34365 | 2011-02-16 16:52:51 -0500 | [diff] [blame] | 1282 | if headers is None: |
| 1283 | headers = {'status': '200 OK'} |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 1284 | f = file(filename, 'r') |
| 1285 | self.data = f.read() |
| 1286 | f.close() |
| 1287 | self.headers = headers |
| 1288 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 1289 | def request(self, uri, |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 1290 | method='GET', |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 1291 | body=None, |
| 1292 | headers=None, |
| 1293 | redirections=1, |
| 1294 | connection_type=None): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 1295 | return httplib2.Response(self.headers), self.data |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1296 | |
| 1297 | |
| 1298 | class HttpMockSequence(object): |
| 1299 | """Mock of httplib2.Http |
| 1300 | |
| 1301 | Mocks a sequence of calls to request returning different responses for each |
| 1302 | call. Create an instance initialized with the desired response headers |
| 1303 | and content and then use as if an httplib2.Http instance. |
| 1304 | |
| 1305 | http = HttpMockSequence([ |
| 1306 | ({'status': '401'}, ''), |
| 1307 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 1308 | ({'status': '200'}, 'echo_request_headers'), |
| 1309 | ]) |
| 1310 | resp, content = http.request("http://examples.com") |
| 1311 | |
| 1312 | There are special values you can pass in for content to trigger |
| 1313 | behavours that are helpful in testing. |
| 1314 | |
| 1315 | 'echo_request_headers' means return the request headers in the response body |
Joe Gregorio | e9e236f | 2011-03-21 22:23:14 -0400 | [diff] [blame] | 1316 | 'echo_request_headers_as_json' means return the request headers in |
| 1317 | the response body |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1318 | 'echo_request_body' means return the request body in the response body |
Joe Gregorio | 0bc7091 | 2011-05-24 15:30:49 -0400 | [diff] [blame] | 1319 | 'echo_request_uri' means return the request uri in the response body |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1320 | """ |
| 1321 | |
| 1322 | def __init__(self, iterable): |
| 1323 | """ |
| 1324 | Args: |
| 1325 | iterable: iterable, a sequence of pairs of (headers, body) |
| 1326 | """ |
| 1327 | self._iterable = iterable |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame^] | 1328 | self.follow_redirects = True |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1329 | |
| 1330 | def request(self, uri, |
| 1331 | method='GET', |
| 1332 | body=None, |
| 1333 | headers=None, |
| 1334 | redirections=1, |
| 1335 | connection_type=None): |
| 1336 | resp, content = self._iterable.pop(0) |
| 1337 | if content == 'echo_request_headers': |
| 1338 | content = headers |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 1339 | elif content == 'echo_request_headers_as_json': |
| 1340 | content = simplejson.dumps(headers) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1341 | elif content == 'echo_request_body': |
| 1342 | content = body |
Joe Gregorio | 0bc7091 | 2011-05-24 15:30:49 -0400 | [diff] [blame] | 1343 | elif content == 'echo_request_uri': |
| 1344 | content = uri |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1345 | return httplib2.Response(resp), content |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 1346 | |
| 1347 | |
| 1348 | def set_user_agent(http, user_agent): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 1349 | """Set the user-agent on every request. |
| 1350 | |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 1351 | Args: |
| 1352 | http - An instance of httplib2.Http |
| 1353 | or something that acts like it. |
| 1354 | user_agent: string, the value for the user-agent header. |
| 1355 | |
| 1356 | Returns: |
| 1357 | A modified instance of http that was passed in. |
| 1358 | |
| 1359 | Example: |
| 1360 | |
| 1361 | h = httplib2.Http() |
| 1362 | h = set_user_agent(h, "my-app-name/6.0") |
| 1363 | |
| 1364 | Most of the time the user-agent will be set doing auth, this is for the rare |
| 1365 | cases where you are accessing an unauthenticated endpoint. |
| 1366 | """ |
| 1367 | request_orig = http.request |
| 1368 | |
| 1369 | # The closure that will replace 'httplib2.Http.request'. |
| 1370 | def new_request(uri, method='GET', body=None, headers=None, |
| 1371 | redirections=httplib2.DEFAULT_MAX_REDIRECTS, |
| 1372 | connection_type=None): |
| 1373 | """Modify the request headers to add the user-agent.""" |
| 1374 | if headers is None: |
| 1375 | headers = {} |
| 1376 | if 'user-agent' in headers: |
| 1377 | headers['user-agent'] = user_agent + ' ' + headers['user-agent'] |
| 1378 | else: |
| 1379 | headers['user-agent'] = user_agent |
| 1380 | resp, content = request_orig(uri, method, body, headers, |
| 1381 | redirections, connection_type) |
| 1382 | return resp, content |
| 1383 | |
| 1384 | http.request = new_request |
| 1385 | return http |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 1386 | |
| 1387 | |
| 1388 | def tunnel_patch(http): |
| 1389 | """Tunnel PATCH requests over POST. |
| 1390 | Args: |
| 1391 | http - An instance of httplib2.Http |
| 1392 | or something that acts like it. |
| 1393 | |
| 1394 | Returns: |
| 1395 | A modified instance of http that was passed in. |
| 1396 | |
| 1397 | Example: |
| 1398 | |
| 1399 | h = httplib2.Http() |
| 1400 | h = tunnel_patch(h, "my-app-name/6.0") |
| 1401 | |
| 1402 | Useful if you are running on a platform that doesn't support PATCH. |
| 1403 | Apply this last if you are using OAuth 1.0, as changing the method |
| 1404 | will result in a different signature. |
| 1405 | """ |
| 1406 | request_orig = http.request |
| 1407 | |
| 1408 | # The closure that will replace 'httplib2.Http.request'. |
| 1409 | def new_request(uri, method='GET', body=None, headers=None, |
| 1410 | redirections=httplib2.DEFAULT_MAX_REDIRECTS, |
| 1411 | connection_type=None): |
| 1412 | """Modify the request headers to add the user-agent.""" |
| 1413 | if headers is None: |
| 1414 | headers = {} |
| 1415 | if method == 'PATCH': |
Joe Gregorio | 06d852b | 2011-03-25 15:03:10 -0400 | [diff] [blame] | 1416 | if 'oauth_token' in headers.get('authorization', ''): |
Joe Gregorio | e9e236f | 2011-03-21 22:23:14 -0400 | [diff] [blame] | 1417 | logging.warning( |
Joe Gregorio | 06d852b | 2011-03-25 15:03:10 -0400 | [diff] [blame] | 1418 | 'OAuth 1.0 request made with Credentials after tunnel_patch.') |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 1419 | headers['x-http-method-override'] = "PATCH" |
| 1420 | method = 'POST' |
| 1421 | resp, content = request_orig(uri, method, body, headers, |
| 1422 | redirections, connection_type) |
| 1423 | return resp, content |
| 1424 | |
| 1425 | http.request = new_request |
| 1426 | return http |