#159 - Excute batch requests using the service's connection

This commit is contained in:
Steven Bazyl 2014-12-16 20:52:57 -08:00
parent 2622ebf8dc
commit d999033fd6
3 changed files with 40 additions and 10 deletions

View File

@ -139,14 +139,17 @@ module Google
# the HTTP response.
def process_http_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)
_, call, callback = @calls.assoc(call_response.call_id)
result = Google::APIClient::Result.new(call, call_response)
callback.call(result) if callback
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

View File

@ -225,7 +225,7 @@ module Google
result = @client.execute(params)
return Google::APIClient::Service::Result.new(request, result)
elsif request.instance_of? Google::APIClient::Service::BatchRequest
@client.execute(request.base_batch)
@client.execute(request.base_batch, {:connection => @connection})
end
end
end

View File

@ -493,6 +493,33 @@ RSpec.describe Google::APIClient::Service::Result do
end
RSpec.describe Google::APIClient::Service::BatchRequest do
include ConnectionHelpers
context 'with a service connection' do
before do
@conn = stub_connection do |stub|
stub.post('/batch') do |env|
[500, {'Content-Type' => 'application/json'}, '{}']
end
end
@discovery = Google::APIClient::Service.new('discovery', 'v1',
{:application_name => APPLICATION_NAME, :authorization => nil,
:cache_store => nil, :connection => @conn})
@calls = [
@discovery.apis.get_rest(:api => 'plus', :version => 'v1'),
@discovery.apis.get_rest(:api => 'discovery', :version => 'v1')
]
end
it 'should use the service connection' do
batch = @discovery.batch(@calls) do
end
batch.execute
@conn.verify
end
end
describe 'with the discovery API' do
before do
@discovery = Google::APIClient::Service.new('discovery', 'v1',
@ -585,7 +612,7 @@ RSpec.describe Google::APIClient::Service::BatchRequest do
batch.execute
expect(call1_returned).to eq(true)
expect(call2_returned).to eq(true)
end
end
end
end
end