Class: PuppetX::Relay::Util::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/relay/util/http/client.rb

Direct Known Subclasses

PE, RelayAPI

Instance Method Summary collapse

Constructor Details

#initialize(base_url, settings = nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/puppet_x/relay/util/http/client.rb', line 8

def initialize(base_url, settings = nil)
  @base_url = base_url

  @proxy_host = settings[:proxy_host] if settings
  @proxy_port = settings[:proxy_port] if settings
  raise 'proxy_port should be set if proxy_host is defined' if @proxy_port.nil? && !@proxy_host.nil?

  # restore default behaviour if @proxy_host is not set
  @proxy_host = :ENV if @proxy_host.nil?

  @proxy_user = settings[:proxy_user] if settings
  @proxy_password = settings[:proxy_password] if settings
end

Instance Method Details

#get(path) ⇒ Object



42
43
44
# File 'lib/puppet_x/relay/util/http/client.rb', line 42

def get(path)
  request(:get, path)
end

#post(path, body: nil) ⇒ Object



46
47
48
# File 'lib/puppet_x/relay/util/http/client.rb', line 46

def post(path, body: nil)
  request(:post, path, body: body)
end

#put(path, body: nil) ⇒ Object



50
51
52
# File 'lib/puppet_x/relay/util/http/client.rb', line 50

def put(path, body: nil)
  request(:put, path, body: body)
end

#request(verb, path, body: nil) ⇒ Net::HTTPResponse

Parameters:

  • verb (Symbol)
  • path (String)

Returns:

  • (Net::HTTPResponse)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet_x/relay/util/http/client.rb', line 25

def request(verb, path, body: nil)
  uri = URI.join(@base_url, path)

  req = Object.const_get("Net::HTTP::#{verb.to_s.capitalize}").new(uri)
  req['Content-Type'] = 'application/json'
  req.body = body.to_json if body

  update_request!(req)

  http = Net::HTTP.new(uri.host, uri.port, @proxy_host, @proxy_port, @proxy_user, @proxy_password)
  http.use_ssl = uri.scheme == 'https'
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  update_http!(http)

  http.start { |sess| sess.request(req) }
end