Skip to content

Commit 3905a5b

Browse files
committed
Updating Blob.exists to accept a client.
Towards #952, removing connection from methods / constructors.
1 parent 872a101 commit 3905a5b

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

gcloud/storage/blob.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,20 @@ def generate_signed_url(self, expiration, method='GET',
200200
api_access_endpoint=_API_ACCESS_ENDPOINT,
201201
expiration=expiration, method=method)
202202

203-
def exists(self, connection=None):
203+
def exists(self, client=None):
204204
"""Determines whether or not this blob exists.
205205
206-
:type connection: :class:`gcloud.storage.connection.Connection` or
207-
``NoneType``
208-
:param connection: Optional. The connection to use when sending
209-
requests. If not provided, falls back to default.
206+
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
207+
:param client: Optional. The client to use. If not passed, falls back
208+
to default connection.
210209
211210
:rtype: boolean
212211
:returns: True if the blob exists in Cloud Storage.
213212
"""
214-
connection = _require_connection(connection)
213+
if client is None:
214+
connection = _require_connection()
215+
else:
216+
connection = client.connection
215217
try:
216218
# We only need the status code (200 or not) so we seek to
217219
# minimize the returned payload.

gcloud/storage/test_blob.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,32 @@ def test_exists_miss(self):
257257
NONESUCH = 'nonesuch'
258258
not_found_response = {'status': NOT_FOUND}
259259
connection = _Connection(not_found_response)
260+
client = _Client(connection)
260261
bucket = _Bucket()
261262
blob = self._makeOne(NONESUCH, bucket=bucket)
262-
self.assertFalse(blob.exists(connection=connection))
263+
self.assertFalse(blob.exists(client=client))
264+
265+
def test_exists_implicit(self):
266+
from gcloud.storage._testing import _monkey_defaults
267+
from six.moves.http_client import NOT_FOUND
268+
NONESUCH = 'nonesuch'
269+
not_found_response = {'status': NOT_FOUND}
270+
connection = _Connection(not_found_response)
271+
bucket = _Bucket()
272+
blob = self._makeOne(NONESUCH, bucket=bucket)
273+
with _monkey_defaults(connection=connection):
274+
self.assertFalse(blob.exists())
263275

264276
def test_exists_hit(self):
265277
from six.moves.http_client import OK
266278
BLOB_NAME = 'blob-name'
267279
found_response = {'status': OK}
268280
connection = _Connection(found_response)
281+
client = _Client(connection)
269282
bucket = _Bucket()
270283
blob = self._makeOne(BLOB_NAME, bucket=bucket)
271284
bucket._blobs[BLOB_NAME] = 1
272-
self.assertTrue(blob.exists(connection=connection))
285+
self.assertTrue(blob.exists(client=client))
273286

274287
def test_rename_w_implicit_connection(self):
275288
from gcloud.storage._testing import _monkey_defaults
@@ -307,24 +320,26 @@ def test_delete_w_implicit_connection(self):
307320
BLOB_NAME = 'blob-name'
308321
not_found_response = {'status': NOT_FOUND}
309322
connection = _Connection(not_found_response)
323+
client = _Client(connection)
310324
bucket = _Bucket()
311325
blob = self._makeOne(BLOB_NAME, bucket=bucket)
312326
bucket._blobs[BLOB_NAME] = 1
313327
with _monkey_defaults(connection=connection):
314328
blob.delete()
315-
self.assertFalse(blob.exists(connection=connection))
329+
self.assertFalse(blob.exists(client=client))
316330
self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])
317331

318332
def test_delete_w_explicit_connection(self):
319333
from six.moves.http_client import NOT_FOUND
320334
BLOB_NAME = 'blob-name'
321335
not_found_response = {'status': NOT_FOUND}
322336
connection = _Connection(not_found_response)
337+
client = _Client(connection)
323338
bucket = _Bucket()
324339
blob = self._makeOne(BLOB_NAME, bucket=bucket)
325340
bucket._blobs[BLOB_NAME] = 1
326341
blob.delete(connection=connection)
327-
self.assertFalse(blob.exists(connection=connection))
342+
self.assertFalse(blob.exists(client=client))
328343
self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])
329344

330345
def _download_to_file_helper(self, chunk_size=None):

0 commit comments

Comments
 (0)