Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 94fd0ca

Browse files
authored
refactor: add / use 'Client._delete_resource' method (#442)
Also, forward 'retry' through when deleting blobs during 'Bucket.delete'. Toward #38.
1 parent 3a8a920 commit 94fd0ca

File tree

8 files changed

+621
-402
lines changed

8 files changed

+621
-402
lines changed

google/cloud/storage/bucket.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,9 +1466,9 @@ def delete(
14661466
self,
14671467
force=False,
14681468
client=None,
1469-
timeout=_DEFAULT_TIMEOUT,
14701469
if_metageneration_match=None,
14711470
if_metageneration_not_match=None,
1471+
timeout=_DEFAULT_TIMEOUT,
14721472
retry=DEFAULT_RETRY,
14731473
):
14741474
"""Delete this bucket.
@@ -1496,13 +1496,6 @@ def delete(
14961496
:param client: (Optional) The client to use. If not passed, falls back
14971497
to the ``client`` stored on the current bucket.
14981498
1499-
:type timeout: float or tuple
1500-
:param timeout: (Optional) The amount of time, in seconds, to wait
1501-
for the server response on each request.
1502-
1503-
Can also be passed as a tuple (connect_timeout, read_timeout).
1504-
See :meth:`requests.Session.request` documentation for details.
1505-
15061499
:type if_metageneration_match: long
15071500
:param if_metageneration_match: (Optional) Make the operation conditional on whether the
15081501
blob's current metageneration matches the given value.
@@ -1511,6 +1504,13 @@ def delete(
15111504
:param if_metageneration_not_match: (Optional) Make the operation conditional on whether the
15121505
blob's current metageneration does not match the given value.
15131506
1507+
:type timeout: float or tuple
1508+
:param timeout: (Optional) The amount of time, in seconds, to wait
1509+
for the server response on each request.
1510+
1511+
Can also be passed as a tuple (connect_timeout, read_timeout).
1512+
See :meth:`requests.Session.request` documentation for details.
1513+
15141514
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
15151515
:param retry: (Optional) How to retry the RPC. A None value will disable retries.
15161516
A google.api_core.retry.Retry value will enable retries, and the object will
@@ -1545,6 +1545,7 @@ def delete(
15451545
max_results=self._MAX_OBJECTS_FOR_ITERATION + 1,
15461546
client=client,
15471547
timeout=timeout,
1548+
retry=retry,
15481549
)
15491550
)
15501551
if len(blobs) > self._MAX_OBJECTS_FOR_ITERATION:
@@ -1558,19 +1559,22 @@ def delete(
15581559

15591560
# Ignore 404 errors on delete.
15601561
self.delete_blobs(
1561-
blobs, on_error=lambda blob: None, client=client, timeout=timeout
1562+
blobs,
1563+
on_error=lambda blob: None,
1564+
client=client,
1565+
timeout=timeout,
1566+
retry=retry,
15621567
)
15631568

15641569
# We intentionally pass `_target_object=None` since a DELETE
15651570
# request has no response value (whether in a standard request or
15661571
# in a batch request).
1567-
client._connection.api_request(
1568-
method="DELETE",
1569-
path=self.path,
1572+
client._delete_resource(
1573+
self.path,
15701574
query_params=query_params,
1571-
_target_object=None,
15721575
timeout=timeout,
15731576
retry=retry,
1577+
_target_object=None,
15741578
)
15751579

15761580
def delete_blob(
@@ -1677,13 +1681,12 @@ def delete_blob(
16771681
# We intentionally pass `_target_object=None` since a DELETE
16781682
# request has no response value (whether in a standard request or
16791683
# in a batch request).
1680-
client._connection.api_request(
1681-
method="DELETE",
1682-
path=blob.path,
1684+
client._delete_resource(
1685+
blob.path,
16831686
query_params=query_params,
1684-
_target_object=None,
16851687
timeout=timeout,
16861688
retry=retry,
1689+
_target_object=None,
16871690
)
16881691

16891692
def delete_blobs(
@@ -1802,11 +1805,11 @@ def delete_blobs(
18021805
self.delete_blob(
18031806
blob_name,
18041807
client=client,
1805-
timeout=timeout,
18061808
if_generation_match=next(if_generation_match, None),
18071809
if_generation_not_match=next(if_generation_not_match, None),
18081810
if_metageneration_match=next(if_metageneration_match, None),
18091811
if_metageneration_not_match=next(if_metageneration_not_match, None),
1812+
timeout=timeout,
18101813
retry=retry,
18111814
)
18121815
except NotFound:

google/cloud/storage/client.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,72 @@ def _put_resource(
528528
_target_object=_target_object,
529529
)
530530

531+
def _delete_resource(
532+
self,
533+
path,
534+
query_params=None,
535+
headers=None,
536+
timeout=_DEFAULT_TIMEOUT,
537+
retry=DEFAULT_RETRY,
538+
_target_object=None,
539+
):
540+
"""Helper for bucket / blob methods making API 'DELETE' calls.
541+
542+
Args:
543+
path str:
544+
The path of the resource to delete.
545+
546+
query_params Optional[dict]:
547+
HTTP query parameters to be passed
548+
549+
headers Optional[dict]:
550+
HTTP headers to be passed
551+
552+
timeout (Optional[Union[float, Tuple[float, float]]]):
553+
The amount of time, in seconds, to wait for the server response.
554+
555+
Can also be passed as a tuple (connect_timeout, read_timeout).
556+
See :meth:`requests.Session.request` documentation for details.
557+
558+
retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
559+
How to retry the RPC. A None value will disable retries.
560+
A google.api_core.retry.Retry value will enable retries, and the object will
561+
define retriable response codes and errors and configure backoff and timeout options.
562+
563+
A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
564+
activates it only if certain conditions are met. This class exists to provide safe defaults
565+
for RPC calls that are not technically safe to retry normally (due to potential data
566+
duplication or other side-effects) but become safe to retry if a condition such as
567+
if_metageneration_match is set.
568+
569+
See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
570+
information on retry types and how to configure them.
571+
572+
_target_object (Union[ \
573+
:class:`~google.cloud.storage.bucket.Bucket`, \
574+
:class:`~google.cloud.storage.bucket.blob`, \
575+
]):
576+
Object to which future data is to be applied -- only relevant
577+
in the context of a batch.
578+
579+
Returns:
580+
dict
581+
The JSON resource fetched
582+
583+
Raises:
584+
google.cloud.exceptions.NotFound
585+
If the bucket is not found.
586+
"""
587+
return self._connection.api_request(
588+
method="DELETE",
589+
path=path,
590+
query_params=query_params,
591+
headers=headers,
592+
timeout=timeout,
593+
retry=retry,
594+
_target_object=_target_object,
595+
)
596+
531597
def get_bucket(
532598
self,
533599
bucket_or_name,

google/cloud/storage/hmac_key.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,6 @@ def delete(self, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):
336336
if self.user_project is not None:
337337
qs_params["userProject"] = self.user_project
338338

339-
self._client._connection.api_request(
340-
method="DELETE",
341-
path=self.path,
342-
query_params=qs_params,
343-
timeout=timeout,
344-
retry=retry,
339+
self._client._delete_resource(
340+
self.path, query_params=qs_params, timeout=timeout, retry=retry,
345341
)

google/cloud/storage/notification.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,8 @@ def delete(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):
429429
if self.bucket.user_project is not None:
430430
query_params["userProject"] = self.bucket.user_project
431431

432-
client._connection.api_request(
433-
method="DELETE",
434-
path=self.path,
435-
query_params=query_params,
436-
timeout=timeout,
437-
retry=retry,
432+
client._delete_resource(
433+
self.path, query_params=query_params, timeout=timeout, retry=retry,
438434
)
439435

440436

0 commit comments

Comments
 (0)