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



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

#closeObject

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.

Close the external http client.



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| ... } ⇒ String

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.

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: {})

    passed through to the request execution

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

    :redirect_limit number of HTTP redirections to allow for this request.

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

Yields:

Returns:

  • (String)

    if a block is not given, returns the response body



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::Response.new(client.get(url.request_uri, headers, options), url)

  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: {}, &block) ⇒ String

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.

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

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

    encoded and set as the url query

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

    passed through to the request execution

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

    :redirect_limit number of HTTP redirections to allow for this request.

Options Hash (options:):

  • :content_type (String)

    the type of the body content

  • :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

Returns:

  • (String)

    the body of the request response



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

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::Response.new(client.post(url.request_uri, body, headers, options), url)

  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