lib/runcible/base.rb in runcible-0.3.1 vs lib/runcible/base.rb in runcible-0.3.2

- old
+ new

@@ -42,11 +42,12 @@ :api_path => '/pulp/api/v2/', :url => 'https://localhost', :user => '', :http_auth => {:password => {} }, :headers => {:content_type => 'application/json', - :accept => 'application/json'} + :accept => 'application/json'}, + :logging => {} }.merge(conf) end def self.config @@config @@ -55,18 +56,20 @@ def self.call(method, path, options={}) clone_config = self.config.clone #on occation path will already have prefix (sync cancel) path = clone_config[:api_path] + path if !path.start_with?(clone_config[:api_path]) - RestClient.log = clone_config[:logger] if clone_config[:logger] + RestClient.log = [] + logger = clone_config[:logging][:logger] + debug_logging = clone_config[:logging][:debug] + exception_logging = clone_config[:logging][:exception] headers = clone_config[:headers].clone get_params = options[:params] if options[:params] path = combine_get_params(path, get_params) if get_params - if clone_config[:oauth] headers = add_oauth_header(method, path, headers) if clone_config[:oauth] headers["pulp-user"] = clone_config[:user] client = RestClient::Resource.new(clone_config[:url]) else @@ -75,13 +78,26 @@ args = [method] args << generate_payload(options) if [:post, :put].include?(method) args << headers - process_response(client[path].send(*args)) + response = get_response(client, path, *args) + process_response(response) + + rescue => e + log_exception + raise e end + def self.get_response(client, path, *args) + client[path].send(*args) do |response, request, result, &block| + resp = response.return!(request, result) + log_debug + return resp + end + end + def self.combine_get_params(path, params) query_string = params.collect do |k, v| if v.is_a? Array v.collect{|y| "#{k.to_s}=#{y.to_s}" }.join('&') else @@ -170,9 +186,27 @@ http_request = method_to_http_request[method].new(path) consumer.sign!(http_request) headers['Authorization'] = http_request['Authorization'] return headers + end + + def self.log_debug + if self.config[:logging][:debug] + log_message = generate_log_message + self.config[:logging][:logger].debug(log_message) + end + end + + def self.log_exception + if self.config[:logging][:exception] + log_message = generate_log_message + self.config[:logging][:logger].error(log_message) + end + end + + def self.generate_log_message + RestClient.log.join('\n') end end end