- 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,52 +30,56 @@ 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.
# Creates a new Service. @@discovered = {}
#
# @param [String, Symbol] api_name ##
# The name of the API this service will access. # Creates a new Service.
# @param [String, Symbol] api_version #
# The version of the API this service will access. # @param [String, Symbol] api_name
# @param [Hash] options # The name of the API this service will access.
# The configuration parameters for the service. # @param [String, Symbol] api_version
# @option options [Symbol, #generate_authenticated_request] :authorization # The version of the API this service will access.
# (:oauth_1) # @param [Hash] options
# The authorization mechanism used by the client. The following # The configuration parameters for the service.
# mechanisms are supported out-of-the-box: # @option options [Symbol, #generate_authenticated_request] :authorization
# <ul> # (:oauth_1)
# <li><code>:two_legged_oauth_1</code></li> # The authorization mechanism used by the client. The following
# <li><code>:oauth_1</code></li> # mechanisms are supported out-of-the-box:
# <li><code>:oauth_2</code></li> # <ul>
# </ul> # <li><code>:two_legged_oauth_1</code></li>
# @option options [Boolean] :auto_refresh_token (true) # <li><code>:oauth_1</code></li>
# The setting that controls whether or not the api client attempts to # <li><code>:oauth_2</code></li>
# refresh authorization when a 401 is hit in #execute. If the token does # </ul>
# not support it, this option is ignored. # @option options [Boolean] :auto_refresh_token (true)
# @option options [String] :application_name # The setting that controls whether or not the api client attempts to
# The name of the application using the client. # refresh authorization when a 401 is hit in #execute. If the token does
# @option options [String] :application_version # not support it, this option is ignored.
# The version number of the application using the client. # @option options [String] :application_name
# @option options [String] :host ("www.googleapis.com") # The name of the application using the client.
# The API hostname used by the client. This rarely needs to be changed. # @option options [String] :application_version
# @option options [String] :port (443) # The version number of the application using the client.
# The port number used by the client. This rarely needs to be changed. # @option options [String] :host ("www.googleapis.com")
# @option options [String] :discovery_path ("/discovery/v1") # The API hostname used by the client. This rarely needs to be changed.
# The discovery base path. This rarely needs to be changed. # @option options [String] :port (443)
# @option options [String] :ca_file # The port number used by the client. This rarely needs to be changed.
# Optional set of root certificates to use when validating SSL connections. # @option options [String] :discovery_path ("/discovery/v1")
# By default, a bundled set of trusted roots will be used. # The discovery base path. This rarely needs to be changed.
# @option options [#generate_authenticated_request] :authorization # @option options [String] :ca_file
# The authorization mechanism for requests. Used only if # Optional set of root certificates to use when validating SSL connections.
# `:authenticated` is `true`. # By default, a bundled set of trusted roots will be used.
# @option options [TrueClass, FalseClass] :authenticated (default: true) # @option options [#generate_authenticated_request] :authorization
# `true` if requests must be signed or somehow # The authorization mechanism for requests. Used only if
# authenticated, `false` otherwise. # `:authenticated` is `true`.
# @option options [TrueClass, FalseClass] :gzip (default: true) # @option options [TrueClass, FalseClass] :authenticated (default: true)
# `true` if gzip enabled, `false` otherwise. # `true` if requests must be signed or somehow
# @option options [Faraday] :connection # authenticated, `false` otherwise.
# A custom connection to be used for all requests. # @option options [TrueClass, FalseClass] :gzip (default: true)
# `true` if gzip enabled, `false` otherwise.
# @option options [Faraday::Connection] :connection
# 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
if api_version.nil? if api_version.nil?
@ -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

View File

@ -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