From 7f23ef522c910dced953ead36b72efbefb0b9a51 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 10 Jul 2015 22:55:41 -0700 Subject: [PATCH] Updating patch() and reload() storage methods to accept a client. Towards #952, removing connection from methods / constructors. --- gcloud/storage/_helpers.py | 40 +++++++++++++++++++++++---------- gcloud/storage/client.py | 2 +- gcloud/storage/test__helpers.py | 12 ++++++++-- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/gcloud/storage/_helpers.py b/gcloud/storage/_helpers.py index 44433335368a7..8f074ee79bd6f 100644 --- a/gcloud/storage/_helpers.py +++ b/gcloud/storage/_helpers.py @@ -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'} @@ -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]) diff --git a/gcloud/storage/client.py b/gcloud/storage/client.py index ac5ffb4bd227f..fd0402d232b6b 100644 --- a/gcloud/storage/client.py +++ b/gcloud/storage/client.py @@ -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): diff --git a/gcloud/storage/test__helpers.py b/gcloud/storage/test__helpers.py index 104ecb38bd05b..c2e72cc542139 100644 --- a/gcloud/storage/test__helpers.py +++ b/gcloud/storage/test__helpers.py @@ -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) @@ -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) @@ -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