Class: Google::APIClient::BatchRequest

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

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

Creates a new batch request.

Parameters:

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

    Set of options for this request, the only important one being :connection, which specifies an HTTP connection to use.

  • block (Proc)

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



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(options = {}, &block)
  # Request options, ignoring method and parameters.
  @options = 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

#callbacksObject (readonly)

Returns the value of attribute callbacks.



38
39
40
# File 'lib/google/api_client/batch.rb', line 38

def callbacks
  @callbacks
end

#callsObject (readonly)

Returns the value of attribute calls.



38
39
40
# File 'lib/google/api_client/batch.rb', line 38

def calls
  @calls
end

#optionsObject

Returns the value of attribute options.



37
38
39
# File 'lib/google/api_client/batch.rb', line 37

def options
  @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.

Parameters:

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

    the call to be added.

  • call_id: (String)

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

  • block: (Proc)

    callback for this call’s response.

Returns:



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.

Parameters:

  • response: (Faraday::Response)

    the HTTP response.



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_requestArray<String, String, Hash, String>

Convert this batch request into an HTTP request.

Returns:

  • (Array<String, String, Hash, String>)

    An array consisting of, in order: HTTP method, request path, request headers and request body.



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