Adding batch support to new service interface

This commit is contained in:
Sergio Gomes 2013-10-04 15:56:06 +01:00
parent 00f66633e0
commit 83d411990c
3 changed files with 239 additions and 18 deletions

View File

@ -17,6 +17,7 @@ require 'google/api_client/service/stub_generator'
require 'google/api_client/service/resource'
require 'google/api_client/service/request'
require 'google/api_client/service/result'
require 'google/api_client/service/batch'
module Google
class APIClient
@ -150,34 +151,54 @@ module Google
# @return [Faraday::Connection]
attr_accessor :connection
##
# Prepares a Google::APIClient::BatchRequest object to make batched calls.
# @param [Array] calls
# Optional array of Google::APIClient::Service::Request to initialize
# the batch request with.
# @param [Proc] block
# Callback for every call's response. Won't be called if a call defined
# a callback of its own.
#
# @yield [Google::APIClient::Service::Result]
# block to be called when result ready
def batch(calls = nil, &block)
Google::APIClient::Service::BatchRequest.new(self, calls, &block)
end
##
# Executes an API request.
# Do not call directly; this method is only used by Request objects when
# executing.
#
# @param [Google::APIClient::Service::Request] request
# @param [Google::APIClient::Service::Request,
# Google::APIClient::Service::BatchCall] request
# The request to be executed.
def execute(request)
params = {:api_method => request.method,
:parameters => request.parameters,
:connection => @connection}
if request.respond_to? :body
if request.body.respond_to? :to_hash
params[:body_object] = request.body
else
params[:body] = request.body
if request.instance_of? Google::APIClient::Service::Request
params = {:api_method => request.method,
:parameters => request.parameters,
:connection => @connection}
if request.respond_to? :body
if request.body.respond_to? :to_hash
params[:body_object] = request.body
else
params[:body] = request.body
end
end
end
if request.respond_to? :media
params[:media] = request.media
end
[:authenticated, :gzip].each do |option|
if @options.include? option
params[option] = @options[option]
if request.respond_to? :media
params[:media] = request.media
end
[:authenticated, :gzip].each do |option|
if @options.include? option
params[option] = @options[option]
end
end
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)
end
result = @client.execute(params)
return Google::APIClient::Service::Result.new(request, result)
end
end
end

View File

@ -0,0 +1,103 @@
# Copyright 2013 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'google/api_client/service/result'
require 'google/api_client/batch'
module Google
class APIClient
class Service
##
# Helper class to contain the result of an individual batched call.
#
class BatchedCallResult < Result
# @return [String] UUID of the call
def call_index
return @base_result.response.call_id.to_i - 1
end
end
##
#
#
class BatchRequest
##
# Creates a new batch request.
# This class shouldn't be instantiated directly, but rather through
# Service.batch.
#
# @param [Array] calls
# List of Google::APIClient::Service::Request to be made.
# @param [Proc] block
# Callback for every call's response. Won't be called if a call
# defined a callback of its own.
#
# @yield [Google::APIClient::Service::Result]
# block to be called when result ready
def initialize(service, calls, &block)
@service = service
@base_batch = Google::APIClient::BatchRequest.new
@global_callback = block if block_given?
if calls && calls.length > 0
calls.each do |call|
add(call)
end
end
end
##
# Add a new call to the batch request.
#
# @param [Google::APIClient::Service::Request] call
# the call to be added.
# @param [Proc] block
# callback for this call's response.
#
# @return [Google::APIClient::Service::BatchRequest]
# the BatchRequest, for chaining
#
# @yield [Google::APIClient::Service::Result]
# block to be called when result ready
def add(call, &block)
if !block_given? && @global_callback.nil?
raise BatchError, 'Request needs a block'
end
callback = block || @global_callback
base_call = {
:api_method => call.method,
:parameters => call.parameters
}
@base_batch.add(base_call) do |base_result|
result = Google::APIClient::Service::BatchedCallResult.new(
call, base_result)
callback.call(result)
end
return self
end
##
# Executes the batch request.
def execute
@service.execute(self)
end
attr_reader :base_batch
end
end
end
end

View File

@ -473,3 +473,100 @@ describe Google::APIClient::Service::Result do
end
end
end
describe Google::APIClient::Service::BatchRequest do
describe 'with the discovery API' do
before do
@discovery = Google::APIClient::Service.new('discovery', 'v1',
{:application_name => APPLICATION_NAME, :authorization => nil})
end
describe 'with two valid requests' do
before do
@calls = [
@discovery.apis.get_rest(:api => 'plus', :version => 'v1'),
@discovery.apis.get_rest(:api => 'discovery', :version => 'v1')
]
end
it 'should execute both when using a global callback' do
block_called = 0
batch = @discovery.batch(@calls) do |result|
block_called += 1
result.status.should == 200
end
batch.execute
block_called.should == 2
end
it 'should execute both when using individual callbacks' do
call1_returned, call2_returned = false, false
batch = @discovery.batch
batch.add(@calls[0]) do |result|
call1_returned = true
result.status.should == 200
result.call_index.should == 0
end
batch.add(@calls[1]) do |result|
call2_returned = true
result.status.should == 200
result.call_index.should == 1
end
batch.execute
call1_returned.should == true
call2_returned.should == true
end
end
describe 'with a valid request and an invalid one' do
before do
@calls = [
@discovery.apis.get_rest(:api => 'plus', :version => 'v1'),
@discovery.apis.get_rest(:api => 'invalid', :version => 'invalid')
]
end
it 'should execute both when using a global callback' do
block_called = 0
batch = @discovery.batch(@calls) do |result|
block_called += 1
if result.call_index == 0
result.status.should == 200
else
result.status.should >= 400
result.status.should < 500
end
end
batch.execute
block_called.should == 2
end
it 'should execute both when using individual callbacks' do
call1_returned, call2_returned = false, false
batch = @discovery.batch
batch.add(@calls[0]) do |result|
call1_returned = true
result.status.should == 200
result.call_index.should == 0
end
batch.add(@calls[1]) do |result|
call2_returned = true
result.status.should >= 400
result.status.should < 500
result.call_index.should == 1
end
batch.execute
call1_returned.should == true
call2_returned.should == true
end
end
end
end