Added HTTP Proxy support
[arvados.git] / lib / google / api_client.rb
index 4a9678d10ba825f396f1323c3b68da46603f1dfa..50aeeefe901fc0e193414b24c0c0cc140895c001 100644 (file)
 
 
 require 'faraday'
-require 'faraday/utils'
 require 'multi_json'
 require 'compat/multi_json'
 require 'stringio'
+require 'retriable'
 
 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/gzip'
+require 'google/api_client/client_secrets'
+require 'google/api_client/railtie' if defined?(Rails::Railtie)
 
 module Google
+
   ##
   # This class manages APIs communication.
   class APIClient
+    include Google::APIClient::Logging
+    
     ##
     # Creates a new Google API client.
     #
@@ -46,6 +54,10 @@ module Google
     #     <li><code>:oauth_1</code></li>
     #     <li><code>:oauth_2</code></li>
     #   </ul>
+    # @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
@@ -60,7 +72,12 @@ module Google
     #   The port number used by the client. This rarely needs to be changed.
     # @option options [String] :discovery_path ("/discovery/v1")
     #   The discovery base path. This rarely needs to be changed.
+    # @option options [String] :ca_file
+    #   Optional set of root certificates to use when validating SSL connections.
+    #   By default, a bundled set of trusted roots will be used.
     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 +90,40 @@ 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
+
+      proxy = options[:proxy] || Object::ENV["http_proxy"]
+
       self.user_agent = options[:user_agent] || (
         "#{application_string} " +
-        "google-api-ruby-client/#{VERSION::STRING} " +
-         ENV::OS_VERSION
+        "google-api-ruby-client/#{Google::APIClient::VERSION::STRING} #{ENV::OS_VERSION} (gzip)"
       ).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]
+      self.retries = options.fetch(:retries) { 0 }
       @discovery_uris = {}
       @discovery_documents = {}
       @discovered_apis = {}
+      ca_file = options[:ca_file] || File.expand_path('../../cacerts.pem', __FILE__)
+      self.connection = Faraday.new do |faraday|
+        faraday.response :gzip
+        faraday.options.params_encoder = Faraday::FlatParamsEncoder
+        faraday.ssl.ca_file = ca_file
+        faraday.ssl.verify = true
+        faraday.proxy proxy
+        faraday.adapter Faraday.default_adapter
+      end
       return self
     end
 
@@ -152,6 +183,19 @@ module Google
       return @authorization
     end
 
+    ##
+    # Default Faraday/HTTP connection.
+    #
+    # @return [Faraday::Connection]
+    attr_accessor :connection
+
+    ##
+    # 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.
     #
@@ -192,6 +236,13 @@ module Google
     #   The base path. Should almost always be '/discovery/v1'.
     attr_accessor :discovery_path
 
+    ##
+    # Number of times to retry on recoverable errors
+    # 
+    # @return [FixNum]
+    #  Number of retries
+    attr_accessor :retries
+
     ##
     # Returns the URI for the directory document.
     #
@@ -341,7 +392,7 @@ module Google
     # Returns the method object for a given RPC name and service version.
     #
     # @param [String, Symbol] rpc_name The RPC name of the desired method.
-    # @param [String, Symbol] rpc_name The API the method is within.
+    # @param [String, Symbol] api The API the method is within.
     # @param [String] version The desired version of the API.
     #
     # @return [Google::APIClient::Method] The method object.
@@ -386,6 +437,8 @@ module Google
     # Verifies an ID token against a server certificate. Used to ensure that
     # an ID token supplied by an untrusted client-side mechanism is valid.
     # Raises an error if the token is invalid or missing.
+    # 
+    # @deprecated Use the google-id-token gem for verifying JWTs
     def verify_id_token!
       require 'jwt'
       require 'openssl'
@@ -439,7 +492,7 @@ module Google
     ##
     # Generates a request.
     #
-    # @option options [Google::APIClient::Method, String] :api_method
+    # @option options [Google::APIClient::Method] :api_method
     #   The method object or the RPC name of the method being executed.
     # @option options [Hash, Array] :parameters
     #   The parameters to send to the method.
@@ -480,21 +533,23 @@ module Google
     #   If a Hash, the below parameters are handled. If an Array, the
     #   parameters are assumed to be in the below order:
     #
-    #   - (Google::APIClient::Method, String) api_method:
+    #   - (Google::APIClient::Method) api_method:
     #     The method object or the RPC name of the method being executed.
     #   - (Hash, Array) parameters:
     #     The parameters to send to the method.
     #   - (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`.
     #     - (TrueClass, FalseClass) :authenticated (default: true) -
     #       `true` if the request must be signed or somehow
     #       authenticated, `false` otherwise.
+    #     - (TrueClass, FalseClass) :gzip (default: true) - 
+    #       `true` if gzip enabled, `false` otherwise.
+    #     - (FixNum) :retries -
+    #       # of times to retry on recoverable errors
     #
     # @return [Google::APIClient::Result] The result from the API, nil if batch.
     #
@@ -502,17 +557,17 @@ module Google
     #   result = client.execute(batch_request)
     #
     # @example
+    #   plus = client.discovered_api('plus')
     #   result = client.execute(
-    #     :api_method => 'plus.activities.list',
+    #     :api_method => plus.activities.list,
     #     :parameters => {'collection' => 'public', 'userId' => 'me'}
     #   )
     #
     # @see Google::APIClient#generate_request
