Class: Puppet::Rest::Client

Inherits:
Object show all
Defined in:
lib/puppet/rest/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssl_context:, receive_timeout: Puppet[:http_read_timeout], client: HTTPClient.new(agent_name: nil, default_header: { 'User-Agent' => Puppet[:http_user_agent], 'X-PUPPET-VERSION' => Puppet::PUPPETVERSION })) ⇒ Client

Create a new HTTP client for querying the given API.

Parameters:

  • ssl_context (Puppet::SSL::SSLContext)

    the SSL configuration for this client

  • receive_timeout (Integer) (defaults to: Puppet[:http_read_timeout])

    how long in seconds this client will wait for a response after making a request

  • client (HTTPClient) (defaults to: HTTPClient.new(agent_name: nil, default_header: { 'User-Agent' => Puppet[:http_user_agent], 'X-PUPPET-VERSION' => Puppet::PUPPETVERSION }))

    the third-party HTTP client wrapped by this class. This param is only used for testing.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/rest/client.rb', line 18

def initialize(ssl_context:,
               receive_timeout: Puppet[:http_read_timeout],
               client: HTTPClient.new(agent_name: nil,
                                      default_header: {
                                        'User-Agent' => Puppet[:http_user_agent],
                                        'X-PUPPET-VERSION' => Puppet::PUPPETVERSION
                                      }))
  @client = client
  @client.tcp_keepalive = true
  @client.connect_timeout = Puppet[:http_connect_timeout]
  @client.receive_timeout = receive_timeout
  @client.transparent_gzip_decompression = true

  if Puppet[:http_debug]
    @client.debug_dev = $stderr
  end

  @ca_path = Puppet[:ssl_client_ca_auth] || Puppet[:localcacert]
  @verifier = Puppet::SSL::Validator::DefaultValidator.new(@ca_path)
  configure_verify_mode(ssl_context)

  @dns_resolver = Puppet::Network::Resolver.new
end

Instance Attribute Details

#dns_resolverObject (readonly)

Returns the value of attribute dns_resolver.



10
11
12
# File 'lib/puppet/rest/client.rb', line 10

def dns_resolver
  @dns_resolver
end

Instance Method Details

#get(url, query: nil, header: nil, &block) ⇒ Object

Make a GET request to the specified URL with the specified params.

Parameters:

  • url (URI::HTTPS)

    the full path to query

  • query (Hash) (defaults to: nil)

    any URL params to add to send to the endpoint

  • header (Hash) (defaults to: nil)

    any additional entries to add to the default header

Raises:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/puppet/rest/client.rb', line 48

def get(url, query: nil, header: nil, &block)
  begin
    @client.get_content(url.to_s, { query: query, header: header }) do |chunk|
      block.call(chunk)
    end
  rescue HTTPClient::BadResponseError => e
    raise Puppet::Rest::ResponseError.new(e.message, Puppet::Rest::Response.new(e.res))
  rescue OpenSSL::OpenSSLError => e
    Puppet::Util::SSL.handle_connection_error(e, @verifier, url.host)
  end
end

#put(url, body:, query: nil, header: nil) ⇒ Puppet::Rest::Response

Make a PUT request to the specified URL with the specified params.

Parameters:

  • url (URI::HTTPS)

    the full path to query

  • body (String/Hash)

    the contents of the PUT request

  • query (Hash) (defaults to: nil)

    any URL params to add to send to the endpoint

  • header (Hash) (defaults to: nil)

    any additional entries to add to the default header

Returns:



66
67
68
69
70
71
72
73
# File 'lib/puppet/rest/client.rb', line 66

def put(url, body:, query: nil, header: nil)
  begin
    response = @client.put(url.to_s, body: body, query: query, header: header)
    Puppet::Rest::Response.new(response)
  rescue OpenSSL::OpenSSLError => e
    Puppet::Util::SSL.handle_connection_error(e, @verifier, url.host)
  end
end