Skip to content

Commit

Permalink
Updating Blob.exists to accept a client.
Browse files Browse the repository at this point in the history
Towards googleapis#952, removing connection from methods / constructors.
  • Loading branch information
dhermes committed Jul 11, 2015
1 parent cd7cb5d commit de29f26
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
14 changes: 8 additions & 6 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,20 @@ def generate_signed_url(self, expiration, method='GET',
api_access_endpoint=_API_ACCESS_ENDPOINT,
expiration=expiration, method=method)

def exists(self, connection=None):
def exists(self, client=None):
"""Determines whether or not this blob 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 blob exists in Cloud Storage.
"""
connection = _require_connection(connection)
if client is None:
connection = _require_connection()
else:
connection = client.connection
try:
# We only need the status code (200 or not) so we seek to
# minimize the returned payload.
Expand Down
23 changes: 19 additions & 4 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,32 @@ def test_exists_miss(self):
NONESUCH = 'nonesuch'
not_found_response = {'status': NOT_FOUND}
connection = _Connection(not_found_response)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(NONESUCH, bucket=bucket)
self.assertFalse(blob.exists(connection=connection))
self.assertFalse(blob.exists(client=client))

def test_exists_implicit(self):
from gcloud.storage._testing import _monkey_defaults
from six.moves.http_client import NOT_FOUND
NONESUCH = 'nonesuch'
not_found_response = {'status': NOT_FOUND}
connection = _Connection(not_found_response)
bucket = _Bucket()
blob = self._makeOne(NONESUCH, bucket=bucket)
with _monkey_defaults(connection=connection):
self.assertFalse(blob.exists())

def test_exists_hit(self):
from six.moves.http_client import OK
BLOB_NAME = 'blob-name'
found_response = {'status': OK}
connection = _Connection(found_response)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
bucket._blobs[BLOB_NAME] = 1
self.assertTrue(blob.exists(connection=connection))
self.assertTrue(blob.exists(client=client))

def test_rename_w_implicit_connection(self):
from gcloud.storage._testing import _monkey_defaults
Expand Down Expand Up @@ -307,24 +320,26 @@ def test_delete_w_implicit_connection(self):
BLOB_NAME = 'blob-name'
not_found_response = {'status': NOT_FOUND}
connection = _Connection(not_found_response)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
bucket._blobs[BLOB_NAME] = 1
with _monkey_defaults(connection=connection):
blob.delete()
self.assertFalse(blob.exists(connection=connection))
self.assertFalse(blob.exists(client=client))
self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])

def test_delete_w_explicit_connection(self):
from six.moves.http_client import NOT_FOUND
BLOB_NAME = 'blob-name'
not_found_response = {'status': NOT_FOUND}
connection = _Connection(not_found_response)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
bucket._blobs[BLOB_NAME] = 1
blob.delete(connection=connection)
self.assertFalse(blob.exists(connection=connection))
self.assertFalse(blob.exists(client=client))
self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])

def _download_to_file_helper(self, chunk_size=None):
Expand Down

0 comments on commit de29f26

Please sign in to comment.