- Improving the Service interface with access to more properties

- Adding end-to-end spec
- Fixing bugs
This commit is contained in:
Sergio Gomes 2013-09-11 16:51:10 +01:00
parent e73ce124fa
commit 1e2405093b
2 changed files with 102 additions and 57 deletions

View File

@ -30,6 +30,10 @@ module Google
# result = calendar.events.list('calendarId' => 'primary').execute()
class Service
include Google::APIClient::Service::StubGenerator
extend Forwardable
# Cache for discovered APIs.
@@discovered = {}
##
# Creates a new Service.
@ -74,7 +78,7 @@ module Google
# authenticated, `false` otherwise.
# @option options [TrueClass, FalseClass] :gzip (default: true)
# `true` if gzip enabled, `false` otherwise.
# @option options [Faraday] :connection
# @option options [Faraday::Connection] :connection
# A custom connection to be used for all requests.
def initialize(api_name, api_version, options = {})
@api_name = api_name.to_s
@ -98,35 +102,65 @@ module Google
end
@client = Google::APIClient.new(params)
@client.logger = options[:logger] if options.include? :logger
@connection = options[:connection] || @client.connection
@options = options
@api = @client.discovered_api(api_name, api_version)
# Cache discovered APIs in memory.
# Not thread-safe, but the worst that can happen is a cache miss.
unless @api = @@discovered[[api_name, api_version]]
@@discovered[[api_name, api_version]] = @api = @client.discovered_api(
api_name, api_version)
end
generate_call_stubs(self, @api)
end
##
# Logger for the Service.
#
# @return [Logger] logger instance.
def logger
@client.logger
end
# @return [Logger]
# The logger instance.
def_delegators :@client, :logger, :logger=
##
# Set the Logger for the Service.
def logger=(obj)
@client.logger = obj
end
# Returns the authorization mechanism used by the service.
#
# @return [#generate_authenticated_request] The authorization mechanism.
def_delegators :@client, :authorization, :authorization=
##
# The setting that controls whether or not the service attempts to
# refresh authorization when a 401 is hit during an API call.
#
# @return [Boolean]
def_delegators :@client, :auto_refresh_token, :auto_refresh_token=
##
# The application's API key issued by the API console.
#
# @return [String] The API key.
def_delegators :@client, :key, :key=
##
# The Faraday/HTTP connection used by this service.
#
# @return [Faraday::Connection]
attr_accessor :connection
##
# Executes an API request.
# Do not call directly; this method is only used by Request objects when
# executing.
#
# @param [Google::APIClient::Service::Request] request
# The request to be executed.
def execute(request)
params = {:api_method => request.method,
:parameters => request.parameters}
:parameters => request.parameters,
:connection => @connection}
if request.respond_to? :body
if request.body.respond_to? :to_hash
params[:body_object] = request.body
@ -137,13 +171,13 @@ module Google
if request.respond_to? :media
params[:media] = request.media
end
[:connection, :authenticated, :gzip].each do |option|
[:authenticated, :gzip].each do |option|
if @options.include? option
params[option] = @options[option]
end
end
result = @client.execute(params)
return Google::APIClient::Result.new(request, result)
return Google::APIClient::Service::Result.new(request, result)
end
end
end

View File

@ -261,6 +261,17 @@ describe Google::APIClient::Service do
end
end
end
describe 'with the Discovery API' do
it 'should make a valid end-to-end request' do
discovery = Google::APIClient::Service.new('discovery', 'v1',
{:application_name => APPLICATION_NAME, :authenticated => false})
result = discovery.apis.get_rest(:api => 'discovery', :version => 'v1').execute
result.should_not be_nil
result.data.name.should == 'discovery'
result.data.version.should == 'v1'
end
end
end