- Improving the Service interface with access to more properties
- Adding end-to-end spec - Fixing bugs
This commit is contained in:
parent
e73ce124fa
commit
1e2405093b
|
@ -30,6 +30,10 @@ module Google
|
||||||
# result = calendar.events.list('calendarId' => 'primary').execute()
|
# result = calendar.events.list('calendarId' => 'primary').execute()
|
||||||
class Service
|
class Service
|
||||||
include Google::APIClient::Service::StubGenerator
|
include Google::APIClient::Service::StubGenerator
|
||||||
|
extend Forwardable
|
||||||
|
|
||||||
|
# Cache for discovered APIs.
|
||||||
|
@@discovered = {}
|
||||||
|
|
||||||
##
|
##
|
||||||
# Creates a new Service.
|
# Creates a new Service.
|
||||||
|
@ -74,7 +78,7 @@ module Google
|
||||||
# authenticated, `false` otherwise.
|
# authenticated, `false` otherwise.
|
||||||
# @option options [TrueClass, FalseClass] :gzip (default: true)
|
# @option options [TrueClass, FalseClass] :gzip (default: true)
|
||||||
# `true` if gzip enabled, `false` otherwise.
|
# `true` if gzip enabled, `false` otherwise.
|
||||||
# @option options [Faraday] :connection
|
# @option options [Faraday::Connection] :connection
|
||||||
# A custom connection to be used for all requests.
|
# A custom connection to be used for all requests.
|
||||||
def initialize(api_name, api_version, options = {})
|
def initialize(api_name, api_version, options = {})
|
||||||
@api_name = api_name.to_s
|
@api_name = api_name.to_s
|
||||||
|
@ -98,35 +102,65 @@ module Google
|
||||||
end
|
end
|
||||||
|
|
||||||
@client = Google::APIClient.new(params)
|
@client = Google::APIClient.new(params)
|
||||||
|
@client.logger = options[:logger] if options.include? :logger
|
||||||
|
|
||||||
|
@connection = options[:connection] || @client.connection
|
||||||
|
|
||||||
@options = options
|
@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)
|
generate_call_stubs(self, @api)
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Logger for the Service.
|
# Logger for the Service.
|
||||||
#
|
#
|
||||||
# @return [Logger] logger instance.
|
# @return [Logger]
|
||||||
def logger
|
# The logger instance.
|
||||||
@client.logger
|
def_delegators :@client, :logger, :logger=
|
||||||
end
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Set the Logger for the Service.
|
# Returns the authorization mechanism used by the service.
|
||||||
def logger=(obj)
|
#
|
||||||
@client.logger = obj
|
# @return [#generate_authenticated_request] The authorization mechanism.
|
||||||
end
|
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.
|
# Executes an API request.
|
||||||
# Do not call directly; this method is only used by Request objects when
|
# Do not call directly; this method is only used by Request objects when
|
||||||
# executing.
|
# executing.
|
||||||
|
#
|
||||||
# @param [Google::APIClient::Service::Request] request
|
# @param [Google::APIClient::Service::Request] request
|
||||||
# The request to be executed.
|
# The request to be executed.
|
||||||
def execute(request)
|
def execute(request)
|
||||||
params = {:api_method => request.method,
|
params = {:api_method => request.method,
|
||||||
:parameters => request.parameters}
|
:parameters => request.parameters,
|
||||||
|
:connection => @connection}
|
||||||
if request.respond_to? :body
|
if request.respond_to? :body
|
||||||
if request.body.respond_to? :to_hash
|
if request.body.respond_to? :to_hash
|
||||||
params[:body_object] = request.body
|
params[:body_object] = request.body
|
||||||
|
@ -137,13 +171,13 @@ module Google
|
||||||
if request.respond_to? :media
|
if request.respond_to? :media
|
||||||
params[:media] = request.media
|
params[:media] = request.media
|
||||||
end
|
end
|
||||||
[:connection, :authenticated, :gzip].each do |option|
|
[:authenticated, :gzip].each do |option|
|
||||||
if @options.include? option
|
if @options.include? option
|
||||||
params[option] = @options[option]
|
params[option] = @options[option]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
result = @client.execute(params)
|
result = @client.execute(params)
|
||||||
return Google::APIClient::Result.new(request, result)
|
return Google::APIClient::Service::Result.new(request, result)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -261,6 +261,17 @@ describe Google::APIClient::Service do
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue