Skip to content

Commit

Permalink
Make storage.get_all_buckets() respect implicit/explicit cnxn.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Mar 28, 2015
1 parent 7ce72ba commit adc470b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
12 changes: 8 additions & 4 deletions gcloud/storage/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ def get_all_buckets(project=None, connection=None):
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending requests.
If not provided, falls back to default.
If not provided, _BucketIterator() will fall back to
default.
:rtype: iterable of :class:`gcloud.storage.bucket.Bucket` objects.
:returns: All buckets belonging to this project.
"""
if connection is None:
connection = get_default_connection()
if project is None:
project = get_default_project()
extra_params = {'project': project}
Expand Down Expand Up @@ -171,6 +170,11 @@ class _BucketIterator(Iterator):
"""

def __init__(self, connection, extra_params=None):
# If an implicit connection was intended, we pass along `None` to the
# Bucket() constructor as well.
self._ctor_connection = connection
if connection is None:
connection = get_default_connection()
super(_BucketIterator, self).__init__(connection=connection, path='/b',
extra_params=extra_params)

Expand All @@ -182,6 +186,6 @@ def get_items_from_response(self, response):
"""
for item in response.get('items', []):
name = item.get('name')
bucket = Bucket(name, connection=self.connection)
bucket = Bucket(name, connection=self._ctor_connection)
bucket._properties = item
yield bucket
13 changes: 10 additions & 3 deletions gcloud/storage/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,19 +282,26 @@ def test_get_items_from_response_empty(self):
iterator = self._makeOne(connection)
self.assertEqual(list(iterator.get_items_from_response({})), [])

def test_get_items_from_response_non_empty(self):
def _get_items_helper(self, connection):
from gcloud.storage.bucket import Bucket
BLOB_NAME = 'blob-name'
response = {'items': [{'name': BLOB_NAME}]}
connection = object()
iterator = self._makeOne(connection)
buckets = list(iterator.get_items_from_response(response))
self.assertEqual(len(buckets), 1)
bucket = buckets[0]
self.assertTrue(isinstance(bucket, Bucket))
self.assertTrue(bucket.connection is connection)
self.assertTrue(bucket._connection is connection)
self.assertEqual(bucket.name, BLOB_NAME)

def test_get_items_from_response_non_empty(self):
connection = object()
self._get_items_helper(connection)

def test_get_items_from_response_implicit_connection(self):
connection = None
self._get_items_helper(connection)


class Http(object):

Expand Down

0 comments on commit adc470b

Please sign in to comment.