Class: Pulp::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/pulp/api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(domain, port, options = {}) ⇒ ApiClient

Returns a new instance of ApiClient.



9
10
11
12
13
14
15
16
17
18
# File 'lib/puppet_x/pulp/api_client.rb', line 9

def initialize(domain, port, options = {})
  @http = Net::HTTP.new(domain, port)
  @http.use_ssl      = options.fetch(:use_ssl) { true }
  @http.verify_mode  = options.fetch(:verify_mode) { OpenSSL::SSL::VERIFY_PEER }
  @http.open_timeout = options.fetch(:open_timeout) { 10 }
  @http.read_timeout = options.fetch(:read_timeout) { 120 }
  @http.ca_file      = options.fetch(:ca_file) { nil }
  @http.cert         = options.fetch(:cert) { nil}
  @http.key          = options.fetch(:key) { nil}
end

Instance Method Details

#delete(path) ⇒ Object



50
51
52
53
# File 'lib/puppet_x/pulp/api_client.rb', line 50

def delete(path)
  request = Net::HTTP::Delete.new(path)
  parse fetch(request)
end

#finishObject



30
31
32
# File 'lib/puppet_x/pulp/api_client.rb', line 30

def finish
  @http.finish if @http.started?
end

#get(path, params = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/puppet_x/pulp/api_client.rb', line 34

def get(path, params = {})
  uri       = URI.parse(path)
  uri.query = URI.encode_www_form(params) unless params.empty?
  request   = Net::HTTP::Get.new(uri.to_s)
  parse fetch(request)
end

#post(path, params = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/puppet_x/pulp/api_client.rb', line 41

def post(path, params = {})
  request = Net::HTTP::Post.new(path)
  request.content_type = 'application/json'
  auth = params.delete(:basic_auth)
  request.basic_auth(auth[:username], auth[:password]) if auth
  request.body = JSON.generate(params) unless params.empty?
  parse fetch(request)
end

#put(path, params = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/puppet_x/pulp/api_client.rb', line 55

def put(path, params = {})
  request = Net::HTTP::Put.new(path)
  request.content_type = 'application/json'
  request.body = JSON.generate(params) unless params.empty?
  parse fetch(request)
end

#startObject



20
21
22
23
24
25
26
27
28
# File 'lib/puppet_x/pulp/api_client.rb', line 20

def start
  if block_given?
    @http.start unless @http.started?
    yield(self)
    @http.finish if @http.started?
  else
    @http.start unless @http.started?
  end
end