Class: Google::APIClient::BatchRequest

Inherits:
Request
  • Object
show all
Defined in:
lib/google/api_client/batch.rb

Overview

Wraps multiple API calls into a single over-the-wire HTTP request.

Examples:


client = Google::APIClient.new
urlshortener = client.discovered_api('urlshortener')
batch = Google::APIClient::BatchRequest.new do |result|
   puts result.data
end

batch.add(:api_method => urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/foo' })
batch.add(:api_method => urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/bar' })

client.execute(batch)

Constant Summary collapse

BATCH_BOUNDARY =
"-----------RubyApiBatchRequest".freeze

Constants inherited from Request

Request::MULTIPART_BOUNDARY

Instance Attribute Summary collapse

Attributes inherited from Request

#api_method, #authenticated, #authorization, #body, #headers, #http_method, #media, #parameters, #upload_type, #uri

Instance Method Summary collapse

Methods inherited from Request

#send, #to_env, #to_hash

Methods included from Logging

#logger

Constructor Details

#initialize(options = {}, &block) {|Google::APIClient::Result| ... } ⇒ Google::APIClient::BatchRequest

Creates a new batch request.

Parameters:

  • options (Hash) (defaults to: {})

    Set of options for this request.

  • block (Proc)

    Callback for every call’s response. Won’t be called if a call defined a callback of its own.

Yields:



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/google/api_client/batch.rb', line 88

def initialize(options = {}, &block)
  @calls = []
  @global_callback = nil
  @global_callback = block if block_given?
  @last_auto_id = 0

  @base_id = SecureRandom.uuid

  options[:uri] ||= 'https://www.googleapis.com/batch'
  options[:http_method] ||= 'POST'

  super options
end

Instance Attribute Details

#callsArray<(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.

Returns:



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.

Parameters:

  • call (Hash, Google::APIClient::Request)

    the call to be added.

  • call_id (String) (defaults to: nil)

    the ID to be used for this call. Must be unique

  • block (Proc)

    callback for this call’s response.

Yields:

Returns:



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.

Parameters:

  • response (Faraday::Response)

    the HTTP response.



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_requestString

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.

Returns:

  • (String)

    the request body.



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