Add a new request. Every callback added will be paired with a unique id, the request_id. That unique id will be passed back to the callback when the response comes back from the server. The default behavior is to have the library generate it's own unique id. If the c
(self, request, callback=None, request_id=None)
| 1409 | |
| 1410 | @util.positional(2) |
| 1411 | def add(self, request, callback=None, request_id=None): |
| 1412 | """Add a new request. |
| 1413 | |
| 1414 | Every callback added will be paired with a unique id, the request_id. That |
| 1415 | unique id will be passed back to the callback when the response comes back |
| 1416 | from the server. The default behavior is to have the library generate it's |
| 1417 | own unique id. If the caller passes in a request_id then they must ensure |
| 1418 | uniqueness for each request_id, and if they are not an exception is |
| 1419 | raised. Callers should either supply all request_ids or never supply a |
| 1420 | request id, to avoid such an error. |
| 1421 | |
| 1422 | Args: |
| 1423 | request: HttpRequest, Request to add to the batch. |
| 1424 | callback: callable, A callback to be called for this response, of the |
| 1425 | form callback(id, response, exception). The first parameter is the |
| 1426 | request id, and the second is the deserialized response object. The |
| 1427 | third is an googleapiclient.errors.HttpError exception object if an HTTP error |
| 1428 | occurred while processing the request, or None if no errors occurred. |
| 1429 | request_id: string, A unique id for the request. The id will be passed |
| 1430 | to the callback with the response. |
| 1431 | |
| 1432 | Returns: |
| 1433 | None |
| 1434 | |
| 1435 | Raises: |
| 1436 | BatchError if a media request is added to a batch. |
| 1437 | KeyError is the request_id is not unique. |
| 1438 | """ |
| 1439 | |
| 1440 | if len(self._order) >= MAX_BATCH_LIMIT: |
| 1441 | raise BatchError( |
| 1442 | "Exceeded the maximum calls(%d) in a single batch request." |
| 1443 | % MAX_BATCH_LIMIT |
| 1444 | ) |
| 1445 | if request_id is None: |
| 1446 | request_id = self._new_id() |
| 1447 | if request.resumable is not None: |
| 1448 | raise BatchError("Media requests cannot be used in a batch request.") |
| 1449 | if request_id in self._requests: |
| 1450 | raise KeyError("A request with this ID already exists: %s" % request_id) |
| 1451 | self._requests[request_id] = request |
| 1452 | self._callbacks[request_id] = callback |
| 1453 | self._order.append(request_id) |
| 1454 | |
| 1455 | def _execute(self, http, order, requests): |
| 1456 | """Serialize batch request, send to server, process response. |