Class: KatelloApi::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/katello_api/base.rb

Constant Summary collapse

API_VERSION =
"2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/katello_api/base.rb', line 31

def initialize(config, options = {})
  headers = { :content_type => 'application/json',
              :accept => "application/json,version=#{API_VERSION}" }

  @client = RestClient::Resource.new(
      "#{config[:base_url]}/katello",
      { :user => config[:username],
        :password => config[:password],
        :oauth => config[:oauth],
        :headers => headers
      }.merge(options))
  @config = config
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



29
30
31
# File 'lib/katello_api/base.rb', line 29

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/katello_api/base.rb', line 29

def config
  @config
end

Class Method Details

.docObject

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/katello_api/base.rb', line 68

def self.doc
  raise NotImplementedError
end

.method_doc(method) ⇒ Object



76
77
78
# File 'lib/katello_api/base.rb', line 76

def self.method_doc(method)
  method_docs[method.to_s]
end

.validation_hash(method) ⇒ Object



72
73
74
# File 'lib/katello_api/base.rb', line 72

def self.validation_hash(method)
  validation_hashes[method.to_s]
end

Instance Method Details

#http_call(http_method, path, params = { }, headers = { }) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/katello_api/base.rb', line 54

def http_call(http_method, path, params = { }, headers = { })
  headers ||= { }

  args = [http_method]
  if %w[post put].include?(http_method.to_s)
    args << params.to_json
  else
    headers[:params] = params if params
  end

  args << headers if headers
  process_data client[path].send(*args)
end

#perform_call(method_name, params, headers) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/katello_api/base.rb', line 45

def perform_call(method_name, params, headers)
  method_doc = self.class.method_doc(method_name)
  check_params params, :allowed => method_doc['params'].any?, :method => method_name
  method_apis = method_doc['apis']
  api = find_suitable_api_call(method_apis, params)
  url, params = fill_params_in_url api['api_url'], params
  return http_call(api['http_method'].downcase, url, params, headers)
end

#validate_params!(params, rules) ⇒ Object

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/katello_api/base.rb', line 80

def validate_params!(params, rules)
  return unless params.is_a?(Hash)

  invalid_keys = params.keys.map(&:to_s) - (rules.is_a?(Hash) ? rules.keys : rules)
  raise ArgumentError, "Invalid keys: #{invalid_keys.join(", ")}" unless invalid_keys.empty?

  if rules.is_a? Hash
    rules.each do |key, sub_keys|
      validate_params!(params[key], sub_keys) if params[key]
    end
  end
end