From 111c7ef59edbafda3da0424ee57334ecb9b5ace0 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Thu, 26 Jan 2017 17:00:04 -0800 Subject: [PATCH] Changing storage Connection to only accept client. --- storage/google/cloud/storage/_http.py | 13 ++----------- storage/google/cloud/storage/batch.py | 3 +-- storage/google/cloud/storage/client.py | 8 ++++++-- storage/unit_tests/test__http.py | 4 ++-- storage/unit_tests/test_batch.py | 2 +- storage/unit_tests/test_client.py | 18 +++++++++--------- 6 files changed, 21 insertions(+), 27 deletions(-) diff --git a/storage/google/cloud/storage/_http.py b/storage/google/cloud/storage/_http.py index 9deaf4fd37ca..5137082f955d 100644 --- a/storage/google/cloud/storage/_http.py +++ b/storage/google/cloud/storage/_http.py @@ -20,12 +20,8 @@ class Connection(_http.JSONConnection): """A connection to Google Cloud Storage via the JSON REST API. - :type credentials: :class:`oauth2client.client.OAuth2Credentials` - :param credentials: (Optional) The OAuth2 Credentials to use for this - connection. - - :type http: :class:`httplib2.Http` or class that defines ``request()``. - :param http: (Optional) HTTP object to make requests. + :type client: :class:`~google.cloud.storage.client.Client` + :param client: The client that owns the current connection. """ API_BASE_URL = _http.API_BASE_URL @@ -36,8 +32,3 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/storage/{api_version}{path}' """A template for the URL of a particular API call.""" - - SCOPE = ('https://www.googleapis.com/auth/devstorage.full_control', - 'https://www.googleapis.com/auth/devstorage.read_only', - 'https://www.googleapis.com/auth/devstorage.read_write') - """The scopes required for authenticating as a Cloud Storage consumer.""" diff --git a/storage/google/cloud/storage/batch.py b/storage/google/cloud/storage/batch.py index a68d9cdc3ea6..146c52a227bc 100644 --- a/storage/google/cloud/storage/batch.py +++ b/storage/google/cloud/storage/batch.py @@ -132,8 +132,7 @@ class Batch(Connection): _MAX_BATCH_SIZE = 1000 def __init__(self, client): - super(Batch, self).__init__() - self._client = client + super(Batch, self).__init__(client) self._requests = [] self._target_objects = [] diff --git a/storage/google/cloud/storage/client.py b/storage/google/cloud/storage/client.py index 624954e6e7f0..6af7ac650dd3 100644 --- a/storage/google/cloud/storage/client.py +++ b/storage/google/cloud/storage/client.py @@ -46,12 +46,16 @@ class Client(ClientWithProject): ``credentials`` for the current object. """ + SCOPE = ('https://www.googleapis.com/auth/devstorage.full_control', + 'https://www.googleapis.com/auth/devstorage.read_only', + 'https://www.googleapis.com/auth/devstorage.read_write') + """The scopes required for authenticating as a Cloud Storage consumer.""" + def __init__(self, project=None, credentials=None, http=None): self._base_connection = None super(Client, self).__init__(project=project, credentials=credentials, http=http) - self._connection = Connection( - credentials=self._credentials, http=self._http) + self._connection = Connection(self) self._batch_stack = _LocalStack() @property diff --git a/storage/unit_tests/test__http.py b/storage/unit_tests/test__http.py index 3bd1f0031923..ca9bde20cbd3 100644 --- a/storage/unit_tests/test__http.py +++ b/storage/unit_tests/test__http.py @@ -27,7 +27,7 @@ def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) def test_build_api_url_no_extra_query_params(self): - conn = self._make_one() + conn = self._make_one(object()) URI = '/'.join([ conn.API_BASE_URL, 'storage', @@ -40,7 +40,7 @@ def test_build_api_url_w_extra_query_params(self): from six.moves.urllib.parse import parse_qsl from six.moves.urllib.parse import urlsplit - conn = self._make_one() + conn = self._make_one(object()) uri = conn.build_api_url('/foo', {'bar': 'baz'}) scheme, netloc, path, qs, _ = urlsplit(uri) self.assertEqual('%s://%s' % (scheme, netloc), conn.API_BASE_URL) diff --git a/storage/unit_tests/test_batch.py b/storage/unit_tests/test_batch.py index 8557a1a48a46..41861f3c18d7 100644 --- a/storage/unit_tests/test_batch.py +++ b/storage/unit_tests/test_batch.py @@ -399,7 +399,7 @@ def test_as_context_mgr_wo_error(self): project = 'PROJECT' credentials = _make_credentials() client = Client(project=project, credentials=credentials) - client._base_connection._http = http + client._http_internal = http self.assertEqual(list(client._batch_stack), []) diff --git a/storage/unit_tests/test_client.py b/storage/unit_tests/test_client.py index 8ee437fc0ec8..9696d4e5fa51 100644 --- a/storage/unit_tests/test_client.py +++ b/storage/unit_tests/test_client.py @@ -140,7 +140,7 @@ def test_get_bucket_miss(self): 'b', 'nonesuch?projection=noAcl', ]) - http = client._connection._http = _Http( + http = client._http_internal = _Http( {'status': '404', 'content-type': 'application/json'}, b'{}', ) @@ -163,7 +163,7 @@ def test_get_bucket_hit(self): 'b', '%s?projection=noAcl' % (BLOB_NAME,), ]) - http = client._connection._http = _Http( + http = client._http_internal = _Http( {'status': '200', 'content-type': 'application/json'}, '{{"name": "{0}"}}'.format(BLOB_NAME).encode('utf-8'), ) @@ -187,7 +187,7 @@ def test_lookup_bucket_miss(self): 'b', 'nonesuch?projection=noAcl', ]) - http = client._connection._http = _Http( + http = client._http_internal = _Http( {'status': '404', 'content-type': 'application/json'}, b'{}', ) @@ -211,7 +211,7 @@ def test_lookup_bucket_hit(self): 'b', '%s?projection=noAcl' % (BLOB_NAME,), ]) - http = client._connection._http = _Http( + http = client._http_internal = _Http( {'status': '200', 'content-type': 'application/json'}, '{{"name": "{0}"}}'.format(BLOB_NAME).encode('utf-8'), ) @@ -236,7 +236,7 @@ def test_create_bucket_conflict(self): client._connection.API_VERSION, 'b?project=%s' % (PROJECT,), ]) - http = client._connection._http = _Http( + http = client._http_internal = _Http( {'status': '409', 'content-type': 'application/json'}, '{"error": {"message": "Conflict"}}', ) @@ -259,7 +259,7 @@ def test_create_bucket_success(self): client._connection.API_VERSION, 'b?project=%s' % (PROJECT,), ]) - http = client._connection._http = _Http( + http = client._http_internal = _Http( {'status': '200', 'content-type': 'application/json'}, '{{"name": "{0}"}}'.format(BLOB_NAME).encode('utf-8'), ) @@ -282,7 +282,7 @@ def test_list_buckets_empty(self): 'project': [PROJECT], 'projection': ['noAcl'], } - http = client._connection._http = _Http( + http = client._http_internal = _Http( {'status': '200', 'content-type': 'application/json'}, b'{}', ) @@ -319,7 +319,7 @@ def test_list_buckets_non_empty(self): client._connection.API_VERSION, ]) URI = '/'.join([BASE_URI, 'b?%s' % (query_params,)]) - http = client._connection._http = _Http( + http = client._http_internal = _Http( {'status': '200', 'content-type': 'application/json'}, '{{"items": [{{"name": "{0}"}}]}}'.format(BUCKET_NAME) .encode('utf-8'), @@ -354,7 +354,7 @@ def test_list_buckets_all_arguments(self): 'fields': [FIELDS], } - http = client._connection._http = _Http( + http = client._http_internal = _Http( {'status': '200', 'content-type': 'application/json'}, '{"items": []}', )