Class: Google::APIClient::BatchRequest
- Defined in:
- lib/google/api_client/batch.rb
Overview
Wraps multiple API calls into a single over-the-wire HTTP request.
Constant Summary collapse
- BATCH_BOUNDARY =
"-----------RubyApiBatchRequest".freeze
Constants inherited from Request
Instance Attribute Summary collapse
-
#calls ⇒ Array<(String,Google::APIClient::Request,Proc)] List of API calls in the batch
readonly
private
Array<(String,Google::APIClient::Request,Proc)] List of API calls in the batch.
Attributes inherited from Request
#api_method, #authenticated, #authorization, #body, #headers, #http_method, #media, #parameters, #upload_type, #uri
Instance Method Summary collapse
-
#add(call, call_id = nil, &block) {|Google::APIClient::Result| ... } ⇒ Google::APIClient::BatchRequest
Add a new call to the batch request.
-
#initialize(options = {}, &block) {|Google::APIClient::Result| ... } ⇒ Google::APIClient::BatchRequest
constructor
Creates a new batch request.
-
#process_http_response(response) ⇒ Object
private
Processes the HTTP response to the batch request, issuing callbacks.
-
#to_http_request ⇒ String
private
Return the request body for the BatchRequest’s HTTP request.
Methods inherited from Request
Methods included from Logging
Constructor Details
#initialize(options = {}, &block) {|Google::APIClient::Result| ... } ⇒ Google::APIClient::BatchRequest
Creates a new batch request.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/google/api_client/batch.rb', line 88 def initialize( = {}, &block) @calls = [] @global_callback = nil @global_callback = block if block_given? @last_auto_id = 0 @base_id = SecureRandom.uuid [:uri] ||= 'https://www.googleapis.com/batch' [:http_method] ||= 'POST' super end |
Instance Attribute Details
#calls ⇒ Array<(String,Google::APIClient::Request,Proc)] List of API calls in the batch (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Array<(String,Google::APIClient::Request,Proc)] List of API calls in the batch.
72 73 74 |
# File 'lib/google/api_client/batch.rb', line 72 def calls @calls end |
Instance Method Details
#add(call, call_id = nil, &block) {|Google::APIClient::Result| ... } ⇒ Google::APIClient::BatchRequest
Add a new call to the batch request. Each call must have its own call ID; if not provided, one will automatically be generated, avoiding collisions. If duplicate call IDs are provided, an error will be thrown.
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/google/api_client/batch.rb', line 120 def add(call, call_id = nil, &block) unless call.kind_of?(Google::APIClient::Reference) call = Google::APIClient::Reference.new(call) end call_id ||= new_id if @calls.assoc(call_id) raise BatchError, 'A call with this ID already exists: %s' % call_id end callback = block_given? ? block : @global_callback @calls << [call_id, call, callback] return self end |
#process_http_response(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Processes the HTTP response to the batch request, issuing callbacks.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/google/api_client/batch.rb', line 141 def process_http_response(response) content_type = find_header('Content-Type', response.headers) m = /.*boundary=(.+)/.match(content_type) if m boundary = m[1] parts = response.body.split(/--#{Regexp.escape(boundary)}/) parts = parts[1...-1] parts.each do |part| call_response = deserialize_call_response(part) _, call, callback = @calls.assoc(call_response.call_id) result = Google::APIClient::Result.new(call, call_response) callback.call(result) if callback end end Google::APIClient::Result.new(self, response) end |
#to_http_request ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the request body for the BatchRequest’s HTTP request.
165 166 167 168 169 170 171 172 |
# File 'lib/google/api_client/batch.rb', line 165 def to_http_request if @calls.nil? || @calls.empty? raise BatchError, 'Cannot make an empty batch request' end parts = @calls.map {|(call_id, call, _callback)| serialize_call(call_id, call)} build_multipart(parts, 'multipart/mixed', BATCH_BOUNDARY) super end |