Skip to content

Commit

Permalink
Updating patch() and reload() storage methods to accept a client.
Browse files Browse the repository at this point in the history
Towards googleapis#952, removing connection from methods / constructors.
  • Loading branch information
dhermes committed Jul 11, 2015
1 parent d47b573 commit 7f23ef5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
40 changes: 28 additions & 12 deletions gcloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,32 @@ def __init__(self, name=None):
self._properties = {}
self._changes = set()

def reload(self, connection=None):
@staticmethod
def _client_or_connection(client):
"""Temporary method to get a connection from a client.
If the client is null, gets the connection from the environment.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: The connection determined from the ``client`` or environment.
"""
if client is None:
return _require_connection()
else:
return client.connection

def reload(self, client=None):
"""Reload properties from Cloud Storage.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: An explicit connection to use for the API request.
If not passed, use the connection assigned to
the object in its constructor.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
"""
connection = _require_connection(connection)
connection = self._client_or_connection(client)
# Pass only '?projection=noAcl' here because 'acl' and related
# are handled via custom endpoints.
query_params = {'projection': 'noAcl'}
Expand Down Expand Up @@ -90,17 +107,16 @@ def _set_properties(self, value):
# If the values are reset, the changes must as well.
self._changes = set()

def patch(self, connection=None):
def patch(self, client=None):
"""Sends all changed properties in a PATCH request.
Updates the ``_properties`` with the response from the backend.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: An explicit connection to use for the API request.
If not passed, use the connection assigned to
the object in its constructor.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
"""
connection = _require_connection(connection)
connection = self._client_or_connection(client)
# Pass '?projection=full' here because 'PATCH' documented not
# to work properly w/ 'noAcl'.
update_properties = dict((key, self._properties[key])
Expand Down
2 changes: 1 addition & 1 deletion gcloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_bucket(self, bucket_name):
:raises: :class:`gcloud.exceptions.NotFound`
"""
bucket = Bucket(bucket_name)
bucket.reload(connection=self.connection)
bucket.reload(client=self)
return bucket

def lookup_bucket(self, bucket_name):
Expand Down
12 changes: 10 additions & 2 deletions gcloud/storage/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ def test_reload_w_implicit_connection(self):

def test_reload_w_explicit_connection(self):
connection = _Connection({'foo': 'Foo'})
client = _Client(connection)
derived = self._derivedClass('/path')()
# Make sure changes is not a set, so we can observe a change.
derived._changes = object()
derived.reload(connection)
derived.reload(client=client)
self.assertEqual(derived._properties, {'foo': 'Foo'})
kw = connection._requested
self.assertEqual(len(kw), 1)
Expand Down Expand Up @@ -108,13 +109,14 @@ def test_patch_w_implicit_connection(self):

def test_patch_w_explicit_connection(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']) # Ignore baz.
derived.patch(connection)
derived.patch(client=client)
self.assertEqual(derived._properties, {'foo': 'Foo'})
kw = connection._requested
self.assertEqual(len(kw), 1)
Expand Down Expand Up @@ -301,3 +303,9 @@ def __enter__(self):
def __exit__(self, *args):
from gcloud.storage.batch import _BATCHES
_BATCHES.pop()


class _Client(object):

def __init__(self, connection):
self.connection = connection

0 comments on commit 7f23ef5

Please sign in to comment.