Skip to content

Commit d999033

Browse files
committed
googleapis#159 - Excute batch requests using the service's connection
1 parent 2622ebf commit d999033

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

lib/google/api_client/batch.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,17 @@ def add(call, call_id = nil, &block)
139139
# the HTTP response.
140140
def process_http_response(response)
141141
content_type = find_header('Content-Type', response.headers)
142-
boundary = /.*boundary=(.+)/.match(content_type)[1]
143-
parts = response.body.split(/--#{Regexp.escape(boundary)}/)
144-
parts = parts[1...-1]
145-
parts.each do |part|
146-
call_response = deserialize_call_response(part)
147-
_, call, callback = @calls.assoc(call_response.call_id)
148-
result = Google::APIClient::Result.new(call, call_response)
149-
callback.call(result) if callback
142+
m = /.*boundary=(.+)/.match(content_type)
143+
if m
144+
boundary = m[1]
145+
parts = response.body.split(/--#{Regexp.escape(boundary)}/)
146+
parts = parts[1...-1]
147+
parts.each do |part|
148+
call_response = deserialize_call_response(part)
149+
_, call, callback = @calls.assoc(call_response.call_id)
150+
result = Google::APIClient::Result.new(call, call_response)
151+
callback.call(result) if callback
152+
end
150153
end
151154
Google::APIClient::Result.new(self, response)
152155
end

lib/google/api_client/service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def execute(request)
225225
result = @client.execute(params)
226226
return Google::APIClient::Service::Result.new(request, result)
227227
elsif request.instance_of? Google::APIClient::Service::BatchRequest
228-
@client.execute(request.base_batch)
228+
@client.execute(request.base_batch, {:connection => @connection})
229229
end
230230
end
231231
end

spec/google/api_client/service_spec.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,33 @@
493493
end
494494

495495
RSpec.describe Google::APIClient::Service::BatchRequest do
496+
497+
include ConnectionHelpers
498+
499+
context 'with a service connection' do
500+
before do
501+
@conn = stub_connection do |stub|
502+
stub.post('/batch') do |env|
503+
[500, {'Content-Type' => 'application/json'}, '{}']
504+
end
505+
end
506+
@discovery = Google::APIClient::Service.new('discovery', 'v1',
507+
{:application_name => APPLICATION_NAME, :authorization => nil,
508+
:cache_store => nil, :connection => @conn})
509+
@calls = [
510+
@discovery.apis.get_rest(:api => 'plus', :version => 'v1'),
511+
@discovery.apis.get_rest(:api => 'discovery', :version => 'v1')
512+
]
513+
end
514+
515+
it 'should use the service connection' do
516+
batch = @discovery.batch(@calls) do
517+
end
518+
batch.execute
519+
@conn.verify
520+
end
521+
end
522+
496523
describe 'with the discovery API' do
497524
before do
498525
@discovery = Google::APIClient::Service.new('discovery', 'v1',
@@ -585,7 +612,7 @@
585612
batch.execute
586613
expect(call1_returned).to eq(true)
587614
expect(call2_returned).to eq(true)
588-
end
615+
end
589616
end
590617
end
591618
end

0 commit comments

Comments
 (0)