From da077f6ab8d52efa9f3d35c8ec1cde3a1784285e Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 10 Jul 2015 18:03:26 -0700 Subject: [PATCH] Updating Blob.exists to accept a client. Towards #952, removing connection from methods / constructors. --- gcloud/storage/blob.py | 14 ++++++++------ gcloud/storage/test_blob.py | 23 +++++++++++++++++++---- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index ad3970c6fa305..2eb4578a0f331 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -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. diff --git a/gcloud/storage/test_blob.py b/gcloud/storage/test_blob.py index de8e4bb380442..133b294aa5cd0 100644 --- a/gcloud/storage/test_blob.py +++ b/gcloud/storage/test_blob.py @@ -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 @@ -307,12 +320,13 @@ 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): @@ -320,11 +334,12 @@ def test_delete_w_explicit_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 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):