-    def execute(*params)
-      if params.last.kind_of?(Google::APIClient::Request) &&
-          params.size == 1
-        request = params.pop
-        options = {}
+    def execute!(*params)
+      if params.first.kind_of?(Google::APIClient::Request)
+        request = params.shift
+        options = params.shift || {}
       else
         # This block of code allows us to accept multiple parameter passing
         # styles, and maintaining some backwards compatibility.
@@ -528,81 +583,65 @@ 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
       
-      connection = options[:connection] || Faraday.default_connection
-      request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false
+      request.headers['User-Agent'] ||= '' + self.user_agent unless self.user_agent.nil?
+      request.headers['Accept-Encoding'] ||= 'gzip' unless options[:gzip] == false
+      request.headers['Content-Type'] ||= ''
+      request.parameters['key'] ||= self.key unless self.key.nil?
+      request.parameters['userIp'] ||= self.user_ip unless self.user_ip.nil?
 
-      result = request.send(connection)
+      connection = options[:connection] || self.connection
+      request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false
+      
+      tries = 1 + (options[:retries] || self.retries)
 
-      if request.upload_type == 'resumable'
-        upload =  result.resumable_upload
-        unless upload.complete?
-          result = upload.send(connection)
-        end
-      end
-      return result
-    end
+      Retriable.retriable :tries => tries, 
+                          :on => [TransmissionError],
+                          :on_retry => client_error_handler(request.authorization),
+                          :interval => lambda {|attempts| (2 ** attempts) + rand} do
+        result = request.send(connection, true)
 
-    ##
-    # Same as Google::APIClient#execute, but raises an exception if there was
-    # an error.
-    #
-    # @see Google::APIClient#execute
-    def execute!(*params)
-      result = self.execute(*params)
-      if result.error?
-        error_message = result.error_message
-        case result.response.status
+        case result.status
+          when 200...300
+            result
+          when 301, 302, 303, 307
+            request = generate_request(request.to_hash.merge({
+              :uri => result.headers['location'],
+              :api_method => nil
+            }))
+            raise RedirectError.new(result.headers['location'], result)
           when 400...500
-            exception_type = ClientError
-            error_message ||= "A client error has occurred."
+            raise ClientError.new(result.error_message || "A client error has occurred", result)
           when 500...600
-            exception_type = ServerError
-            error_message ||= "A server error has occurred."
+            raise ServerError.new(result.error_message || "A server error has occurred", result)
           else
-            exception_type = TransmissionError
-            error_message ||= "A transmission error has occurred."
+            raise TransmissionError.new(result.error_message || "A transmission error has occurred", result)
         end
-        raise exception_type, error_message
       end
-      return result
     end
-    
+
     ##
-    # Ensures API method names specified as strings resolve to 
-    # discovered method instances
-    def resolve_method(method, version)
-      version ||= 'v1'
-      if method.kind_of?(Google::APIClient::Method) || method == nil
-        return method
-      elsif method.respond_to?(:to_str) || method.kind_of?(Symbol)
-        # This method of guessing the API is unreliable. This will fail for
-        # APIs where the first segment of the RPC name does not match the
-        # service name. However, this is a fallback mechanism anyway.
-        # Developers should be passing in a reference to the method, rather
-        # than passing in a string or symbol. This should raise an error
-        # in the case of a mismatch.
-        method = method.to_s        
-        api = method[/^([^.]+)\./, 1]
-        api_method = self.discovered_method(method, api, version)
-        if api_method.nil?
-          raise ArgumentError, "API method could not be found."
-        end
-        return api_method
-      else
-        raise TypeError,
-          "Expected Google::APIClient::Method, got #{method.class}."
+    # Same as Google::APIClient#execute!, but does not raise an exception for
+    # normal API errros.
+    #
+    # @see Google::APIClient#execute
+    def execute(*params)
+      begin
+        return self.execute!(*params)
+      rescue TransmissionError => e
+        return e.result
       end
     end
-    
+
     protected
-    
+
     ##
     # Resolves a URI template against the client's configured base.
     #
+    # @api private
     # @param [String, Addressable::URI, Addressable::Template] template
     #   The template to resolve.
     # @param [Hash] mapping The mapping that corresponds to the template.
@@ -625,7 +664,33 @@ module Google
       return Addressable::Template.new(@base_uri + template).expand(mapping)
     end
     
+
+    ##
+    # Returns on proc for special processing of retries as not all client errors
+    # are recoverable. Only 401s should be retried and only if the credentials
+    # are refreshable
+    #
+    # @param [#fetch_access_token!] authorization
+    #   OAuth 2 credentials
+    # @return [Proc] 
+    def client_error_handler(authorization)  
+      can_refresh = authorization.respond_to?(:refresh_token) && auto_refresh_token 
+      Proc.new do |exception, tries|
+        next unless exception.kind_of?(ClientError)
+        if exception.result.status == 401 && can_refresh && tries == 1
+          begin
+            logger.debug("Attempting refresh of access token & retry of request")
+            authorization.fetch_access_token!
+            next
+          rescue Signet::AuthorizationError
+          end
+        end
+        raise exception
+      end
+    end
+
   end
+
 end
 
 require 'google/api_client/version'