Skip to content

Commit

Permalink
Updating Bucket.exists method to accept a client.
Browse files Browse the repository at this point in the history
Towards #952, removing connection from methods / constructors.

Also adding a temporary Bucket._client_or_connection method
to allow switching from an explicit client to an implicit
connection.
  • Loading branch information
dhermes committed Jul 13, 2015
1 parent 83590a1 commit 78b1d83
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
29 changes: 23 additions & 6 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,35 @@ def __init__(self, name=None):
def __repr__(self):
return '<Bucket: %s>' % self.name

def exists(self, connection=None):
@staticmethod
def _client_or_connection(client):
"""Temporary method to get a connection from a client.
If the client is null, gets the connection from the environment.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: The connection determined from the ``client`` or environment.
"""
if client is None:
return _require_connection()
else:
return client.connection

def exists(self, client=None):
"""Determines whether or not this bucket exists.
: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.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:rtype: boolean
:returns: True if the bucket exists in Cloud Storage.
"""
connection = _require_connection(connection)
connection = self._client_or_connection(client)
try:
# We only need the status code (200 or not) so we seek to
# minimize the returned payload.
Expand Down
29 changes: 27 additions & 2 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ def test_ctor_explicit(self):
self.assertFalse(bucket._default_object_acl.loaded)
self.assertTrue(bucket._default_object_acl.bucket is bucket)

def test__client_or_connection_implicit(self):
from gcloud._testing import _Monkey
from gcloud.storage import bucket as MUT
bucket = self._makeOne()
num_mock_require_calls = [0]
cnxn = object()

def mock_require():
num_mock_require_calls[0] += 1
return cnxn

with _Monkey(MUT, _require_connection=mock_require):
result = bucket._client_or_connection(None)

self.assertTrue(result is cnxn)
self.assertEqual(num_mock_require_calls, [1])

def test_exists_miss(self):
from gcloud.exceptions import NotFound

Expand All @@ -139,7 +156,8 @@ def api_request(cls, *args, **kwargs):

BUCKET_NAME = 'bucket-name'
bucket = self._makeOne(BUCKET_NAME)
self.assertFalse(bucket.exists(connection=_FakeConnection))
client = _Client(_FakeConnection)
self.assertFalse(bucket.exists(client=client))
expected_called_kwargs = {
'method': 'GET',
'path': bucket.path,
Expand All @@ -164,7 +182,8 @@ def api_request(cls, *args, **kwargs):

BUCKET_NAME = 'bucket-name'
bucket = self._makeOne(BUCKET_NAME)
self.assertTrue(bucket.exists(connection=_FakeConnection))
client = _Client(_FakeConnection)
self.assertTrue(bucket.exists(client=client))
expected_called_kwargs = {
'method': 'GET',
'path': bucket.path,
Expand Down Expand Up @@ -1049,3 +1068,9 @@ class MockFile(io.StringIO):
def __init__(self, name, buffer_=None):
super(MockFile, self).__init__(buffer_)
self.name = name


class _Client(object):

def __init__(self, connection):
self.connection = connection

1 comment on commit 78b1d83

@tseaver
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit LGTM.

Please sign in to comment.