1 # Copyright 2013 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 require 'google/api_client/service/result'
16 require 'google/api_client/batch'
23 # Helper class to contain the result of an individual batched call.
25 class BatchedCallResult < Result
26 # @return [Fixnum] Index of the call
28 return @base_result.response.call_id.to_i - 1
37 # Creates a new batch request.
38 # This class shouldn't be instantiated directly, but rather through
41 # @param [Array] calls
42 # List of Google::APIClient::Service::Request to be made.
44 # Callback for every call's response. Won't be called if a call
45 # defined a callback of its own.
47 # @yield [Google::APIClient::Service::Result]
48 # block to be called when result ready
49 def initialize(service, calls, &block)
51 @base_batch = Google::APIClient::BatchRequest.new
52 @global_callback = block if block_given?
54 if calls && calls.length > 0
62 # Add a new call to the batch request.
64 # @param [Google::APIClient::Service::Request] call
65 # the call to be added.
67 # callback for this call's response.
69 # @return [Google::APIClient::Service::BatchRequest]
70 # the BatchRequest, for chaining
72 # @yield [Google::APIClient::Service::Result]
73 # block to be called when result ready
75 if !block_given? && @global_callback.nil?
76 raise BatchError, 'Request needs a block'
78 callback = block || @global_callback
80 :api_method => call.method,
81 :parameters => call.parameters
83 if call.respond_to? :body
84 if call.body.respond_to? :to_hash
85 base_call[:body_object] = call.body
87 base_call[:body] = call.body
90 @base_batch.add(base_call) do |base_result|
91 result = Google::APIClient::Service::BatchedCallResult.new(
99 # Executes the batch request.
101 @service.execute(self)
104 attr_reader :base_batch