Skip to content

Commit

Permalink
Updating storage.Batch 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 13, 2015
1 parent 586274a commit 798845d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
14 changes: 5 additions & 9 deletions gcloud/storage/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

from gcloud._helpers import _LocalStack
from gcloud.exceptions import make_exception
from gcloud.storage import _implicit_environ
from gcloud.storage.connection import Connection


Expand Down Expand Up @@ -130,17 +129,14 @@ def __setitem__(self, key, value):
class Batch(Connection):
"""Proxy an underlying connection, batching up change operations.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: the connection for which the batch proxies.
:type client: :class:`gcloud.storage.client.Client`
:param client: The client to use for making connections.
"""
_MAX_BATCH_SIZE = 1000

def __init__(self, connection=None):
if connection is None:
connection = _implicit_environ.get_default_connection()

def __init__(self, client):
super(Batch, self).__init__()
self._connection = connection
self._client = client
self._requests = []
self._target_objects = []

Expand Down Expand Up @@ -248,7 +244,7 @@ def finish(self):

url = '%s/batch' % self.API_BASE_URL

response, content = self._connection._make_request(
response, content = self._client.connection._make_request(
'POST', url, data=body, headers=headers)
responses = list(_unpack_batch_response(response, content))
self._finish_futures(responses)
Expand Down
38 changes: 19 additions & 19 deletions gcloud/storage/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,9 @@ def _makeOne(self, *args, **kw):
def test_ctor_w_explicit_connection(self):
http = _HTTP()
connection = _Connection(http=http)
batch = self._makeOne(connection)
self.assertTrue(batch._connection is connection)
self.assertEqual(len(batch._requests), 0)
self.assertEqual(len(batch._target_objects), 0)

def test_ctor_w_implicit_connection(self):
from gcloud.storage._testing import _monkey_defaults

http = _HTTP()
connection = _Connection(http=http)
with _monkey_defaults(connection=connection):
batch = self._makeOne()

self.assertTrue(batch._connection is connection)
client = _Client(connection)
batch = self._makeOne(client)
self.assertTrue(batch._client is client)
self.assertEqual(len(batch._requests), 0)
self.assertEqual(len(batch._target_objects), 0)

Expand Down Expand Up @@ -262,7 +251,8 @@ def test_finish_nonempty(self):
expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="'
http = _HTTP((expected, _THREE_PART_MIME_RESPONSE))
connection = _Connection(http=http)
batch = self._makeOne(connection)
client = _Client(connection)
batch = self._makeOne(client)
batch.API_BASE_URL = 'http://api.example.com'
batch._do_request('POST', URL, {}, {'foo': 1, 'bar': 2}, None)
batch._do_request('PATCH', URL, {}, {'bar': 3}, None)
Expand Down Expand Up @@ -311,7 +301,8 @@ def test_finish_responses_mismatch(self):
expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="'
http = _HTTP((expected, _TWO_PART_MIME_RESPONSE_WITH_FAIL))
connection = _Connection(http=http)
batch = self._makeOne(connection)
client = _Client(connection)
batch = self._makeOne(client)
batch.API_BASE_URL = 'http://api.example.com'
batch._requests.append(('GET', URL, {}, None))
self.assertRaises(ValueError, batch.finish)
Expand All @@ -323,7 +314,8 @@ def test_finish_nonempty_with_status_failure(self):
expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="'
http = _HTTP((expected, _TWO_PART_MIME_RESPONSE_WITH_FAIL))
connection = _Connection(http=http)
batch = self._makeOne(connection)
client = _Client(connection)
batch = self._makeOne(client)
batch.API_BASE_URL = 'http://api.example.com'
target1 = _MockObject()
target2 = _MockObject()
Expand Down Expand Up @@ -363,7 +355,8 @@ def test_finish_nonempty_non_multipart_response(self):
expected['content-type'] = 'text/plain'
http = _HTTP((expected, 'NOT A MIME_RESPONSE'))
connection = _Connection(http=http)
batch = self._makeOne(connection)
client = _Client(connection)
batch = self._makeOne(client)
batch._requests.append(('POST', URL, {}, {'foo': 1, 'bar': 2}))
batch._requests.append(('PATCH', URL, {}, {'bar': 3}))
batch._requests.append(('DELETE', URL, {}, None))
Expand All @@ -376,13 +369,14 @@ def test_as_context_mgr_wo_error(self):
expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="'
http = _HTTP((expected, _THREE_PART_MIME_RESPONSE))
connection = _Connection(http=http)
client = _Client(connection)

self.assertEqual(list(_BATCHES), [])

target1 = _MockObject()
target2 = _MockObject()
target3 = _MockObject()
with self._makeOne(connection) as batch:
with self._makeOne(client) as batch:
self.assertEqual(list(_BATCHES), [batch])
batch._make_request('POST', URL, {'foo': 1, 'bar': 2},
target_object=target1)
Expand Down Expand Up @@ -596,3 +590,9 @@ def request(self, method, uri, headers, body):

class _MockObject(object):
pass


class _Client(object):

def __init__(self, connection):
self.connection = connection
2 changes: 1 addition & 1 deletion system_tests/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def setUp(self):
self.case_buckets_to_delete = []

def tearDown(self):
with storage.Batch():
with storage.Batch(CLIENT):
for bucket_name in self.case_buckets_to_delete:
storage.Bucket(bucket_name).delete()

Expand Down

1 comment on commit 798845d

@tseaver
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit LGTM.

Please sign in to comment.