Skip to content

Commit e2dccec

Browse files
tristan-lintheacodes
authored andcommitted
Add client-side limit for batch requests (#585)
1 parent fef743a commit e2dccec

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

googleapiclient/http.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373

7474
MAX_URI_LENGTH = 2048
7575

76+
MAX_BATCH_LIMIT = 1000
77+
7678
_TOO_MANY_REQUESTS = 429
7779

7880
DEFAULT_HTTP_TIMEOUT_SEC = 60
@@ -1322,6 +1324,10 @@ def add(self, request, callback=None, request_id=None):
13221324
BatchError if a media request is added to a batch.
13231325
KeyError is the request_id is not unique.
13241326
"""
1327+
1328+
if len(self._order) >= MAX_BATCH_LIMIT:
1329+
raise BatchError("Exceeded the maximum calls(%d) in a single bactch request."
1330+
% MAX_BATCH_LIMIT)
13251331
if request_id is None:
13261332
request_id = self._new_id()
13271333
if request.resumable is not None:

tests/test_http.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,21 @@ def test_add(self):
11921192
batch.add(self.request1, request_id='1')
11931193
self.assertRaises(KeyError, batch.add, self.request1, request_id='1')
11941194

1195+
def test_add_fail_for_over_limit(self):
1196+
from googleapiclient.http import MAX_BATCH_LIMIT
1197+
1198+
batch = BatchHttpRequest()
1199+
for i in xrange(0, MAX_BATCH_LIMIT):
1200+
batch.add(HttpRequest(
1201+
None,
1202+
None,
1203+
'https://www.googleapis.com/someapi/v1/collection/?foo=bar',
1204+
method='POST',
1205+
body='{}',
1206+
headers={'content-type': 'application/json'})
1207+
)
1208+
self.assertRaises(BatchError, batch.add, self.request1)
1209+
11951210
def test_add_fail_for_resumable(self):
11961211
batch = BatchHttpRequest()
11971212

0 commit comments

Comments
 (0)