Skip to content

Commit 78b1d83

Browse files
committed
Updating Bucket.exists method to accept a client.
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.
1 parent 83590a1 commit 78b1d83

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

gcloud/storage/bucket.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,35 @@ def __init__(self, name=None):
9595
def __repr__(self):
9696
return '<Bucket: %s>' % self.name
9797

98-
def exists(self, connection=None):
98+
@staticmethod
99+
def _client_or_connection(client):
100+
"""Temporary method to get a connection from a client.
101+
102+
If the client is null, gets the connection from the environment.
103+
104+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
105+
:param client: Optional. The client to use. If not passed, falls back
106+
to default connection.
107+
108+
:rtype: :class:`gcloud.storage.connection.Connection`
109+
:returns: The connection determined from the ``client`` or environment.
110+
"""
111+
if client is None:
112+
return _require_connection()
113+
else:
114+
return client.connection
115+
116+
def exists(self, client=None):
99117
"""Determines whether or not this bucket exists.
100118
101-
:type connection: :class:`gcloud.storage.connection.Connection` or
102-
``NoneType``
103-
:param connection: Optional. The connection to use when sending
104-
requests. If not provided, falls back to default.
119+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
120+
:param client: Optional. The client to use. If not passed, falls back
121+
to default connection.
105122
106123
:rtype: boolean
107124
:returns: True if the bucket exists in Cloud Storage.
108125
"""
109-
connection = _require_connection(connection)
126+
connection = self._client_or_connection(client)
110127
try:
111128
# We only need the status code (200 or not) so we seek to
112129
# minimize the returned payload.

gcloud/storage/test_bucket.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ def test_ctor_explicit(self):
125125
self.assertFalse(bucket._default_object_acl.loaded)
126126
self.assertTrue(bucket._default_object_acl.bucket is bucket)
127127

128+
def test__client_or_connection_implicit(self):
129+
from gcloud._testing import _Monkey
130+
from gcloud.storage import bucket as MUT
131+
bucket = self._makeOne()
132+
num_mock_require_calls = [0]
133+
cnxn = object()
134+
135+
def mock_require():
136+
num_mock_require_calls[0] += 1
137+
return cnxn
138+
139+
with _Monkey(MUT, _require_connection=mock_require):
140+
result = bucket._client_or_connection(None)
141+
142+
self.assertTrue(result is cnxn)
143+
self.assertEqual(num_mock_require_calls, [1])
144+
128145
def test_exists_miss(self):
129146
from gcloud.exceptions import NotFound
130147

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

140157
BUCKET_NAME = 'bucket-name'
141158
bucket = self._makeOne(BUCKET_NAME)
142-
self.assertFalse(bucket.exists(connection=_FakeConnection))
159+
client = _Client(_FakeConnection)
160+
self.assertFalse(bucket.exists(client=client))
143161
expected_called_kwargs = {
144162
'method': 'GET',
145163
'path': bucket.path,
@@ -164,7 +182,8 @@ def api_request(cls, *args, **kwargs):
164182

165183
BUCKET_NAME = 'bucket-name'
166184
bucket = self._makeOne(BUCKET_NAME)
167-
self.assertTrue(bucket.exists(connection=_FakeConnection))
185+
client = _Client(_FakeConnection)
186+
self.assertTrue(bucket.exists(client=client))
168187
expected_called_kwargs = {
169188
'method': 'GET',
170189
'path': bucket.path,
@@ -1049,3 +1068,9 @@ class MockFile(io.StringIO):
10491068
def __init__(self, name, buffer_=None):
10501069
super(MockFile, self).__init__(buffer_)
10511070
self.name = name
1071+
1072+
1073+
class _Client(object):
1074+
1075+
def __init__(self, connection):
1076+
self.connection = connection

0 commit comments

Comments
 (0)