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

Methods inherited from Client

#default_ssl_context

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



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

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.



64
65
66
# File 'lib/puppet/http/external_client.rb', line 64

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)


74
75
76
# File 'lib/puppet/http/external_client.rb', line 74

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)


70
71
72
# File 'lib/puppet/http/external_client.rb', line 70

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)


86
87
88
# File 'lib/puppet/http/external_client.rb', line 86

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:



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

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)


78
79
80
# File 'lib/puppet/http/external_client.rb', line 78

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:



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

def post(url, body, headers: {}, params: {}, options: {}, &block)
  raise ArgumentError, "'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)


82
83
84
# File 'lib/puppet/http/external_client.rb', line 82

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