Skip to content

Commit 83d4119

Browse files
author
Sergio Gomes
committed
Adding batch support to new service interface
1 parent 00f6663 commit 83d4119

File tree

3 files changed

+239
-18
lines changed

3 files changed

+239
-18
lines changed

lib/google/api_client/service.rb

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require 'google/api_client/service/resource'
1818
require 'google/api_client/service/request'
1919
require 'google/api_client/service/result'
20+
require 'google/api_client/service/batch'
2021

2122
module Google
2223
class APIClient
@@ -150,34 +151,54 @@ def initialize(api_name, api_version, options = {})
150151
# @return [Faraday::Connection]
151152
attr_accessor :connection
152153

154+
##
155+
# Prepares a Google::APIClient::BatchRequest object to make batched calls.
156+
# @param [Array] calls
157+
# Optional array of Google::APIClient::Service::Request to initialize
158+
# the batch request with.
159+
# @param [Proc] block
160+
# Callback for every call's response. Won't be called if a call defined
161+
# a callback of its own.
162+
#
163+
# @yield [Google::APIClient::Service::Result]
164+
# block to be called when result ready
165+
def batch(calls = nil, &block)
166+
Google::APIClient::Service::BatchRequest.new(self, calls, &block)
167+
end
168+
153169
##
154170
# Executes an API request.
155171
# Do not call directly; this method is only used by Request objects when
156172
# executing.
157173
#
158-
# @param [Google::APIClient::Service::Request] request
174+
# @param [Google::APIClient::Service::Request,
175+
# Google::APIClient::Service::BatchCall] request
159176
# The request to be executed.
160177
def execute(request)
161-
params = {:api_method => request.method,
162-
:parameters => request.parameters,
163-
:connection => @connection}
164-
if request.respond_to? :body
165-
if request.body.respond_to? :to_hash
166-
params[:body_object] = request.body
167-
else
168-
params[:body] = request.body
178+
if request.instance_of? Google::APIClient::Service::Request
179+
params = {:api_method => request.method,
180+
:parameters => request.parameters,
181+
:connection => @connection}
182+
if request.respond_to? :body
183+
if request.body.respond_to? :to_hash
184+
params[:body_object] = request.body
185+
else
186+
params[:body] = request.body
187+
end
169188
end
170-
end
171-
if request.respond_to? :media
172-
params[:media] = request.media
173-
end
174-
[:authenticated, :gzip].each do |option|
175-
if @options.include? option
176-
params[option] = @options[option]
189+
if request.respond_to? :media
190+
params[:media] = request.media
191+
end
192+
[:authenticated, :gzip].each do |option|
193+
if @options.include? option
194+
params[option] = @options[option]
195+
end
177196
end
197+
result = @client.execute(params)
198+
return Google::APIClient::Service::Result.new(request, result)
199+
elsif request.instance_of? Google::APIClient::Service::BatchRequest
200+
@client.execute(request.base_batch)
178201
end
179-
result = @client.execute(params)
180-
return Google::APIClient::Service::Result.new(request, result)
181202
end
182203
end
183204
end
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Copyright 2013 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'google/api_client/service/result'
16+
require 'google/api_client/batch'
17+
18+
module Google
19+
class APIClient
20+
class Service
21+
22+
##
23+
# Helper class to contain the result of an individual batched call.
24+
#
25+
class BatchedCallResult < Result
26+
# @return [String] UUID of the call
27+
def call_index
28+
return @base_result.response.call_id.to_i - 1
29+
end
30+
end
31+
32+
##
33+
#
34+
#
35+
class BatchRequest
36+
##
37+
# Creates a new batch request.
38+
# This class shouldn't be instantiated directly, but rather through
39+
# Service.batch.
40+
#
41+
# @param [Array] calls
42+
# List of Google::APIClient::Service::Request to be made.
43+
# @param [Proc] block
44+
# Callback for every call's response. Won't be called if a call
45+
# defined a callback of its own.
46+
#
47+
# @yield [Google::APIClient::Service::Result]
48+
# block to be called when result ready
49+
def initialize(service, calls, &block)
50+
@service = service
51+
@base_batch = Google::APIClient::BatchRequest.new
52+
@global_callback = block if block_given?
53+
54+
if calls && calls.length > 0
55+
calls.each do |call|
56+
add(call)
57+
end
58+
end
59+
end
60+
61+
##
62+
# Add a new call to the batch request.
63+
#
64+
# @param [Google::APIClient::Service::Request] call
65+
# the call to be added.
66+
# @param [Proc] block
67+
# callback for this call's response.
68+
#
69+
# @return [Google::APIClient::Service::BatchRequest]
70+
# the BatchRequest, for chaining
71+
#
72+
# @yield [Google::APIClient::Service::Result]
73+
# block to be called when result ready
74+
def add(call, &block)
75+
if !block_given? && @global_callback.nil?
76+
raise BatchError, 'Request needs a block'
77+
end
78+
callback = block || @global_callback
79+
base_call = {
80+
:api_method => call.method,
81+
:parameters => call.parameters
82+
}
83+
@base_batch.add(base_call) do |base_result|
84+
result = Google::APIClient::Service::BatchedCallResult.new(
85+
call, base_result)
86+
callback.call(result)
87+
end
88+
return self
89+
end
90+
91+
##
92+
# Executes the batch request.
93+
def execute
94+
@service.execute(self)
95+
end
96+
97+
attr_reader :base_batch
98+
99+
end
100+
101+
end
102+
end
103+
end

