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

Commit 4cdf0c4

Browse files
committed
refactor: use 'Client._patch_resource' in 'Blob.make_private'
Indirecly, via 'ACL.save'. Also, adds 'timeout' and 'retry' support to 'Blob.make_private'. Toward #38.
1 parent 83201db commit 4cdf0c4

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

google/cloud/storage/blob.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,16 +3013,40 @@ def make_public(
30133013
self.acl.all().grant_read()
30143014
self.acl.save(client=client, timeout=timeout, retry=retry)
30153015

3016-
def make_private(self, client=None):
3016+
def make_private(
3017+
self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY,
3018+
):
30173019
"""Update blob's ACL, revoking read access for anonymous users.
30183020
30193021
:type client: :class:`~google.cloud.storage.client.Client` or
30203022
``NoneType``
30213023
:param client: (Optional) The client to use. If not passed, falls back
30223024
to the ``client`` stored on the blob's bucket.
3025+
3026+
:type timeout: float or tuple
3027+
:param timeout: (Optional) The amount of time, in seconds, to wait
3028+
for the server response. The timeout applies to each underlying
3029+
request.
3030+
3031+
Can also be passed as a tuple (connect_timeout, read_timeout).
3032+
See :meth:`requests.Session.request` documentation for details.
3033+
3034+
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
3035+
:param retry: (Optional) How to retry the RPC. A None value will disable retries.
3036+
A google.api_core.retry.Retry value will enable retries, and the object will
3037+
define retriable response codes and errors and configure backoff and timeout options.
3038+
3039+
A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
3040+
activates it only if certain conditions are met. This class exists to provide safe defaults
3041+
for RPC calls that are not technically safe to retry normally (due to potential data
3042+
duplication or other side-effects) but become safe to retry if a condition such as
3043+
if_metageneration_match is set.
3044+
3045+
See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
3046+
information on retry types and how to configure them.
30233047
"""
30243048
self.acl.all().revoke_read()
3025-
self.acl.save(client=client)
3049+
self.acl.save(client=client, timeout=timeout, retry=retry)
30263050

30273051
def compose(
30283052
self,

tests/unit/test_blob.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3472,23 +3472,55 @@ def test_make_public_w_timeout_w_retry(self):
34723472
retry=retry,
34733473
)
34743474

3475-
def test_make_private(self):
3476-
BLOB_NAME = "blob-name"
3475+
def test_make_private_w_defaults(self):
3476+
blob_name = "blob-name"
34773477
no_permissions = []
3478-
after = ({"status": http_client.OK}, {"acl": no_permissions})
3479-
connection = _Connection(after)
3480-
client = _Client(connection)
3478+
api_response = {"acl": no_permissions}
3479+
client = mock.Mock(spec=["_patch_resource"])
3480+
client._patch_resource.return_value = api_response
34813481
bucket = _Bucket(client=client)
3482-
blob = self._make_one(BLOB_NAME, bucket=bucket)
3482+
blob = self._make_one(blob_name, bucket=bucket)
34833483
blob.acl.loaded = True
3484+
34843485
blob.make_private()
3486+
34853487
self.assertEqual(list(blob.acl), no_permissions)
3486-
kw = connection._requested
3487-
self.assertEqual(len(kw), 1)
3488-
self.assertEqual(kw[0]["method"], "PATCH")
3489-
self.assertEqual(kw[0]["path"], "/b/name/o/%s" % BLOB_NAME)
3490-
self.assertEqual(kw[0]["data"], {"acl": no_permissions})
3491-
self.assertEqual(kw[0]["query_params"], {"projection": "full"})
3488+
3489+
expected_patch_data = {"acl": no_permissions}
3490+
expected_query_params = {"projection": "full"}
3491+
client._patch_resource.assert_called_once_with(
3492+
blob.path,
3493+
expected_patch_data,
3494+
query_params=expected_query_params,
3495+
timeout=self._get_default_timeout(),
3496+
retry=DEFAULT_RETRY,
3497+
)
3498+
3499+
def test_make_private_w_timeout_w_retry(self):
3500+
blob_name = "blob-name"
3501+
no_permissions = []
3502+
api_response = {"acl": no_permissions}
3503+
client = mock.Mock(spec=["_patch_resource"])
3504+
client._patch_resource.return_value = api_response
3505+
bucket = _Bucket(client=client)
3506+
blob = self._make_one(blob_name, bucket=bucket)
3507+
blob.acl.loaded = True
3508+
timeout = 42
3509+
retry = mock.Mock(spec=[])
3510+
3511+
blob.make_private(timeout=timeout, retry=retry)
3512+
3513+
self.assertEqual(list(blob.acl), no_permissions)
3514+
3515+
expected_patch_data = {"acl": no_permissions}
3516+
expected_query_params = {"projection": "full"}
3517+
client._patch_resource.assert_called_once_with(
3518+
blob.path,
3519+
expected_patch_data,
3520+
query_params=expected_query_params,
3521+
timeout=timeout,
3522+
retry=retry,
3523+
)
34923524

34933525
def test_compose_wo_content_type_set(self):
34943526
SOURCE_1 = "source-1"

0 commit comments

Comments
 (0)