Skip to content

Commit

Permalink
Changing storage Connection to only accept client.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Jan 27, 2017
1 parent f651cce commit 111c7ef
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 27 deletions.
13 changes: 2 additions & 11 deletions storage/google/cloud/storage/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."""
3 changes: 1 addition & 2 deletions storage/google/cloud/storage/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
8 changes: 6 additions & 2 deletions storage/google/cloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions storage/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion storage/unit_tests/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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), [])

Expand Down
18 changes: 9 additions & 9 deletions storage/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'{}',
)
Expand All @@ -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'),
)
Expand All @@ -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'{}',
)
Expand All @@ -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'),
)
Expand All @@ -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"}}',
)
Expand All @@ -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'),
)
Expand All @@ -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'{}',
)
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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": []}',
)
Expand Down

0 comments on commit 111c7ef

Please sign in to comment.