Skip to content

Commit 586274a

Browse files
committed
Updating patch() and reload() storage methods to accept a client.
Towards #952, removing connection from methods / constructors.
1 parent adb0bc1 commit 586274a

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

gcloud/storage/_helpers.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,32 @@ def __init__(self, name=None):
4545
self._properties = {}
4646
self._changes = set()
4747

48-
def reload(self, connection=None):
48+
@staticmethod
49+
def _client_or_connection(client):
50+
"""Temporary method to get a connection from a client.
51+
52+
If the client is null, gets the connection from the environment.
53+
54+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
55+
:param client: Optional. The client to use. If not passed, falls back
56+
to default connection.
57+
58+
:rtype: :class:`gcloud.storage.connection.Connection`
59+
:returns: The connection determined from the ``client`` or environment.
60+
"""
61+
if client is None:
62+
return _require_connection()
63+
else:
64+
return client.connection
65+
66+
def reload(self, client=None):
4967
"""Reload properties from Cloud Storage.
5068
51-
:type connection: :class:`gcloud.storage.connection.Connection`
52-
:param connection: An explicit connection to use for the API request.
53-
If not passed, use the connection assigned to
54-
the object in its constructor.
69+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
70+
:param client: Optional. The client to use. If not passed, falls back
71+
to default connection.
5572
"""
56-
connection = _require_connection(connection)
73+
connection = self._client_or_connection(client)
5774
# Pass only '?projection=noAcl' here because 'acl' and related
5875
# are handled via custom endpoints.
5976
query_params = {'projection': 'noAcl'}
@@ -90,17 +107,16 @@ def _set_properties(self, value):
90107
# If the values are reset, the changes must as well.
91108
self._changes = set()
92109

93-
def patch(self, connection=None):
110+
def patch(self, client=None):
94111
"""Sends all changed properties in a PATCH request.
95112
96113
Updates the ``_properties`` with the response from the backend.
97114
98-
:type connection: :class:`gcloud.storage.connection.Connection`
99-
:param connection: An explicit connection to use for the API request.
100-
If not passed, use the connection assigned to
101-
the object in its constructor.
115+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
116+
:param client: Optional. The client to use. If not passed, falls back
117+
to default connection.
102118
"""
103-
connection = _require_connection(connection)
119+
connection = self._client_or_connection(client)
104120
# Pass '?projection=full' here because 'PATCH' documented not
105121
# to work properly w/ 'noAcl'.
106122
update_properties = dict((key, self._properties[key])

gcloud/storage/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def get_bucket(self, bucket_name):
6868
:raises: :class:`gcloud.exceptions.NotFound`
6969
"""
7070
bucket = Bucket(bucket_name)
71-
bucket.reload(connection=self.connection)
71+
bucket.reload(client=self)
7272
return bucket
7373

7474
def lookup_bucket(self, bucket_name):

gcloud/storage/test__helpers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ def test_reload_w_implicit_connection(self):
6060

6161
def test_reload_w_explicit_connection(self):
6262
connection = _Connection({'foo': 'Foo'})
63+
client = _Client(connection)
6364
derived = self._derivedClass('/path')()
6465
# Make sure changes is not a set, so we can observe a change.
6566
derived._changes = object()
66-
derived.reload(connection)
67+
derived.reload(client=client)
6768
self.assertEqual(derived._properties, {'foo': 'Foo'})
6869
kw = connection._requested
6970
self.assertEqual(len(kw), 1)
@@ -108,13 +109,14 @@ def test_patch_w_implicit_connection(self):
108109

109110
def test_patch_w_explicit_connection(self):
110111
connection = _Connection({'foo': 'Foo'})
112+
client = _Client(connection)
111113
derived = self._derivedClass('/path')()
112114
# Make sure changes is non-empty, so we can observe a change.
113115
BAR = object()
114116
BAZ = object()
115117
derived._properties = {'bar': BAR, 'baz': BAZ}
116118
derived._changes = set(['bar']) # Ignore baz.
117-
derived.patch(connection)
119+
derived.patch(client=client)
118120
self.assertEqual(derived._properties, {'foo': 'Foo'})
119121
kw = connection._requested
120122
self.assertEqual(len(kw), 1)
@@ -301,3 +303,9 @@ def __enter__(self):
301303
def __exit__(self, *args):
302304
from gcloud.storage.batch import _BATCHES
303305
_BATCHES.pop()
306+
307+
308+
class _Client(object):
309+
310+
def __init__(self, connection):
311+
self.connection = connection

0 commit comments

Comments
 (0)