Skip to content

Commit f19effa

Browse files
committed
Make storage.get_all_buckets() respect implicit/explicit cnxn.
1 parent fa2ff2b commit f19effa

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

gcloud/storage/api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,12 @@ def get_all_buckets(project=None, connection=None):
7676
:type connection: :class:`gcloud.storage.connection.Connection` or
7777
``NoneType``
7878
:param connection: Optional. The connection to use when sending requests.
79-
If not provided, falls back to default.
79+
If not provided, _BucketIterator() will fall back to
80+
default.
8081
8182
:rtype: iterable of :class:`gcloud.storage.bucket.Bucket` objects.
8283
:returns: All buckets belonging to this project.
8384
"""
84-
if connection is None:
85-
connection = get_default_connection()
8685
if project is None:
8786
project = get_default_project()
8887
extra_params = {'project': project}
@@ -171,6 +170,11 @@ class _BucketIterator(Iterator):
171170
"""
172171

173172
def __init__(self, connection, extra_params=None):
173+
# If an implicit connection was intended, we pass along `None` to the
174+
# Bucket() constructor as well.
175+
self._ctor_connection = connection
176+
if connection is None:
177+
connection = get_default_connection()
174178
super(_BucketIterator, self).__init__(connection=connection, path='/b',
175179
extra_params=extra_params)
176180

@@ -182,6 +186,6 @@ def get_items_from_response(self, response):
182186
"""
183187
for item in response.get('items', []):
184188
name = item.get('name')
185-
bucket = Bucket(name, connection=self.connection)
189+
bucket = Bucket(name, connection=self._ctor_connection)
186190
bucket._properties = item
187191
yield bucket

gcloud/storage/test_api.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,26 @@ def test_get_items_from_response_empty(self):
282282
iterator = self._makeOne(connection)
283283
self.assertEqual(list(iterator.get_items_from_response({})), [])
284284

285-
def test_get_items_from_response_non_empty(self):
285+
def _get_items_helper(self, connection):
286286
from gcloud.storage.bucket import Bucket
287287
BLOB_NAME = 'blob-name'
288288
response = {'items': [{'name': BLOB_NAME}]}
289-
connection = object()
290289
iterator = self._makeOne(connection)
291290
buckets = list(iterator.get_items_from_response(response))
292291
self.assertEqual(len(buckets), 1)
293292
bucket = buckets[0]
294293
self.assertTrue(isinstance(bucket, Bucket))
295-
self.assertTrue(bucket.connection is connection)
294+
self.assertTrue(bucket._connection is connection)
296295
self.assertEqual(bucket.name, BLOB_NAME)
297296

297+
def test_get_items_from_response_non_empty(self):
298+
connection = object()
299+
self._get_items_helper(connection)
300+
301+
def test_get_items_from_response_implicit_connection(self):
302+
connection = None
303+
self._get_items_helper(connection)
304+
298305

299306
class Http(object):
300307

0 commit comments

Comments
 (0)