Skip to content

Commit

Permalink
Adding optional stream argument to DownloadBase.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Jul 31, 2017
1 parent d296d03 commit 0c2bcdc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
14 changes: 11 additions & 3 deletions google/resumable_media/_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class DownloadBase(object):
Args:
media_url (str): The URL containing the media to be downloaded.
stream (IO[bytes]): A write-able stream (i.e. file-like object) that
the downloaded resource can be written to.
start (int): The first byte in a range to be downloaded.
end (int): The last byte in a range to be downloaded.
headers (Optional[Mapping[str, str]]): Extra headers that should
Expand All @@ -48,8 +50,10 @@ class DownloadBase(object):
end (Optional[int]): The last byte in a range to be downloaded.
"""

def __init__(self, media_url, start=None, end=None, headers=None):
def __init__(self, media_url, stream=None,
start=None, end=None, headers=None):
self.media_url = media_url
self._stream = stream
self.start = start
self.end = end
if headers is None:
Expand Down Expand Up @@ -109,6 +113,8 @@ class Download(DownloadBase):
Args:
media_url (str): The URL containing the media to be downloaded.
stream (IO[bytes]): A write-able stream (i.e. file-like object) that
the downloaded resource can be written to.
start (int): The first byte in a range to be downloaded. If not
provided, but ``end`` is provided, will download from the
beginning to ``end`` of the media.
Expand Down Expand Up @@ -166,6 +172,9 @@ def _process_response(self, response):
def consume(self, transport):
"""Consume the resource to be downloaded.
If a ``stream`` is attached to this download, then the downloaded
resource will be written to the stream.
Args:
transport (object): An object which can make authenticated
requests.
Expand Down Expand Up @@ -211,9 +220,8 @@ def __init__(self, media_url, chunk_size, stream,
u'On a chunked download the starting '
u'value cannot be negative.')
super(ChunkedDownload, self).__init__(
media_url, start=start, end=end, headers=headers)
media_url, stream=stream, start=start, end=end, headers=headers)
self.chunk_size = chunk_size
self._stream = stream
self._bytes_downloaded = 0
self._total_bytes = None
self._invalid = False
Expand Down
2 changes: 2 additions & 0 deletions google/resumable_media/requests/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Download(_helpers.RequestsMixin, _download.Download):
Args:
media_url (str): The URL containing the media to be downloaded.
stream (IO[bytes]): A write-able stream (i.e. file-like object) that
the downloaded resource can be written to.
start (int): The first byte in a range to be downloaded. If not
provided, but ``end`` is provided, will download from the
beginning to ``end`` of the media.
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test__download.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TestDownloadBase(object):
def test_constructor_defaults(self):
download = _download.DownloadBase(EXAMPLE_URL)
assert download.media_url == EXAMPLE_URL
assert download._stream is None
assert download.start is None
assert download.end is None
assert download._headers == {}
Expand All @@ -43,8 +44,10 @@ def test_constructor_explicit(self):
end = 10001
headers = {u'foof': u'barf'}
download = _download.DownloadBase(
EXAMPLE_URL, start=start, end=end, headers=headers)
EXAMPLE_URL, stream=mock.sentinel.stream,
start=start, end=end, headers=headers)
assert download.media_url == EXAMPLE_URL
assert download._stream is mock.sentinel.stream
assert download.start == start
assert download.end == end
assert download._headers is headers
Expand Down

0 comments on commit 0c2bcdc

Please sign in to comment.