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:

  • 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. Many APIs enforce stricter quotas in addition to that limit. If you need to make more calls than an API allows, use multiple batch requests.

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

Details

Create batch requests by calling the batch method and supply a block. All calls made within the block are batched together.

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

zoo = Google::Apis::ZooV1::ZooService.new
zoo.batch do |zoo|
  zoo.get_animal('elephant') do |animal, err|
    # do something with the elephant
  end
  zoo.list_animals() do |animals, err|
    # do something with the list of animals
  end
end

Note that when calling methods in batch a block must be supplied to handle the result and potential error. You can also supply a single callback that gets called for each response:

zoo = Google::Apis::ZooV1::ZooService.new
callback = lambda { |animal, err| ... }
zoo.batch do |zoo|
  zoo.get_animal('elephant', &callback)
  zoo.get_animal('porcupine', &callback)
  zoo.get_animal('swan', &callback)
end