forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STORAGE: Making Connection.get_all_buckets a standalone method.
See comments in googleapis#700 for context.
- Loading branch information
Showing
9 changed files
with
205 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Methods for interacting with Google Cloud Storage. | ||
Allows interacting with Cloud Storage via user-friendly objects | ||
rather than via Connection. | ||
""" | ||
|
||
from gcloud.storage._implicit_environ import get_default_connection | ||
from gcloud.storage.bucket import Bucket | ||
from gcloud.storage.iterator import Iterator | ||
|
||
|
||
def get_all_buckets(connection=None): | ||
"""Get all buckets in the project. | ||
This will not populate the list of blobs available in each | ||
bucket. | ||
>>> from gcloud import storage | ||
>>> for bucket in storage.get_all_buckets(): | ||
>>> print bucket | ||
This implements "storage.buckets.list". | ||
: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. | ||
:rtype: list of :class:`gcloud.storage.bucket.Bucket` objects. | ||
:returns: All buckets belonging to this project. | ||
""" | ||
if connection is None: | ||
connection = get_default_connection() | ||
return iter(_BucketIterator(connection=connection)) | ||
|
||
|
||
class _BucketIterator(Iterator): | ||
"""An iterator listing all buckets. | ||
You shouldn't have to use this directly, but instead should use the | ||
helper methods on :class:`gcloud.storage.connection.Connection` | ||
objects. | ||
:type connection: :class:`gcloud.storage.connection.Connection` | ||
:param connection: The connection to use for querying the list of buckets. | ||
""" | ||
|
||
def __init__(self, connection): | ||
super(_BucketIterator, self).__init__(connection=connection, path='/b') | ||
|
||
def get_items_from_response(self, response): | ||
"""Factory method which yields :class:`.Bucket` items from a response. | ||
:type response: dict | ||
:param response: The JSON API response for a page of buckets. | ||
""" | ||
for item in response.get('items', []): | ||
yield Bucket(properties=item, connection=self.connection) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest2 | ||
|
||
|
||
class Test__BucketIterator(unittest2.TestCase): | ||
|
||
def _getTargetClass(self): | ||
from gcloud.storage.api import _BucketIterator | ||
return _BucketIterator | ||
|
||
def _makeOne(self, *args, **kw): | ||
return self._getTargetClass()(*args, **kw) | ||
|
||
def test_ctor(self): | ||
connection = object() | ||
iterator = self._makeOne(connection) | ||
self.assertTrue(iterator.connection is connection) | ||
self.assertEqual(iterator.path, '/b') | ||
self.assertEqual(iterator.page_number, 0) | ||
self.assertEqual(iterator.next_page_token, None) | ||
|
||
def test_get_items_from_response_empty(self): | ||
connection = object() | ||
iterator = self._makeOne(connection) | ||
self.assertEqual(list(iterator.get_items_from_response({})), []) | ||
|
||
def test_get_items_from_response_non_empty(self): | ||
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.assertEqual(bucket.name, BLOB_NAME) | ||
|
||
|
||
class Test_get_all_buckets(unittest2.TestCase): | ||
|
||
def _callFUT(self, connection=None): | ||
from gcloud.storage.api import get_all_buckets | ||
return get_all_buckets(connection=connection) | ||
|
||
def test_empty(self): | ||
from gcloud.storage.connection import Connection | ||
PROJECT = 'project' | ||
conn = Connection(PROJECT) | ||
URI = '/'.join([ | ||
conn.API_BASE_URL, | ||
'storage', | ||
conn.API_VERSION, | ||
'b?project=%s' % PROJECT, | ||
]) | ||
http = conn._http = Http( | ||
{'status': '200', 'content-type': 'application/json'}, | ||
'{}', | ||
) | ||
buckets = list(self._callFUT(conn)) | ||
self.assertEqual(len(buckets), 0) | ||
self.assertEqual(http._called_with['method'], 'GET') | ||
self.assertEqual(http._called_with['uri'], URI) | ||
|
||
def _get_all_buckets_non_empty_helper(self, use_default=False): | ||
from gcloud.storage._testing import _monkey_defaults | ||
from gcloud.storage.connection import Connection | ||
PROJECT = 'project' | ||
BUCKET_NAME = 'bucket-name' | ||
conn = Connection(PROJECT) | ||
URI = '/'.join([ | ||
conn.API_BASE_URL, | ||
'storage', | ||
conn.API_VERSION, | ||
'b?project=%s' % PROJECT, | ||
]) | ||
http = conn._http = Http( | ||
{'status': '200', 'content-type': 'application/json'}, | ||
'{"items": [{"name": "%s"}]}' % BUCKET_NAME, | ||
) | ||
|
||
if use_default: | ||
with _monkey_defaults(connection=conn): | ||
buckets = list(self._callFUT()) | ||
else: | ||
buckets = list(self._callFUT(conn)) | ||
|
||
self.assertEqual(len(buckets), 1) | ||
self.assertEqual(buckets[0].name, BUCKET_NAME) | ||
self.assertEqual(http._called_with['method'], 'GET') | ||
self.assertEqual(http._called_with['uri'], URI) | ||
|
||
def test_non_empty(self): | ||
self._get_all_buckets_non_empty_helper(use_default=False) | ||
|
||
def test_non_use_default(self): | ||
self._get_all_buckets_non_empty_helper(use_default=True) | ||
|
||
|
||
class Http(object): | ||
|
||
_called_with = None | ||
|
||
def __init__(self, headers, content): | ||
from httplib2 import Response | ||
self._response = Response(headers) | ||
self._content = content | ||
|
||
def request(self, **kw): | ||
self._called_with = kw | ||
return self._response, self._content |
Oops, something went wrong.