X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/4d3c1801b7dccf5f2938b2a9ddbbb88da0e32e96..ee6f8ae6f983f2beb1afa94f9332f9e244fbbb9d:/lib/google/api_client.rb diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 4856bde042..4c9ee03c7a 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -20,19 +20,25 @@ require 'compat/multi_json' require 'stringio' require 'google/api_client/version' +require 'google/api_client/logging' require 'google/api_client/errors' require 'google/api_client/environment' require 'google/api_client/discovery' +require 'google/api_client/request' require 'google/api_client/reference' require 'google/api_client/result' require 'google/api_client/media' require 'google/api_client/service_account' require 'google/api_client/batch' +require 'google/api_client/railtie' if defined?(Rails) module Google + ## # This class manages APIs communication. class APIClient + include Google::APIClient::Logging + ## # Creates a new Google API client. # @@ -46,6 +52,10 @@ module Google #
  • :oauth_1
  • #
  • :oauth_2
  • # + # @option options [Boolean] :auto_refresh_token (true) + # The setting that controls whether or not the api client attempts to + # refresh authorization when a 401 is hit in #execute. If the token does + # not support it, this option is ignored. # @option options [String] :application_name # The name of the application using the client. # @option options [String] :application_version @@ -61,6 +71,8 @@ module Google # @option options [String] :discovery_path ("/discovery/v1") # The discovery base path. This rarely needs to be changed. def initialize(options={}) + logger.debug { "#{self.class} - Initializing client with options #{options}" } + # Normalize key to String to allow indifferent access. options = options.inject({}) do |accu, (key, value)| accu[key.to_sym] = value @@ -73,26 +85,29 @@ module Google # Most developers will want to leave this value alone and use the # application_name option. - application_string = ( - options[:application_name] ? ( - "#{options[:application_name]}/" + - "#{options[:application_version] || '0.0.0'}" - ) : "" - ) + if options[:application_name] + app_name = options[:application_name] + app_version = options[:application_version] + application_string = "#{app_name}/#{app_version || '0.0.0'}" + else + logger.warn { "#{self.class} - Please provide :application_name and :application_version when initializing the client" } + end self.user_agent = options[:user_agent] || ( "#{application_string} " + - "google-api-ruby-client/#{VERSION::STRING} " + + "google-api-ruby-client/#{Google::APIClient::VERSION::STRING} " + ENV::OS_VERSION ).strip # The writer method understands a few Symbols and will generate useful # default authentication mechanisms. self.authorization = options.key?(:authorization) ? options[:authorization] : :oauth_2 + self.auto_refresh_token = options.fetch(:auto_refresh_token) { true } self.key = options[:key] self.user_ip = options[:user_ip] @discovery_uris = {} @discovery_documents = {} @discovered_apis = {} + return self end @@ -152,6 +167,13 @@ module Google return @authorization end + ## + # The setting that controls whether or not the api client attempts to + # refresh authorization when a 401 is hit in #execute. + # + # @return [Boolean] + attr_accessor :auto_refresh_token + ## # The application's API key issued by the API console. # @@ -487,8 +509,6 @@ module Google # - (String) body: The body of the request. # - (Hash, Array) headers: The HTTP headers for the request. # - (Hash) options: A set of options for the request, of which: - # - (String) :version (default: "v1") - - # The service version. Only used if `api_method` is a `String`. # - (#generate_authenticated_request) :authorization (default: true) - # The authorization mechanism for the response. Used only if # `:authenticated` is `true`. @@ -529,7 +549,7 @@ module Google options[:parameters] = params.shift if params.size > 0 options[:body] = params.shift if params.size > 0 options[:headers] = params.shift if params.size > 0 - options[:client] = self + options.update(params.shift) if params.size > 0 request = self.generate_request(options) end @@ -541,6 +561,16 @@ module Google request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false result = request.send(connection) + if result.status == 401 && authorization.respond_to?(:refresh_token) && auto_refresh_token + begin + logger.debug("Attempting refresh of access token & retry of request") + authorization.fetch_access_token! + result = request.send(connection) + rescue Signet::AuthorizationError + # Ignore since we want the original error + end + end + return result end