Class: Puppet::HTTP::ExternalClient Private

Inherits:
Client show all
Defined in:
lib/puppet/http/external_client.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Adapts an external http_client_class to the HTTP client API. The former is typically registered by puppetserver and only implements a subset of the Puppet::Network::HTTP::Connection methods. As a result, only the `get` and `post` methods are supported. Calling `delete`, etc will raise a NotImplementedError.

Instance Attribute Summary

Attributes inherited from Client

#pool

Instance Method Summary collapse

Constructor Details

#initialize(http_client_class) ⇒ ExternalClient

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create an external http client.

Parameters:

  • http_client_class (Class)

    The class to create to handle the request


13
14
15
# File 'lib/puppet/http/external_client.rb', line 13

def initialize(http_client_class)
  @http_client_class = http_client_class
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close persistent connections in the pool.


62
63
64
# File 'lib/puppet/http/external_client.rb', line 62

def close
  # This is a noop as puppetserver doesn't provide a way to close its http client.
end

#connect(uri, options: {}, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (NotImplementedError)

72
73
74
# File 'lib/puppet/http/external_client.rb', line 72

def connect(uri, options: {}, &block)
  raise NotImplementedError
end

#create_sessionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The following are intentionally not documented

Raises:

  • (NotImplementedError)

68
69
70
# File 'lib/puppet/http/external_client.rb', line 68

def create_session
  raise NotImplementedError
end

#delete(url, headers: {}, params: {}, options: {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (NotImplementedError)

84
85
86
# File 'lib/puppet/http/external_client.rb', line 84

def delete(url, headers: {}, params: {}, options: {})
  raise NotImplementedError
end

#get(url, headers: {}, params: {}, options: {}) {|Puppet::HTTP::Response| ... } ⇒ Puppet::HTTP::Response

Submits a GET HTTP request to the given url

Parameters:

  • url (URI)

    the location to submit the http request

  • headers (Hash) (defaults to: {})

    merged with the default headers defined by the client

  • params (Hash) (defaults to: {})

    encoded and set as the url query

  • options (Hash) (defaults to: {})

    HTTP request options. Options not recognized by the HTTP implementation will be ignored.

Options Hash (options:):

  • :ssl_context (Puppet::SSL::SSLContext) — default: nil

    ssl context to be used for connections

  • :include_system_store (Boolean) — default: false

    if we should include the system store for connection

  • :redirect_limit (Integer) — default: 10

    The maximum number of HTTP redirections to allow for this request.

  • :basic_auth (Hash)

    A map of `:username` => `String` and `:password` => `String`

  • :metric_id (String)

    The metric id used to track metrics on requests.

Yields:

Returns:


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet/http/external_client.rb', line 19

def get(url, headers: {}, params: {}, options: {}, &block)
  url = encode_query(url, params)

  options[:use_ssl] = url.scheme == 'https'

  client = @http_client_class.new(url.host, url.port, options)
  response = Puppet::HTTP::ResponseNetHTTP.new(url, client.get(url.request_uri, headers, options))

  if block_given?
    yield response
  else
    response
  end
rescue Puppet::HTTP::HTTPError
  raise
rescue => e
  raise Puppet::HTTP::HTTPError.new(e.message, e)
end

#head(url, headers: {}, params: {}, options: {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (NotImplementedError)

76
77
78
# File 'lib/puppet/http/external_client.rb', line 76

def head(url, headers: {}, params: {}, options: {})
  raise NotImplementedError
end

#post(url, body, headers: {}, params: {}, options: {}) {|Puppet::HTTP::Response| ... } ⇒ Puppet::HTTP::Response

Submits a POST HTTP request to the given url

Parameters:

  • url (URI)

    the location to submit the http request

  • body (String)

    the body of the POST request

  • headers (Hash) (defaults to: {})

    merged with the default headers defined by the client. The `Content-Type` header is required and should correspond to the type of data passed as the `body` argument.

  • params (Hash) (defaults to: {})

    encoded and set as the url query

  • options (Hash) (defaults to: {})

    HTTP request options. Options not recognized by the HTTP implementation will be ignored.

Options Hash (options:):

  • :ssl_context (Puppet::SSL::SSLContext) — default: nil

    ssl context to be used for connections

  • :include_system_store (Boolean) — default: false

    if we should include the system store for connection

  • :redirect_limit (Integer) — default: 10

    The maximum number of HTTP redirections to allow for this request.

  • :basic_auth (Hash)

    A map of `:username` => `String` and `:password` => `String`

  • :metric_id (String)

    The metric id used to track metrics on requests.

Yields:

Returns:


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/puppet/http/external_client.rb', line 40

def post(url, body, headers: {}, params: {}, options: {}, &block)
  raise ArgumentError.new("'post' requires a string 'body' argument") unless body.is_a?(String)
  url = encode_query(url, params)

  options[:use_ssl] = url.scheme == 'https'

  client = @http_client_class.new(url.host, url.port, options)
  response = Puppet::HTTP::ResponseNetHTTP.new(url, client.post(url.request_uri, body, headers, options))

  if block_given?
    yield response
  else
    response
  end
rescue Puppet::HTTP::HTTPError, ArgumentError
  raise
rescue => e
  raise Puppet::HTTP::HTTPError.new(e.message, e)
end

#put(url, headers: {}, params: {}, options: {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (NotImplementedError)

80
81
82
# File 'lib/puppet/http/external_client.rb', line 80

def put(url, headers: {}, params: {}, options: {})
  raise NotImplementedError
end