Skip to content

Commit

Permalink
refactor: add / use 'Client._delete_resource' method (googleapis#442)
Browse files Browse the repository at this point in the history
Also, forward 'retry' through when deleting blobs during 'Bucket.delete'.

Toward googleapis#38.
  • Loading branch information
tseaver authored and cojenco committed Oct 13, 2021
1 parent ac168b3 commit d5880e0
Show file tree
Hide file tree
Showing 8 changed files with 621 additions and 402 deletions.
39 changes: 21 additions & 18 deletions google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,9 +1466,9 @@ def delete(
self,
force=False,
client=None,
timeout=_DEFAULT_TIMEOUT,
if_metageneration_match=None,
if_metageneration_not_match=None,
timeout=_DEFAULT_TIMEOUT,
retry=DEFAULT_RETRY,
):
"""Delete this bucket.
Expand Down Expand Up @@ -1496,13 +1496,6 @@ def delete(
:param client: (Optional) The client to use. If not passed, falls back
to the ``client`` stored on the current bucket.
:type timeout: float or tuple
:param timeout: (Optional) The amount of time, in seconds, to wait
for the server response on each request.
Can also be passed as a tuple (connect_timeout, read_timeout).
See :meth:`requests.Session.request` documentation for details.
:type if_metageneration_match: long
:param if_metageneration_match: (Optional) Make the operation conditional on whether the
blob's current metageneration matches the given value.
Expand All @@ -1511,6 +1504,13 @@ def delete(
:param if_metageneration_not_match: (Optional) Make the operation conditional on whether the
blob's current metageneration does not match the given value.
:type timeout: float or tuple
:param timeout: (Optional) The amount of time, in seconds, to wait
for the server response on each request.
Can also be passed as a tuple (connect_timeout, read_timeout).
See :meth:`requests.Session.request` documentation for details.
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
:param retry: (Optional) How to retry the RPC. A None value will disable retries.
A google.api_core.retry.Retry value will enable retries, and the object will
Expand Down Expand Up @@ -1545,6 +1545,7 @@ def delete(
max_results=self._MAX_OBJECTS_FOR_ITERATION + 1,
client=client,
timeout=timeout,
retry=retry,
)
)
if len(blobs) > self._MAX_OBJECTS_FOR_ITERATION:
Expand All @@ -1558,19 +1559,22 @@ def delete(

# Ignore 404 errors on delete.
self.delete_blobs(
blobs, on_error=lambda blob: None, client=client, timeout=timeout
blobs,
on_error=lambda blob: None,
client=client,
timeout=timeout,
retry=retry,
)

# We intentionally pass `_target_object=None` since a DELETE
# request has no response value (whether in a standard request or
# in a batch request).
client._connection.api_request(
method="DELETE",
path=self.path,
client._delete_resource(
self.path,
query_params=query_params,
_target_object=None,
timeout=timeout,
retry=retry,
_target_object=None,
)

def delete_blob(
Expand Down Expand Up @@ -1677,13 +1681,12 @@ def delete_blob(
# We intentionally pass `_target_object=None` since a DELETE
# request has no response value (whether in a standard request or
# in a batch request).
client._connection.api_request(
method="DELETE",
path=blob.path,
client._delete_resource(
blob.path,
query_params=query_params,
_target_object=None,
timeout=timeout,
retry=retry,
_target_object=None,
)

def delete_blobs(
Expand Down Expand Up @@ -1802,11 +1805,11 @@ def delete_blobs(
self.delete_blob(
blob_name,
client=client,
timeout=timeout,
if_generation_match=next(if_generation_match, None),
if_generation_not_match=next(if_generation_not_match, None),
if_metageneration_match=next(if_metageneration_match, None),
if_metageneration_not_match=next(if_metageneration_not_match, None),
timeout=timeout,
retry=retry,
)
except NotFound:
Expand Down
66 changes: 66 additions & 0 deletions google/cloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,72 @@ def _put_resource(
_target_object=_target_object,
)

def _delete_resource(
self,
path,
query_params=None,
headers=None,
timeout=_DEFAULT_TIMEOUT,
retry=DEFAULT_RETRY,
_target_object=None,
):
"""Helper for bucket / blob methods making API 'DELETE' calls.
Args:
path str:
The path of the resource to delete.
query_params Optional[dict]:
HTTP query parameters to be passed
headers Optional[dict]:
HTTP headers to be passed
timeout (Optional[Union[float, Tuple[float, float]]]):
The amount of time, in seconds, to wait for the server response.
Can also be passed as a tuple (connect_timeout, read_timeout).
See :meth:`requests.Session.request` documentation for details.
retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
How to retry the RPC. A None value will disable retries.
A google.api_core.retry.Retry value will enable retries, and the object will
define retriable response codes and errors and configure backoff and timeout options.
A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
activates it only if certain conditions are met. This class exists to provide safe defaults
for RPC calls that are not technically safe to retry normally (due to potential data
duplication or other side-effects) but become safe to retry if a condition such as
if_metageneration_match is set.
See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
information on retry types and how to configure them.
_target_object (Union[ \
:class:`~google.cloud.storage.bucket.Bucket`, \
:class:`~google.cloud.storage.bucket.blob`, \
]):
Object to which future data is to be applied -- only relevant
in the context of a batch.
Returns:
dict
The JSON resource fetched
Raises:
google.cloud.exceptions.NotFound
If the bucket is not found.
"""
return self._connection.api_request(
method="DELETE",
path=path,
query_params=query_params,
headers=headers,
timeout=timeout,
retry=retry,
_target_object=_target_object,
)

def get_bucket(
self,
bucket_or_name,
Expand Down
8 changes: 2 additions & 6 deletions google/cloud/storage/hmac_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,6 @@ def delete(self, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):
if self.user_project is not None:
qs_params["userProject"] = self.user_project

self._client._connection.api_request(
method="DELETE",
path=self.path,
query_params=qs_params,
timeout=timeout,
retry=retry,
self._client._delete_resource(
self.path, query_params=qs_params, timeout=timeout, retry=retry,
)
8 changes: 2 additions & 6 deletions google/cloud/storage/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,8 @@ def delete(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):
if self.bucket.user_project is not None:
query_params["userProject"] = self.bucket.user_project

client._connection.api_request(
method="DELETE",
path=self.path,
query_params=query_params,
timeout=timeout,
retry=retry,
client._delete_resource(
self.path, query_params=query_params, timeout=timeout, retry=retry,
)


Expand Down

0 comments on commit d5880e0

Please sign in to comment.