Add client-side limit for batch requests (#585)


diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index 9e549de..d62a5c1 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -73,6 +73,8 @@
 
 MAX_URI_LENGTH = 2048
 
+MAX_BATCH_LIMIT = 1000
+
 _TOO_MANY_REQUESTS = 429
 
 DEFAULT_HTTP_TIMEOUT_SEC = 60
@@ -1322,6 +1324,10 @@
       BatchError if a media request is added to a batch.
       KeyError is the request_id is not unique.
     """
+
+    if len(self._order) >= MAX_BATCH_LIMIT:
+      raise BatchError("Exceeded the maximum calls(%d) in a single bactch request."
+                       % MAX_BATCH_LIMIT)
     if request_id is None:
       request_id = self._new_id()
     if request.resumable is not None:
diff --git a/tests/test_http.py b/tests/test_http.py
index e381294..3e23ed4 100644
--- a/tests/test_http.py
+++ b/tests/test_http.py
@@ -1192,6 +1192,21 @@
     batch.add(self.request1, request_id='1')
     self.assertRaises(KeyError, batch.add, self.request1, request_id='1')
 
+  def test_add_fail_for_over_limit(self):
+    from googleapiclient.http import MAX_BATCH_LIMIT
+
+    batch = BatchHttpRequest()
+    for i in xrange(0, MAX_BATCH_LIMIT):
+      batch.add(HttpRequest(
+        None,
+        None,
+        'https://www.googleapis.com/someapi/v1/collection/?foo=bar',
+        method='POST',
+        body='{}',
+        headers={'content-type': 'application/json'})
+      )
+    self.assertRaises(BatchError, batch.add, self.request1)
+
   def test_add_fail_for_resumable(self):
     batch = BatchHttpRequest()