| [ Web Proxy ] |
| Viewing: https://raw.githubusercontent.com/nloadholtes/google-api-python-client/master/docs/batch.md | [Back] [Original] |
See belowYou can also supply a single callback that gets called for each response:
See belowThe [add()](https://google.github.io/google-api-python-client/docs/epy/googleapiclient.http.BatchHttpRequest-class.html#add) method also allows you to supply a
request_id parameter for each request.
These IDs are provided to the callbacks.
If you don't supply one, the library creates one for you.
The IDs must be unique for each API request,
otherwise `add()` raises an exception.
If you supply a callback to both `new_batch_http_request()` and `add()`, they both get called.
---
```python
def list_animals(request_id, response, exception):
if exception is not None:
# Do something with the exception
pass
else:
# Do something with the response
pass
def list_farmers(request_id, response):
"""Do something with the farmers list response."""
pass
service = build('farm', 'v2')
batch = service.new_batch_http_request()
batch.add(service.animals().list(), callback=list_animals)
batch.add(service.farmers().list(), callback=list_farmers)
batch.execute(http=http)
```
```python
def insert_animal(request_id, response, exception):
if exception is not None:
# Do something with the exception
pass
else:
# Do something with the response
pass
service = build('farm', 'v2')
batch = service.new_batch_http_request(callback=insert_animal)
batch.add(service.animals().insert(name="sheep"))
batch.add(service.animals().insert(name="pig"))
batch.add(service.animals().insert(name="llama"))
batch.execute(http=http)
```
| Web Proxy Viewer | New URL | Original Page |