From 618605922f68073936ece934c92f1586c5a45e4d Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Thu, 12 Mar 2015 14:58:37 -0700 Subject: [PATCH] Using 'fields' in Bucket.exists(). This decouples Bucket.exists() from Connection.get_bucket(). Relevant to #632. --- gcloud/storage/bucket.py | 6 +++++- gcloud/storage/test_bucket.py | 38 +++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index 210edbbbbd77..cba3b43ced62 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -132,7 +132,11 @@ def exists(self): :returns: True if the bucket exists in Cloud Storage. """ try: - self.connection.get_bucket(self.name) + # We only need the status code (200 or not) so we seek to + # minimize the returned payload. + query_params = {'fields': 'name'} + self.connection.api_request(method='GET', path=self.path, + query_params=query_params) return True except NotFound: return False diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index fbe6b3c994fc..4bef81ad5f4d 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -159,14 +159,22 @@ class _FakeConnection(object): _called_with = [] @classmethod - def get_bucket(cls, bucket_name): - cls._called_with.append(bucket_name) - raise NotFound(bucket_name) + def api_request(cls, *args, **kwargs): + cls._called_with.append((args, kwargs)) + raise NotFound(args) - NAME = 'name' - bucket = self._makeOne(connection=_FakeConnection, name=NAME) + BUCKET_NAME = 'bucket-name' + bucket = self._makeOne(connection=_FakeConnection, name=BUCKET_NAME) self.assertFalse(bucket.exists()) - self.assertEqual(_FakeConnection._called_with, [NAME]) + expected_called_kwargs = { + 'method': 'GET', + 'path': bucket.path, + 'query_params': { + 'fields': 'name', + }, + } + expected_cw = [((), expected_called_kwargs)] + self.assertEqual(_FakeConnection._called_with, expected_cw) def test_exists_hit(self): class _FakeConnection(object): @@ -174,15 +182,23 @@ class _FakeConnection(object): _called_with = [] @classmethod - def get_bucket(cls, bucket_name): - cls._called_with.append(bucket_name) + def api_request(cls, *args, **kwargs): + cls._called_with.append((args, kwargs)) # exists() does not use the return value return object() - NAME = 'name' - bucket = self._makeOne(connection=_FakeConnection, name=NAME) + BUCKET_NAME = 'bucket-name' + bucket = self._makeOne(connection=_FakeConnection, name=BUCKET_NAME) self.assertTrue(bucket.exists()) - self.assertEqual(_FakeConnection._called_with, [NAME]) + expected_called_kwargs = { + 'method': 'GET', + 'path': bucket.path, + 'query_params': { + 'fields': 'name', + }, + } + expected_cw = [((), expected_called_kwargs)] + self.assertEqual(_FakeConnection._called_with, expected_cw) def test_acl_property(self): from gcloud.storage.acl import BucketACL