From e303f784fbc4fb0af9e8a34885ddc4573e9cdab6 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Tue, 9 Jul 2019 11:14:28 -0700 Subject: [PATCH 1/2] Import batch.md from devsite --- docs/batch.md | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 docs/batch.md diff --git a/docs/batch.md b/docs/batch.md new file mode 100644 index 00000000000..15e2ea68ecc --- /dev/null +++ b/docs/batch.md @@ -0,0 +1,112 @@ +# Batch + +
+

+ Each HTTP connection that your application makes results in a certain amount of overhead. + This library supports batching, + to allow your application to put several API calls into a single HTTP request. + Examples of situations when you might want to use batching: +

+ +

+ Note: You're limited to 1000 calls in a single batch request. + If you need to make more calls than that, use multiple batch requests. +

+

+ Note: You cannot use a + media upload + object in a batch request. +

+
+ +
+

Details

+

+ You create batch requests by calling new_batch_http_request() on your service + object, which returns a + BatchHttpRequest + object, and then calling add() for each request you want to execute. + You may pass in a callback with each request that is called with the response to that request. + The callback function arguments are: + a unique request identifier for each API call, + a response object which contains the API call response, + and an exception object which may be set to an exception raised by the API call. + After you've added the requests, you call execute() to make the requests. + The execute() function blocks until all callbacks have been called. +

+

+ In the following code snippet, + two API requests are batched to a single HTTP request, + and each API request is supplied a callback: +

+
+See below
+

+ You can also supply a single callback that gets called for each response: +

+
See below
+

+ The + 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. +

+
+ +--- + +```py +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) +``` + +```py + +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) From 707816fed30f2cf7c4863e0fc00c031a7909b479 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Tue, 9 Jul 2019 11:28:35 -0700 Subject: [PATCH 2/2] Update batch.md --- docs/batch.md | 114 +++++++++++++++++++++----------------------------- 1 file changed, 48 insertions(+), 66 deletions(-) diff --git a/docs/batch.md b/docs/batch.md index 15e2ea68ecc..b8b31058420 100644 --- a/docs/batch.md +++ b/docs/batch.md @@ -1,76 +1,57 @@ # Batch -
-

- Each HTTP connection that your application makes results in a certain amount of overhead. - This library supports batching, - to allow your application to put several API calls into a single HTTP request. - Examples of situations when you might want to use batching: -

- -

- Note: You're limited to 1000 calls in a single batch request. - If you need to make more calls than that, use multiple batch requests. -

-

- Note: You cannot use a - media upload - object in a batch request. -

-
- -
-

Details

-

- You create batch requests by calling new_batch_http_request() on your service - object, which returns a - BatchHttpRequest - object, and then calling add() for each request you want to execute. - You may pass in a callback with each request that is called with the response to that request. - The callback function arguments are: - a unique request identifier for each API call, - a response object which contains the API call response, - and an exception object which may be set to an exception raised by the API call. - After you've added the requests, you call execute() to make the requests. - The execute() function blocks until all callbacks have been called. -

-

- In the following code snippet, - two API requests are batched to a single HTTP request, - and each API request is supplied a callback: -

+Each HTTP connection that your application makes results in a certain amount of overhead. +This library supports batching, +to allow your application to put several API calls into a single HTTP request. +Examples of situations when you might want to use batching: +* You have many small requests to make and would like to minimize HTTP request overhead. +* A user made changes to data while your application was offline, + so your application needs to synchronize its local data with the server + by sending a lot of updates and deletes. + +**Note**: You're limited to 1000 calls in a single batch request. +If you need to make more calls than that, use multiple batch requests. + +**Note**: You cannot use a +[media upload](/api-client-library/python/guide/media_upload) +object in a batch request. + +## Details +You create batch requests by calling `new_batch_http_request()` on your service +object, which returns a +[BatchHttpRequest](https://google.github.io/google-api-python-client/docs/epy/googleapiclient.http.BatchHttpRequest-class.html) +object, and then calling `add()` for each request you want to execute. +You may pass in a callback with each request that is called with the response to that request. +The callback function arguments are: +a unique request identifier for each API call, +a response object which contains the API call response, +and an exception object which may be set to an exception raised by the API call. +After you've added the requests, you call `execute()` to make the requests. +The `execute()` function blocks until all callbacks have been called. + +In the following code snippet, +two API requests are batched to a single HTTP request, +and each API request is supplied a callback:
 See below
-

- You can also supply a single callback that gets called for each response: -

+You can also supply a single callback that gets called for each response: +
See below
-

- The - 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. -

-
+ +The +[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. + --- -```py +```python def list_animals(request_id, response, exception): if exception is not None: # Do something with the exception @@ -92,7 +73,7 @@ batch.add(service.farmers().list(), callback=list_farmers) batch.execute(http=http) ``` -```py +```python def insert_animal(request_id, response, exception): if exception is not None: @@ -110,3 +91,4 @@ 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) +```