Added HTTP Proxy support
[arvados.git] / lib / google / api_client.rb
index dd8be6ab027bb690815200c9134fbfd96e2dfe0b..50aeeefe901fc0e193414b24c0c0cc140895c001 100644 (file)
@@ -97,6 +97,9 @@ module Google
       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/#{Google::APIClient::VERSION::STRING} #{ENV::OS_VERSION} (gzip)"
@@ -118,8 +121,9 @@ module Google
         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      
+      end
       return self
     end
 
@@ -591,9 +595,12 @@ module Google
 
       connection = options[:connection] || self.connection
       request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false
+      
       tries = 1 + (options[:retries] || self.retries)
+
       Retriable.retriable :tries => tries, 
-                          :on => [TransmissionError], 
+                          :on => [TransmissionError],
+                          :on_retry => client_error_handler(request.authorization),
                           :interval => lambda {|attempts| (2 ** attempts) + rand} do
         result = request.send(connection, true)
 
@@ -607,14 +614,6 @@ module Google
             }))
             raise RedirectError.new(result.headers['location'], result)
           when 400...500
-            if result.status == 401 && request.authorization.respond_to?(:refresh_token) && auto_refresh_token
-              begin
-                logger.debug("Attempting refresh of access token & retry of request")
-                request.authorization.fetch_access_token!
-              rescue Signet::AuthorizationError
-                 # Ignore since we want the original error
-              end
-            end
             raise ClientError.new(result.error_message || "A client error has occurred", result)
           when 500...600
             raise ServerError.new(result.error_message || "A server error has occurred", result)
@@ -665,6 +664,31 @@ 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