Skip to content

Commit

Permalink
Add timeout to _PropertyMixin helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
plamut committed Feb 4, 2020
1 parent f3792a7 commit 317b547
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
30 changes: 27 additions & 3 deletions google/cloud/storage/_helpers.py
Expand Up @@ -21,6 +21,9 @@
from hashlib import md5
import os

from google.cloud.storage.constants import _DEFAULT_TIMEOUT


STORAGE_EMULATOR_ENV_VAR = "STORAGE_EMULATOR_HOST"
"""Environment variable defining host for Storage emulator."""

Expand Down Expand Up @@ -117,7 +120,7 @@ def _query_params(self):
params["userProject"] = self.user_project
return params

def reload(self, client=None):
def reload(self, client=None, timeout=_DEFAULT_TIMEOUT):
"""Reload properties from Cloud Storage.
If :attr:`user_project` is set, bills the API request to that project.
Expand All @@ -126,6 +129,12 @@ def reload(self, client=None):
``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current object.
:type timeout: float or tuple
:param timeout: (optional) 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.
"""
client = self._require_client(client)
query_params = self._query_params
Expand All @@ -138,6 +147,7 @@ def reload(self, client=None):
query_params=query_params,
headers=self._encryption_headers(),
_target_object=self,
timeout=timeout,
)
self._set_properties(api_response)

Expand Down Expand Up @@ -169,7 +179,7 @@ def _set_properties(self, value):
# If the values are reset, the changes must as well.
self._changes = set()

def patch(self, client=None):
def patch(self, client=None, timeout=_DEFAULT_TIMEOUT):
"""Sends all changed properties in a PATCH request.
Updates the ``_properties`` with the response from the backend.
Expand All @@ -180,6 +190,12 @@ def patch(self, client=None):
``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current object.
:type timeout: float or tuple
:param timeout: (optional) 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.
"""
client = self._require_client(client)
query_params = self._query_params
Expand All @@ -195,10 +211,11 @@ def patch(self, client=None):
data=update_properties,
query_params=query_params,
_target_object=self,
timeout=timeout,
)
self._set_properties(api_response)

def update(self, client=None):
def update(self, client=None, timeout=_DEFAULT_TIMEOUT):
"""Sends all properties in a PUT request.
Updates the ``_properties`` with the response from the backend.
Expand All @@ -209,6 +226,12 @@ def update(self, client=None):
``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current object.
:type timeout: float or tuple
:param timeout: (optional) 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.
"""
client = self._require_client(client)
query_params = self._query_params
Expand All @@ -219,6 +242,7 @@ def update(self, client=None):
data=self._properties,
query_params=query_params,
_target_object=self,
timeout=timeout,
)
self._set_properties(api_response)

Expand Down
18 changes: 15 additions & 3 deletions tests/unit/test__helpers.py
Expand Up @@ -44,6 +44,12 @@ def test_w_env_var(self):


class Test_PropertyMixin(unittest.TestCase):
@staticmethod
def _get_default_timeout():
from google.cloud.storage.constants import _DEFAULT_TIMEOUT

return _DEFAULT_TIMEOUT

@staticmethod
def _get_target_class():
from google.cloud.storage._helpers import _PropertyMixin
Expand Down Expand Up @@ -103,7 +109,7 @@ def test_reload(self):
# Make sure changes is not a set instance before calling reload
# (which will clear / replace it with an empty set), checked below.
derived._changes = object()
derived.reload(client=client)
derived.reload(client=client, timeout=42)
self.assertEqual(derived._properties, {"foo": "Foo"})
kw = connection._requested
self.assertEqual(len(kw), 1)
Expand All @@ -115,6 +121,7 @@ def test_reload(self):
"query_params": {"projection": "noAcl"},
"headers": {},
"_target_object": derived,
"timeout": 42,
},
)
self.assertEqual(derived._changes, set())
Expand All @@ -139,6 +146,7 @@ def test_reload_w_user_project(self):
"query_params": {"projection": "noAcl", "userProject": user_project},
"headers": {},
"_target_object": derived,
"timeout": self._get_default_timeout(),
},
)
self.assertEqual(derived._changes, set())
Expand All @@ -164,7 +172,7 @@ def test_patch(self):
BAZ = object()
derived._properties = {"bar": BAR, "baz": BAZ}
derived._changes = set(["bar"]) # Ignore baz.
derived.patch(client=client)
derived.patch(client=client, timeout=42)
self.assertEqual(derived._properties, {"foo": "Foo"})
kw = connection._requested
self.assertEqual(len(kw), 1)
Expand All @@ -177,6 +185,7 @@ def test_patch(self):
# Since changes does not include `baz`, we don't see it sent.
"data": {"bar": BAR},
"_target_object": derived,
"timeout": 42,
},
)
# Make sure changes get reset by patch().
Expand Down Expand Up @@ -205,6 +214,7 @@ def test_patch_w_user_project(self):
# Since changes does not include `baz`, we don't see it sent.
"data": {"bar": BAR},
"_target_object": derived,
"timeout": self._get_default_timeout(),
},
)
# Make sure changes get reset by patch().
Expand All @@ -219,14 +229,15 @@ def test_update(self):
BAZ = object()
derived._properties = {"bar": BAR, "baz": BAZ}
derived._changes = set(["bar"]) # Update sends 'baz' anyway.
derived.update(client=client)
derived.update(client=client, timeout=42)
self.assertEqual(derived._properties, {"foo": "Foo"})
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]["method"], "PUT")
self.assertEqual(kw[0]["path"], "/path")
self.assertEqual(kw[0]["query_params"], {"projection": "full"})
self.assertEqual(kw[0]["data"], {"bar": BAR, "baz": BAZ})
self.assertEqual(kw[0]["timeout"], 42)
# Make sure changes get reset by patch().
self.assertEqual(derived._changes, set())

Expand All @@ -250,6 +261,7 @@ def test_update_w_user_project(self):
kw[0]["query_params"], {"projection": "full", "userProject": user_project}
)
self.assertEqual(kw[0]["data"], {"bar": BAR, "baz": BAZ})
self.assertEqual(kw[0]["timeout"], self._get_default_timeout())
# Make sure changes get reset by patch().
self.assertEqual(derived._changes, set())

Expand Down

0 comments on commit 317b547

Please sign in to comment.