spec/google/api_client/service_spec.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,100 @@
473473
end
474474
end
475475
end
476+
477+
describe Google::APIClient::Service::BatchRequest do
478+
describe 'with the discovery API' do
479+
before do
480+
@discovery = Google::APIClient::Service.new('discovery', 'v1',
481+
{:application_name => APPLICATION_NAME, :authorization => nil})
482+
end
483+
484+
describe 'with two valid requests' do
485+
before do
486+
@calls = [
487+
@discovery.apis.get_rest(:api => 'plus', :version => 'v1'),
488+
@discovery.apis.get_rest(:api => 'discovery', :version => 'v1')
489+
]
490+
end
491+
492+
it 'should execute both when using a global callback' do
493+
block_called = 0
494+
batch = @discovery.batch(@calls) do |result|
495+
block_called += 1
496+
result.status.should == 200
497+
end
498+
499+
batch.execute
500+
block_called.should == 2
501+
end
502+
503+
it 'should execute both when using individual callbacks' do
504+
call1_returned, call2_returned = false, false
505+
batch = @discovery.batch
506+
507+
batch.add(@calls[0]) do |result|
508+
call1_returned = true
509+
result.status.should == 200
510+
result.call_index.should == 0
511+
end
512+
513+
batch.add(@calls[1]) do |result|
514+
call2_returned = true
515+
result.status.should == 200
516+
result.call_index.should == 1
517+
end
518+
519+
batch.execute
520+
call1_returned.should == true
521+
call2_returned.should == true
522+
end
523+
end
524+
525+
describe 'with a valid request and an invalid one' do
526+
before do
527+
@calls = [
528+
@discovery.apis.get_rest(:api => 'plus', :version => 'v1'),
529+
@discovery.apis.get_rest(:api => 'invalid', :version => 'invalid')
530+
]
531+
end
532+
533+
it 'should execute both when using a global callback' do
534+
block_called = 0
535+
batch = @discovery.batch(@calls) do |result|
536+
block_called += 1
537+
if result.call_index == 0
538+
result.status.should == 200
539+
else
540+
result.status.should >= 400
541+
result.status.should < 500
542+
end
543+
end
544+
545+
batch.execute
546+
block_called.should == 2
547+
end
548+
549+
it 'should execute both when using individual callbacks' do
550+
call1_returned, call2_returned = false, false
551+
batch = @discovery.batch
552+
553+
batch.add(@calls[0]) do |result|
554+
call1_returned = true
555+
result.status.should == 200
556+
result.call_index.should == 0
557+
end
558+
559+
batch.add(@calls[1]) do |result|
560+
call2_returned = true
561+
result.status.should >= 400
562+
result.status.should < 500
563+
result.call_index.should == 1
564+
end
565+
566+
batch.execute
567+
call1_returned.should == true
568+
call2_returned.should == true
569+
end
570+
end
571+
end
572+
end

0 commit comments

Comments
 (0)