Class: Puppet::Network::HTTP::Connection
- Includes:
- Authentication
- Defined in:
- lib/puppet/network/http/connection.rb
Overview
This class provides simple methods for issuing various types of HTTP requests. It’s interface is intended to mirror Ruby’s Net::HTTP object, but it provides a few important bits of additional functionality. Notably:
-
Any HTTPS requests made using this class will use Puppet’s SSL certificate configuration for their authentication, and
-
Provides some useful error handling for any SSL errors that occur during a request.
Constant Summary collapse
- OPTION_DEFAULTS =
{ :use_ssl => true, :verify => nil, :redirect_limit => 10, }
Instance Method Summary collapse
-
#address ⇒ Object
The address to connect to.
- #delete(path, headers = {'Depth' => 'Infinity'}, options = {}) ⇒ Object
- #get(path, headers = {}, options = {}) ⇒ Object
- #head(path, headers = {}, options = {}) ⇒ Object
-
#initialize(host, port, options = {}) ⇒ Connection
constructor
private
Creates a new HTTP client connection to ‘host`:`port`.
-
#port ⇒ Object
The port to connect to.
- #post(path, data, headers = nil, options = {}) ⇒ Object
- #put(path, data, headers = nil, options = {}) ⇒ Object
- #request(method, *args) ⇒ Object
-
#request_get(*args, &block) ⇒ Object
TODO: These are proxies for the Net::HTTP#request_* methods, which are almost the same as the “get”, “post”, etc.
- #request_head(*args, &block) ⇒ Object
- #request_post(*args, &block) ⇒ Object
-
#use_ssl? ⇒ Boolean
Whether to use ssl.
Methods included from Authentication
Constructor Details
#initialize(host, port, options = {}) ⇒ Connection
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 correct way to obtain a connection is to use one of the factory methods on Puppet::Network::HttpPool
Creates a new HTTP client connection to ‘host`:`port`.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/puppet/network/http/connection.rb', line 51 def initialize(host, port, = {}) @host = host @port = port = .keys - OPTION_DEFAULTS.keys raise Puppet::Error, "Unrecognized option(s): #{.map(&:inspect).sort.join(', ')}" unless .empty? = OPTION_DEFAULTS.merge() @use_ssl = [:use_ssl] @verify = [:verify] @redirect_limit = [:redirect_limit] @site = Puppet::Network::HTTP::Site.new(@use_ssl ? 'https' : 'http', host, port) @pool = Puppet.lookup(:http_pool) end |
Instance Method Details
#address ⇒ Object
The address to connect to.
149 150 151 |
# File 'lib/puppet/network/http/connection.rb', line 149 def address @site.host end |
#delete(path, headers = {'Depth' => 'Infinity'}, options = {}) ⇒ Object
102 103 104 |
# File 'lib/puppet/network/http/connection.rb', line 102 def delete(path, headers = {'Depth' => 'Infinity'}, = {}) request_with_redirects(Net::HTTP::Delete.new(path, headers), ) end |
#get(path, headers = {}, options = {}) ⇒ Object
75 76 77 |
# File 'lib/puppet/network/http/connection.rb', line 75 def get(path, headers = {}, = {}) request_with_redirects(Net::HTTP::Get.new(path, headers), ) end |
#head(path, headers = {}, options = {}) ⇒ Object
94 95 96 |
# File 'lib/puppet/network/http/connection.rb', line 94 def head(path, headers = {}, = {}) request_with_redirects(Net::HTTP::Head.new(path, headers), ) end |
#port ⇒ Object
The port to connect to.
154 155 156 |
# File 'lib/puppet/network/http/connection.rb', line 154 def port @site.port end |
#post(path, data, headers = nil, options = {}) ⇒ Object
84 85 86 87 88 |
# File 'lib/puppet/network/http/connection.rb', line 84 def post(path, data, headers = nil, = {}) request = Net::HTTP::Post.new(path, headers) request.body = data request_with_redirects(request, ) end |
#put(path, data, headers = nil, options = {}) ⇒ Object
111 112 113 114 115 |
# File 'lib/puppet/network/http/connection.rb', line 111 def put(path, data, headers = nil, = {}) request = Net::HTTP::Put.new(path, headers) request.body = data request_with_redirects(request, ) end |
#request(method, *args) ⇒ Object
117 118 119 |
# File 'lib/puppet/network/http/connection.rb', line 117 def request(method, *args) self.send(method, *args) end |
#request_get(*args, &block) ⇒ Object
TODO: These are proxies for the Net::HTTP#request_* methods, which are almost the same as the “get”, “post”, etc. methods that we’ve ported above, but they are able to accept a code block and will yield to it, which is necessary to stream responses, e.g. file content. For now we’re not funneling these proxy implementations through our #request method above, so they will not inherit the same error handling. In the future we may want to refactor these so that they are funneled through that method and do inherit the error handling.
129 130 131 132 133 |
# File 'lib/puppet/network/http/connection.rb', line 129 def request_get(*args, &block) with_connection(@site) do |connection| connection.request_get(*args, &block) end end |
#request_head(*args, &block) ⇒ Object
135 136 137 138 139 |
# File 'lib/puppet/network/http/connection.rb', line 135 def request_head(*args, &block) with_connection(@site) do |connection| connection.request_head(*args, &block) end end |
#request_post(*args, &block) ⇒ Object
141 142 143 144 145 |
# File 'lib/puppet/network/http/connection.rb', line 141 def request_post(*args, &block) with_connection(@site) do |connection| connection.request_post(*args, &block) end end |
#use_ssl? ⇒ Boolean
Whether to use ssl
159 160 161 |
# File 'lib/puppet/network/http/connection.rb', line 159 def use_ssl? @site.use_ssl? end |