Skip to content

Commit 0a52546

Browse files
authored
fix: revise and rename is_etag_in_json(data) (#483)
* fix: handle cases where data is a dict in is_etag_in_json(data) * address comments and accept proposed changes * revise is_etag_in_json to check dict data * revise docstring wording * revise docstrings * rename conditional predicate to is_etag_in_data
1 parent 92251a5 commit 0a52546

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

docs/retry_timeout.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ explicit policy in your code.
133133
134134
from google.api_core.retry import Retry
135135
from google.cloud.storage.retry import ConditionalRetryPolicy
136-
from google.cloud.storage.retry import is_etag_in_json
136+
from google.cloud.storage.retry import is_etag_in_data
137137
138138
def is_retryable(exc):
139139
... # as above
140140
141141
my_retry_policy = Retry(predicate=is_retryable)
142142
my_cond_policy = ConditionalRetryPolicy(
143-
my_retry_policy, conditional_predicate=is_etag_in_json)
143+
my_retry_policy, conditional_predicate=is_etag_in_data)
144144
bucket = client.get_bucket(BUCKET_NAME, retry=my_cond_policy)
145145
146146

google/cloud/storage/retry.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
from google.api_core import retry
1919
from google.auth import exceptions as auth_exceptions
2020

21-
import json
22-
2321

2422
# ConnectionError is a built-in exception only in Python3 and not in Python2.
2523
try:
@@ -120,20 +118,21 @@ def is_metageneration_specified(query_params):
120118
return if_metageneration_match
121119

122120

121+
def is_etag_in_data(data):
122+
"""Return True if an etag is contained in the request body.
123+
124+
:type data: dict or None
125+
:param data: A dict representing the request JSON body. If not passed, returns False.
126+
"""
127+
return data is not None and "etag" in data
128+
129+
123130
def is_etag_in_json(data):
124-
"""Return True if an etag is contained in the JSON body.
125-
126-
Indended for use on calls with relatively short JSON payloads."""
127-
try:
128-
content = json.loads(data)
129-
if content.get("etag"):
130-
return True
131-
# Though this method should only be called when a JSON body is expected,
132-
# the retry policy should be robust to unexpected payloads.
133-
# In Python 3 a JSONDecodeError is possible, but it is a subclass of ValueError.
134-
except (ValueError, TypeError):
135-
pass
136-
return False
131+
"""
132+
``is_etag_in_json`` is supported for backwards-compatibility reasons only;
133+
please use ``is_etag_in_data`` instead.
134+
"""
135+
return is_etag_in_data(data)
137136

138137

139138
DEFAULT_RETRY_IF_GENERATION_SPECIFIED = ConditionalRetryPolicy(

tests/unit/test_retry.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -166,30 +166,24 @@ def test_w_if_metageneration_match(self):
166166
self.assertTrue(self._call_fut(query_params))
167167

168168

169-
class Test_is_etag_in_json(unittest.TestCase):
169+
class Test_is_etag_in_data(unittest.TestCase):
170170
def _call_fut(self, data):
171171
from google.cloud.storage import retry
172172

173-
return retry.is_etag_in_json(data)
173+
return retry.is_etag_in_data(data)
174174

175-
@staticmethod
176-
def _make_json_data(**kw):
177-
import json
178-
179-
return json.dumps(kw)
180-
181-
def test_w_empty(self):
182-
data = self._make_json_data()
175+
def test_w_none(self):
176+
data = None
183177

184178
self.assertFalse(self._call_fut(data))
185179

186180
def test_w_etag_in_data(self):
187-
data = self._make_json_data(etag="123")
181+
data = {"etag": "123"}
188182

189183
self.assertTrue(self._call_fut(data))
190184

191185
def test_w_empty_data(self):
192-
data = ""
186+
data = {}
193187

194188
self.assertFalse(self._call_fut(data))
195189

0 commit comments

Comments
 (0)