Class: Puppet::HTTP::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(pool: Puppet::Network::HTTP::Pool.new, ssl_context: nil, redirect_limit: 10, retry_limit: 100) ⇒ Client


2
3
4
5
6
7
8
9
10
11
12
# File 'lib/puppet/http/client.rb', line 2

def initialize(pool: Puppet::Network::HTTP::Pool.new, ssl_context: nil, redirect_limit: 10, retry_limit: 100)
  @pool = pool
  @default_headers = {
    'X-Puppet-Version' => Puppet.version,
    'User-Agent' => Puppet[:http_user_agent],
  }.freeze
  @default_ssl_context = ssl_context
  @redirector = Puppet::HTTP::Redirector.new(redirect_limit)
  @retry_after_handler = Puppet::HTTP::RetryAfterHandler.new(retry_limit, Puppet[:runinterval])
  @resolvers = build_resolvers
end

Instance Method Details

#closeObject


69
70
71
# File 'lib/puppet/http/client.rb', line 69

def close
  @pool.close
end

#connect(uri, ssl_context: nil, &block) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet/http/client.rb', line 18

def connect(uri, ssl_context: nil, &block)
  ctx = ssl_context ? ssl_context : default_ssl_context
  site = Puppet::Network::HTTP::Site.from_uri(uri)
  verifier = Puppet::SSL::Verifier.new(site.host, ctx)

  @pool.with_connection(site, verifier) do |http|
    if block_given?
      handle_post_connect(uri, http, &block)
    end
  end
rescue Puppet::HTTP::HTTPError
  raise
rescue => e
  raise Puppet::HTTP::ConnectionError.new(_("Failed to connect to %{uri}: %{message}") % {uri: uri, message: e.message}, e)
end

#create_sessionObject


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

def create_session
  Puppet::HTTP::Session.new(self, @resolvers)
end

#get(url, headers: {}, params: {}, ssl_context: nil, user: nil, password: nil, &block) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/puppet/http/client.rb', line 34

def get(url, headers: {}, params: {}, ssl_context: nil, user: nil, password: nil, &block)
  query = encode_params(params)
  unless query.empty?
    url = url.dup
    url.query = query
  end

  request = Net::HTTP::Get.new(url, @default_headers.merge(headers))

  execute_streaming(request, ssl_context: ssl_context, user: user, password: password) do |response|
    if block_given?
      yield response
    else
      response.read_body
    end
  end
end

#put(url, headers: {}, params: {}, content_type:, body:, ssl_context: nil, user: nil, password: nil) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/puppet/http/client.rb', line 52

def put(url, headers: {}, params: {}, content_type:, body:, ssl_context: nil, user: nil, password: nil)
  query = encode_params(params)
  unless query.empty?
    url = url.dup
    url.query = query
  end

  request = Net::HTTP::Put.new(url, @default_headers.merge(headers))
  request.body = body
  request['Content-Length'] = body.bytesize
  request['Content-Type'] = content_type

  execute_streaming(request, ssl_context: ssl_context, user: user, password: password) do |response|
    response.read_body
  end
end