Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow recover to check the status of upload regardless of state #343

Merged
merged 5 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions google/resumable_media/_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,14 +794,8 @@ def _prepare_recover_request(self):
The headers **do not** incorporate the ``_headers`` on the
current instance.

Raises:
ValueError: If the current upload is not in an invalid state.

.. _sans-I/O: https://sans-io.readthedocs.io/
"""
if not self.invalid:
raise ValueError("Upload is not in invalid state, no need to recover.")

headers = {_helpers.CONTENT_RANGE_HEADER: "bytes */*"}
return _PUT, self.resumable_url, None, headers

Expand Down
11 changes: 6 additions & 5 deletions google/resumable_media/requests/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,15 @@ def retriable_request():
)

def recover(self, transport):
"""Recover from a failure.

This method should be used when a :class:`ResumableUpload` is in an
:attr:`~ResumableUpload.invalid` state due to a request failure.
"""Recover from a failure and check the status of the current upload.

This will verify the progress with the server and make sure the
current upload is in a valid state before :meth:`transmit_next_chunk`
can be used again.
can be used again. See https://cloud.google.com/storage/docs/performing-resumable-uploads#status-check
for more information.

This method can be used when a :class:`ResumableUpload` is in an
:attr:`~ResumableUpload.invalid` state due to a request failure.

Args:
transport (~requests.Session): A ``requests`` object which can
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/test__upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,13 @@ def test__prepare_recover_request_not_invalid(self):
upload = _upload.ResumableUpload(RESUMABLE_URL, ONE_MB)
assert not upload.invalid

with pytest.raises(ValueError):
upload._prepare_recover_request()
method, url, payload, headers = upload._prepare_recover_request()
assert method == "PUT"
assert url == upload.resumable_url
assert payload is None
assert headers == {"content-range": "bytes */*"}
# Make sure headers are untouched.
assert upload._headers == {}

def test__prepare_recover_request(self):
upload = _upload.ResumableUpload(RESUMABLE_URL, ONE_MB)
Expand Down