Skip to content

Commit

Permalink
Add 'update' API wrapper for buckets/blobs.
Browse files Browse the repository at this point in the history
Turns out some properties (i.e., 'labels', see googleapis#3711) behave differently
under 'patch semantics'[1], which makes 'update' useful.

[1] https://cloud.google.com/storage/docs/json_api/v1/how-tos/performance#patch
  • Loading branch information
tseaver authored and Luke Sneeringer committed Aug 7, 2017
1 parent b09eda9 commit 2eda894
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions storage/google/cloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ def update(self, client=None):
query_params={'projection': 'full'}, _target_object=self)
self._set_properties(api_response)

def update(self, client=None):
"""Sends all properties in a PUT request.
Updates the ``_properties`` with the response from the backend.
:type client: :class:`~google.cloud.storage.client.Client` or
``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current object.
"""
client = self._require_client(client)
api_response = client._connection.api_request(
method='PUT', path=self.path, data=self._properties,
query_params={'projection': 'full'}, _target_object=self)
self._set_properties(api_response)


def _scalar_property(fieldname):
"""Create a property descriptor around the :class:`_PropertyMixin` helpers.
Expand Down
20 changes: 20 additions & 0 deletions storage/tests/unit/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ def test_update(self):
# Make sure changes get reset by patch().
self.assertEqual(derived._changes, set())

def test_update(self):
connection = _Connection({'foo': 'Foo'})
client = _Client(connection)
derived = self._derivedClass('/path')()
# Make sure changes is non-empty, so we can observe a change.
BAR = object()
BAZ = object()
derived._properties = {'bar': BAR, 'baz': BAZ}
derived._changes = set(['bar']) # Update sends 'baz' anyway.
derived.update(client=client)
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})
# Make sure changes get reset by patch().
self.assertEqual(derived._changes, set())


class Test__scalar_property(unittest.TestCase):

Expand Down

0 comments on commit 2eda894

Please sign in to comment.