Skip to content

Commit ef764ac

Browse files
authored
refactor: add / use 'Client._post_resource' method (#443)
Toward #38.
1 parent 94fd0ca commit ef764ac

File tree

8 files changed

+1400
-1236
lines changed

8 files changed

+1400
-1236
lines changed

β€Žgoogle/cloud/storage/blob.pyβ€Ž

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,14 +3164,13 @@ def compose(
31643164
"sourceObjects": source_objects,
31653165
"destination": self._properties.copy(),
31663166
}
3167-
api_response = client._connection.api_request(
3168-
method="POST",
3169-
path=self.path + "/compose",
3167+
api_response = client._post_resource(
3168+
"{}/compose".format(self.path),
3169+
request,
31703170
query_params=query_params,
3171-
data=request,
3172-
_target_object=self,
31733171
timeout=timeout,
31743172
retry=retry,
3173+
_target_object=self,
31753174
)
31763175
self._set_properties(api_response)
31773176

@@ -3315,15 +3314,15 @@ def rewrite(
33153314
if_source_metageneration_not_match=if_source_metageneration_not_match,
33163315
)
33173316

3318-
api_response = client._connection.api_request(
3319-
method="POST",
3320-
path=source.path + "/rewriteTo" + self.path,
3317+
path = "{}/rewriteTo{}".format(source.path, self.path)
3318+
api_response = client._post_resource(
3319+
path,
3320+
self._properties,
33213321
query_params=query_params,
3322-
data=self._properties,
33233322
headers=headers,
3324-
_target_object=self,
33253323
timeout=timeout,
33263324
retry=retry,
3325+
_target_object=self,
33273326
)
33283327
rewritten = int(api_response["totalBytesRewritten"])
33293328
size = int(api_response["objectSize"])

β€Žgoogle/cloud/storage/bucket.pyβ€Ž

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,13 +1986,13 @@ def copy_blob(
19861986

19871987
new_blob = Blob(bucket=destination_bucket, name=new_name)
19881988
api_path = blob.path + "/copyTo" + new_blob.path
1989-
copy_result = client._connection.api_request(
1990-
method="POST",
1991-
path=api_path,
1989+
copy_result = client._post_resource(
1990+
api_path,
1991+
None,
19921992
query_params=query_params,
1993-
_target_object=new_blob,
19941993
timeout=timeout,
19951994
retry=retry,
1995+
_target_object=new_blob,
19961996
)
19971997

19981998
if not preserve_acl:
@@ -2006,7 +2006,6 @@ def rename_blob(
20062006
blob,
20072007
new_name,
20082008
client=None,
2009-
timeout=_DEFAULT_TIMEOUT,
20102009
if_generation_match=None,
20112010
if_generation_not_match=None,
20122011
if_metageneration_match=None,
@@ -2015,6 +2014,7 @@ def rename_blob(
20152014
if_source_generation_not_match=None,
20162015
if_source_metageneration_match=None,
20172016
if_source_metageneration_not_match=None,
2017+
timeout=_DEFAULT_TIMEOUT,
20182018
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
20192019
):
20202020
"""Rename the given blob using copy and delete operations.
@@ -2044,14 +2044,6 @@ def rename_blob(
20442044
:param client: (Optional) The client to use. If not passed, falls back
20452045
to the ``client`` stored on the current bucket.
20462046
2047-
:type timeout: float or tuple
2048-
:param timeout: (Optional) The amount of time, in seconds, to wait
2049-
for the server response. The timeout applies to each individual
2050-
request.
2051-
2052-
Can also be passed as a tuple (connect_timeout, read_timeout).
2053-
See :meth:`requests.Session.request` documentation for details.
2054-
20552047
:type if_generation_match: long
20562048
:param if_generation_match: (Optional) Makes the operation
20572049
conditional on whether the destination
@@ -2113,6 +2105,14 @@ def rename_blob(
21132105
does not match the given value.
21142106
Also used in the delete request.
21152107
2108+
:type timeout: float or tuple
2109+
:param timeout: (Optional) The amount of time, in seconds, to wait
2110+
for the server response. The timeout applies to each individual
2111+
request.
2112+
2113+
Can also be passed as a tuple (connect_timeout, read_timeout).
2114+
See :meth:`requests.Session.request` documentation for details.
2115+
21162116
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
21172117
:param retry: (Optional) How to retry the RPC. A None value will disable retries.
21182118
A google.api_core.retry.Retry value will enable retries, and the object will
@@ -3311,13 +3311,13 @@ def lock_retention_policy(
33113311
query_params["userProject"] = self.user_project
33123312

33133313
path = "/b/{}/lockRetentionPolicy".format(self.name)
3314-
api_response = client._connection.api_request(
3315-
method="POST",
3316-
path=path,
3314+
api_response = client._post_resource(
3315+
path,
3316+
None,
33173317
query_params=query_params,
3318-
_target_object=self,
33193318
timeout=timeout,
33203319
retry=retry,
3320+
_target_object=self,
33213321
)
33223322
self._set_properties(api_response)
33233323

β€Žgoogle/cloud/storage/client.pyβ€Ž

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

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

878-
api_response = self._connection.api_request(
879-
method="POST",
880-
path="/b",
949+
api_response = self._post_resource(
950+
"/b",
951+
properties,
881952
query_params=query_params,
882-
data=properties,
883-
_target_object=bucket,
884953
timeout=timeout,
885954
retry=retry,
955+
_target_object=bucket,
886956
)
887957

888958
bucket._set_properties(api_response)
@@ -1278,6 +1348,7 @@ def create_hmac_key(
12781348
project_id=None,
12791349
user_project=None,
12801350
timeout=_DEFAULT_TIMEOUT,
1351+
retry=None,
12811352
):
12821353
"""Create an HMAC key for a service account.
12831354
@@ -1298,6 +1369,20 @@ def create_hmac_key(
12981369
Can also be passed as a tuple (connect_timeout, read_timeout).
12991370
See :meth:`requests.Session.request` documentation for details.
13001371
1372+
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
1373+
:param retry: (Optional) How to retry the RPC. A None value will disable retries.
1374+
A google.api_core.retry.Retry value will enable retries, and the object will
1375+
define retriable response codes and errors and configure backoff and timeout options.
1376+
1377+
A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
1378+
activates it only if certain conditions are met. This class exists to provide safe defaults
1379+
for RPC calls that are not technically safe to retry normally (due to potential data
1380+
duplication or other side-effects) but become safe to retry if a condition such as
1381+
if_metageneration_match is set.
1382+
1383+
See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
1384+
information on retry types and how to configure them.
1385+
13011386
:rtype:
13021387
Tuple[:class:`~google.cloud.storage.hmac_key.HMACKeyMetadata`, str]
13031388
:returns: metadata for the created key, plus the bytes of the key's secret, which is an 40-character base64-encoded string.
@@ -1311,12 +1396,8 @@ def create_hmac_key(
13111396
if user_project is not None:
13121397
qs_params["userProject"] = user_project
13131398

1314-
api_response = self._connection.api_request(
1315-
method="POST",
1316-
path=path,
1317-
query_params=qs_params,
1318-
timeout=timeout,
1319-
retry=None,
1399+
api_response = self._post_resource(
1400+
path, None, query_params=qs_params, timeout=timeout, retry=retry,
13201401
)
13211402
metadata = HMACKeyMetadata(self)
13221403
metadata._properties = api_response["metadata"]

β€Žgoogle/cloud/storage/notification.pyβ€Ž

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def _set_properties(self, response):
233233
self._properties.clear()
234234
self._properties.update(response)
235235

236-
def create(self, client=None, timeout=_DEFAULT_TIMEOUT):
236+
def create(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=None):
237237
"""API wrapper: create the notification.
238238
239239
See:
@@ -251,6 +251,20 @@ def create(self, client=None, timeout=_DEFAULT_TIMEOUT):
251251
252252
Can also be passed as a tuple (connect_timeout, read_timeout).
253253
See :meth:`requests.Session.request` documentation for details.
254+
255+
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
256+
:param retry: (Optional) How to retry the RPC. A None value will disable retries.
257+
A google.api_core.retry.Retry value will enable retries, and the object will
258+
define retriable response codes and errors and configure backoff and timeout options.
259+
260+
A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
261+
activates it only if certain conditions are met. This class exists to provide safe defaults
262+
for RPC calls that are not technically safe to retry normally (due to potential data
263+
duplication or other side-effects) but become safe to retry if a condition such as
264+
if_metageneration_match is set.
265+
266+
See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
267+
information on retry types and how to configure them.
254268
"""
255269
if self.notification_id is not None:
256270
raise ValueError(
@@ -266,13 +280,8 @@ def create(self, client=None, timeout=_DEFAULT_TIMEOUT):
266280
path = "/b/{}/notificationConfigs".format(self.bucket.name)
267281
properties = self._properties.copy()
268282
properties["topic"] = _TOPIC_REF_FMT.format(self.topic_project, self.topic_name)
269-
self._properties = client._connection.api_request(
270-
method="POST",
271-
path=path,
272-
query_params=query_params,
273-
data=properties,
274-
timeout=timeout,
275-
retry=None,
283+
self._properties = client._post_resource(
284+
path, properties, query_params=query_params, timeout=timeout, retry=retry,
276285
)
277286

278287
def exists(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):

0 commit comments

Comments
 (0)