Skip to content

Commit 1f57d98

Browse files
authored
Merge pull request #145 from jcoatgoogle/jco/batch_callback
Add batch request callback hook
2 parents b71c414 + 4e88a3d commit 1f57d98

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

apitools/base/py/batch.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def Add(self, service, method, request, global_params=None):
182182
self.api_requests.append(api_request)
183183

184184
def Execute(self, http, sleep_between_polls=5, max_retries=5,
185-
max_batch_size=None):
185+
max_batch_size=None, batch_request_callback=None):
186186
"""Execute all of the requests in the batch.
187187
188188
Args:
@@ -194,6 +194,8 @@ def Execute(self, http, sleep_between_polls=5, max_retries=5,
194194
exception, whatever it happened to be.
195195
max_batch_size: int, if specified requests will be split in batches
196196
of given size.
197+
batch_request_callback: function of (http_response, exception) passed
198+
to BatchHttpRequest which will be run on any given results.
197199
198200
Returns:
199201
List of ApiCalls.
@@ -209,7 +211,10 @@ def Execute(self, http, sleep_between_polls=5, max_retries=5,
209211
for i in range(0, len(requests), batch_size):
210212
# Create a batch_http_request object and populate it with
211213
# incomplete requests.
212-
batch_http_request = BatchHttpRequest(batch_url=self.batch_url)
214+
batch_http_request = BatchHttpRequest(
215+
batch_url=self.batch_url,
216+
callback=batch_request_callback
217+
)
213218
for request in itertools.islice(requests,
214219
i, i + batch_size):
215220
batch_http_request.Add(

apitools/base/py/batch_test.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,18 @@ def testRequestServiceUnavailable(self):
150150
exceptions.HttpError)
151151

152152
def testSingleRequestInBatch(self):
153+
desired_url = 'https://www.example.com'
154+
155+
callback_was_called = []
156+
def _Callback(response, exception):
157+
self.assertEqual({'status': '200'}, response.info)
158+
self.assertEqual('content', response.content)
159+
self.assertEqual(desired_url, response.request_url)
160+
self.assertIsNone(exception)
161+
callback_was_called.append(1)
162+
153163
mock_service = FakeService()
154164

155-
desired_url = 'https://www.example.com'
156165
batch_api_request = batch.BatchApiRequest(batch_url=desired_url)
157166
# The request to be added. The actual request sent will be somewhat
158167
# larger, as this is added to a batch.
@@ -185,7 +194,8 @@ def testSingleRequestInBatch(self):
185194
'desired_request': desired_request,
186195
})
187196

188-
api_request_responses = batch_api_request.Execute(FakeHttp())
197+
api_request_responses = batch_api_request.Execute(
198+
FakeHttp(), batch_request_callback=_Callback)
189199

190200
self.assertEqual(1, len(api_request_responses))
191201
self.assertEqual(1, mock_request.call_count)
@@ -196,6 +206,7 @@ def testSingleRequestInBatch(self):
196206
self.assertEqual({'status': '200'}, response.info)
197207
self.assertEqual('content', response.content)
198208
self.assertEqual(desired_url, response.request_url)
209+
self.assertEquals(1, len(callback_was_called))
199210

200211
def _MakeResponse(self, number_of_parts):
201212
return http_wrapper.Response(

0 commit comments

Comments
 (0)