Class: Google::APIClient::BatchRequest
- Inherits:
-
Object
- Object
- 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
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#calls ⇒ Object
readonly
Returns the value of attribute calls.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#add(call, call_id = nil, &block) ⇒ Google::APIClient::BatchRequest
Add a new call to the batch request.
-
#initialize(options = {}, &block) ⇒ Google::APIClient::BatchRequest
constructor
Creates a new batch request.
-
#process_response(response) ⇒ Object
Processes the HTTP response to the batch request, issuing callbacks.
-
#to_http_request ⇒ Array<String, String, Hash, String>
Convert this batch request into an HTTP request.
Constructor Details
#initialize(options = {}, &block) ⇒ Google::APIClient::BatchRequest
Creates a new batch request.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/google/api_client/batch.rb', line 51 def initialize( = {}, &block) # Request options, ignoring method and parameters. @options = # Batched calls to be made, indexed by call ID. @calls = {} # Callbacks per batched call, indexed by call ID. @callbacks = {} # Order for the call IDs, since Ruby 1.8 hashes are unordered. @order = [] # Global callback to be used for every call. If a specific callback # has been defined for a request, this won't be called. @global_callback = block if block_given? # The last auto generated ID. @last_auto_id = 0 # Base ID for the batch request. @base_id = nil end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
38 39 40 |
# File 'lib/google/api_client/batch.rb', line 38 def callbacks @callbacks end |
#calls ⇒ Object (readonly)
Returns the value of attribute calls.
38 39 40 |
# File 'lib/google/api_client/batch.rb', line 38 def calls @calls end |
#options ⇒ Object
Returns the value of attribute options.
37 38 39 |
# File 'lib/google/api_client/batch.rb', line 37 def @options end |
Instance Method Details
#add(call, call_id = nil, &block) ⇒ 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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/google/api_client/batch.rb', line 80 def add(call, call_id = nil, &block) unless call.kind_of?(Google::APIClient::Reference) call = Google::APIClient::Reference.new(call) end if call_id.nil? call_id = new_id end if @calls.include?(call_id) raise BatchError, 'A call with this ID already exists: %s' % call_id end @calls[call_id] = call @order << call_id if block_given? @callbacks[call_id] = block elsif @global_callback @callbacks[call_id] = @global_callback end return self end |
#process_response(response) ⇒ Object
Processes the HTTP response to the batch request, issuing callbacks.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/google/api_client/batch.rb', line 115 def process_response(response) content_type = find_header('Content-Type', response.headers) boundary = /.*boundary=(.+)/.match(content_type)[1] parts = response.body.split(/--#{Regexp.escape(boundary)}/) parts = parts[1...-1] parts.each do |part| call_response = deserialize_call_response(part) callback = @callbacks[call_response.call_id] call = @calls[call_response.call_id] result = Google::APIClient::Result.new(call, nil, call_response) callback.call(result) if callback end end |
#to_http_request ⇒ Array<String, String, Hash, String>
Convert this batch request into an HTTP request.
107 108 109 |
# File 'lib/google/api_client/batch.rb', line 107 def to_http_request return ['POST', request_uri, request_headers, request_body] end |