lib/runcible/base.rb in runcible-0.0.3 vs lib/runcible/base.rb in runcible-0.0.4

- old
+ new

@@ -33,36 +33,45 @@ # # :user Pulp username # :password Password for this user # :oauth Oauth credentials # :headers Additional headers e.g. content-type => "application/json" + # :url Scheme and hostname for the pulp server e.g. https://localhost/ + # :api_path URL path for the api e.g. pulp/api/v2/ def self.config=(conf={}) @@config = { - :headers => { :content_type => 'application/json', - :accept => 'application/json'} + :api_path => '/pulp/api/v2/', + :url => 'https://localhost', + :user => '', + :http_auth => {:password => {} }, + :headers => {:content_type => 'application/json', + :accept => 'application/json'} }.merge(conf) end def self.config @@config end def self.call(method, path, options={}) - path = '/pulp/api/v2/' + path + path = config[:api_path] + path - headers = options[:headers] ? options[:headers] : {} + headers = config[:headers] headers[:params] = options[:params] if options[:params] - headers = add_oauth_header(method, path, headers) if config[:oauth] - headers["pulp-user"] = "admin"#config[:user] - payload = [:post, :put].include?(method) ? generate_payload(options) : {} + if config[:oauth] + headers = add_oauth_header(method, path, headers) if config[:oauth] + headers["pulp-user"] = config[:user] + client = RestClient::Resource.new(config[:url]) + else + client = RestClient::Resource.new(config[:url], :user => config[:user], :password => config[:http_auth][:password]) + end args = [method] - args << payload.to_json if [:post, :put].include?(method) - args << headers if headers + args << generate_payload(options) if [:post, :put].include?(method) + args << headers - client = RestClient::Resource.new(config[:url], config) process_response(client[path].send(*args)) end def self.generate_payload(options) if options[:payload] @@ -79,11 +88,11 @@ end else payload = {} end - return payload + return payload.to_json end def self.process_response(response) begin response = RestClient::Response.create(JSON.parse(response.body), response.net_http_res, response.args) @@ -105,9 +114,13 @@ keys_to_remove.each do |key| local_names.delete(key) end return local_names + end + + def self.add_http_auth_header + return {:user => config[:user], :password => config[:http_auth][:password]} end def self.add_oauth_header(method, path, headers) default_options = { :site => config[:url], :http_method => method,