Merge pull request #20 from simplymeasured/feature/make-autorefresh-of-token-optional
[arvados.git] / lib / google / api_client.rb
index deeed62018be9cb1d7732c9030dedad0b5770078..4c9ee03c7a9a7b1657f5f0418f884512b5f3326a 100644 (file)
@@ -19,8 +19,8 @@ require 'multi_json'
 require 'compat/multi_json'
 require 'stringio'
 
-require 'google/api_client/logging'
 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'
@@ -37,7 +37,7 @@ module Google
   ##
   # This class manages APIs communication.
   class APIClient
-    include Google::Logging
+    include Google::APIClient::Logging
     
     ##
     # Creates a new Google API client.
@@ -71,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
@@ -88,23 +90,24 @@ module Google
         app_version = options[:application_version]
         application_string = "#{app_name}/#{app_version || '0.0.0'}"
       else
-        logger.warn("Please provide :application_name and :application_version when initializing the APIClient") 
+        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.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
 
@@ -164,12 +167,6 @@ module Google
       return @authorization
     end
 
-    ##
-    # The application's API key issued by the API console.
-    #
-    # @return [String] The API key.
-    attr_accessor :key
-
     ##
     # The setting that controls whether or not the api client attempts to
     # refresh authorization when a 401 is hit in #execute. 
@@ -177,6 +174,12 @@ module Google
     # @return [Boolean]
     attr_accessor :auto_refresh_token
 
+    ##
+    # The application's API key issued by the API console.
+    #
+    # @return [String] The API key.
+    attr_accessor :key
+
     ##
     # The IP address of the user this request is being performed on behalf of.
     #
@@ -560,6 +563,7 @@ module Google
       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