Adding batch support to new service interface
This commit is contained in:
1 parent
00f66633e0
commit
83d411990c
3 files changed
+239
-18
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user