Skip to content

Commit 247899b

Browse files
committed
Merge pull request #984 from dhermes/replace-connection-in-storage
Replace optional connection with client in storage methods and functions.
2 parents 8373227 + 586274a commit 247899b

File tree

10 files changed

+393
-262
lines changed

10 files changed

+393
-262
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/acl.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,33 @@ def get_entities(self):
351351
self._ensure_loaded()
352352
return list(self.entities.values())
353353

354-
def reload(self, connection=None):
354+
@staticmethod
355+
def _client_or_connection(client):
356+
"""Temporary method to get a connection from a client.
357+
358+
If the client is null, gets the connection from the environment.
359+
360+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
361+
:param client: Optional. The client to use. If not passed, falls back
362+
to default connection.
363+
364+
:rtype: :class:`gcloud.storage.connection.Connection`
365+
:returns: The connection determined from the ``client`` or environment.
366+
"""
367+
if client is None:
368+
return _require_connection()
369+
else:
370+
return client.connection
371+
372+
def reload(self, client=None):
355373
"""Reload the ACL data from Cloud Storage.
356374
357-
:type connection: :class:`gcloud.storage.connection.Connection` or None
358-
:param connection: explicit connection to use for API request;
359-
defaults to instance property.
375+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
376+
:param client: Optional. The client to use. If not passed, falls back
377+
to default connection.
360378
"""
361379
path = self.reload_path
362-
connection = _require_connection(connection)
380+
connection = self._client_or_connection(client)
363381

364382
self.entities.clear()
365383

@@ -368,16 +386,16 @@ def reload(self, connection=None):
368386
for entry in found.get('items', ()):
369387
self.add_entity(self.entity_from_dict(entry))
370388

371-
def save(self, acl=None, connection=None):
389+
def save(self, acl=None, client=None):
372390
"""Save this ACL for the current bucket.
373391
374392
:type acl: :class:`gcloud.storage.acl.ACL`, or a compatible list.
375393
:param acl: The ACL object to save. If left blank, this will save
376394
current entries.
377395
378-
:type connection: :class:`gcloud.storage.connection.Connection` or None
379-
:param connection: explicit connection to use for API request;
380-
defaults to instance property.
396+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
397+
:param client: Optional. The client to use. If not passed, falls back
398+
to default connection.
381399
"""
382400
if acl is None:
383401
acl = self
@@ -387,7 +405,7 @@ def save(self, acl=None, connection=None):
387405

388406
if save_to_backend:
389407
path = self.save_path
390-
connection = _require_connection(connection)
408+
connection = self._client_or_connection(client)
391409
result = connection.api_request(
392410
method='PATCH',
393411
path=path,
@@ -398,19 +416,19 @@ def save(self, acl=None, connection=None):
398416
self.add_entity(self.entity_from_dict(entry))
399417
self.loaded = True
400418

401-
def clear(self, connection=None):
419+
def clear(self, client=None):
402420
"""Remove all ACL entries.
403421
404422
Note that this won't actually remove *ALL* the rules, but it
405423
will remove all the non-default rules. In short, you'll still
406424
have access to a bucket that you created even after you clear
407425
ACL rules with this method.
408426
409-
:type connection: :class:`gcloud.storage.connection.Connection` or None
410-
:param connection: explicit connection to use for API request;
411-
defaults to instance property.
427+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
428+
:param client: Optional. The client to use. If not passed, falls back
429+
to default connection.
412430
"""
413-
self.save([], connection)
431+
self.save([], client=client)
414432

415433

416434
class BucketACL(ACL):

0 commit comments

Comments
 (0)