123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- def transmit_next_chunk(self, transport):
- """Transmit the next chunk of the resource to be uploaded.
- If the current upload was initiated with ``stream_final=False``,
- this method will dynamically determine if the upload has completed.
- The upload will be considered complete if the stream produces
- fewer than :attr:`chunk_size` bytes when a chunk is read from it.
- In the case of failure, an exception is thrown that preserves the
- failed response:
- .. testsetup:: bad-response
- import io
- import mock
- import requests
- from six.moves import http_client
- from google import resumable_media
- import google.resumable_media.requests.upload as upload_mod
- transport = mock.Mock(spec=[u'request'])
- fake_response = requests.Response()
- fake_response.status_code = int(http_client.BAD_REQUEST)
- transport.request.return_value = fake_response
- upload_url = u'http://test.invalid'
- upload = upload_mod.ResumableUpload(
- upload_url, resumable_media.UPLOAD_CHUNK_SIZE)
- # Fake that the upload has been initiate()-d
- data = b'data is here'
- upload._stream = io.BytesIO(data)
- upload._total_bytes = len(data)
- upload._resumable_url = u'http://test.invalid?upload_id=nope'
- .. doctest:: bad-response
- :options: +NORMALIZE_WHITESPACE
- >>> error = None
- >>> try:
- ... upload.transmit_next_chunk(transport)
- ... except resumable_media.InvalidResponse as caught_exc:
- ... error = caught_exc
- ...
- >>> error
- InvalidResponse('Request failed with status code', 400,
- 'Expected one of', <HTTPStatus.OK: 200>, 308)
- >>> error.response
- <Response [400]>
- Args:
- transport (~requests.Session): A ``requests`` object which can
- make authenticated requests.
- Returns:
- ~requests.Response: The HTTP response returned by ``transport``.
- Raises:
- ~google.resumable_media.common.InvalidResponse: If the status
- code is not 200 or 308.
- """
- method, url, payload, headers = self._prepare_request()
- result = _helpers.http_request(
- transport, method, url, data=payload, headers=headers,
- retry_strategy=self._retry_strategy)
- self._process_response(result, len(payload))
- return result
|