Class: Puppet::Network::HTTP::Connection Deprecated
- Includes:
- HTTP::ResponseConverter
- Defined in:
- lib/puppet/network/http/connection.rb
Overview
Use :http
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, :verifier => nil, :redirect_limit => 10, }
Instance Method Summary collapse
-
#address ⇒ Object
The address to connect to.
-
#delete(path, headers = { 'Depth' => 'Infinity' }, options = {}) ⇒ Object
options not recognized by this class will be ignored - no error will be thrown.
-
#get(path, headers = {}, options = {}) ⇒ Object
options not recognized by this class will be ignored - no error will be thrown.
-
#head(path, headers = {}, options = {}) ⇒ Object
options not recognized by this class will be ignored - no error will be thrown.
-
#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
options not recognized by this class will be ignored - no error will be thrown.
-
#put(path, data, headers = nil, options = {}) ⇒ Object
options not recognized by this class will be ignored - no error will be thrown.
- #request_get(*args, &block) ⇒ Object
- #request_head(*args) {|ruby_response| ... } ⇒ Object
- #request_post(*args, &block) ⇒ Object
-
#use_ssl? ⇒ Boolean
Whether to use ssl.
- #verifier ⇒ Object private
Methods included from HTTP::ResponseConverter
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`.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/puppet/network/http/connection.rb', line 47 def initialize(host, port, = {}) = .keys - OPTION_DEFAULTS.keys raise Puppet::Error, _("Unrecognized option(s): %{opts}") % { opts: .map(&:inspect).sort.join(', ') } unless .empty? = OPTION_DEFAULTS.merge() @use_ssl = [:use_ssl] if @use_ssl unless [:verifier].is_a?(Puppet::SSL::Verifier) raise ArgumentError, _("Expected an instance of Puppet::SSL::Verifier but was passed a %{klass}") % { klass: [:verifier].class } end @verifier = [:verifier] end @redirect_limit = [:redirect_limit] @site = Puppet::HTTP::Site.new(@use_ssl ? 'https' : 'http', host, port) @client = Puppet.runtime[:http] end |
Instance Method Details
#address ⇒ Object
The address to connect to.
66 67 68 |
# File 'lib/puppet/network/http/connection.rb', line 66 def address @site.host end |
#delete(path, headers = { 'Depth' => 'Infinity' }, options = {}) ⇒ Object
options not recognized by this class will be ignored - no error will be thrown.
143 144 145 146 147 148 149 150 151 |
# File 'lib/puppet/network/http/connection.rb', line 143 def delete(path, headers = { 'Depth' => 'Infinity' }, = {}) headers ||= {} [:ssl_context] ||= resolve_ssl_context [:redirect_limit] ||= @redirect_limit with_error_handling do to_ruby_response(@client.delete(to_url(path), headers: headers, options: )) end end |
#get(path, headers = {}, options = {}) ⇒ Object
options not recognized by this class will be ignored - no error will be thrown.
98 99 100 101 102 103 104 105 106 |
# File 'lib/puppet/network/http/connection.rb', line 98 def get(path, headers = {}, = {}) headers ||= {} [:ssl_context] ||= resolve_ssl_context [:redirect_limit] ||= @redirect_limit with_error_handling do to_ruby_response(@client.get(to_url(path), headers: headers, options: )) end end |
#head(path, headers = {}, options = {}) ⇒ Object
options not recognized by this class will be ignored - no error will be thrown.
129 130 131 132 133 134 135 136 137 |
# File 'lib/puppet/network/http/connection.rb', line 129 def head(path, headers = {}, = {}) headers ||= {} [:ssl_context] ||= resolve_ssl_context [:redirect_limit] ||= @redirect_limit with_error_handling do to_ruby_response(@client.head(to_url(path), headers: headers, options: )) end end |
#port ⇒ Object
The port to connect to.
71 72 73 |
# File 'lib/puppet/network/http/connection.rb', line 71 def port @site.port end |
#post(path, data, headers = nil, options = {}) ⇒ Object
options not recognized by this class will be ignored - no error will be thrown.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/puppet/network/http/connection.rb', line 113 def post(path, data, headers = nil, = {}) headers ||= {} headers['Content-Type'] ||= "application/x-www-form-urlencoded" data ||= '' [:ssl_context] ||= resolve_ssl_context [:redirect_limit] ||= @redirect_limit with_error_handling do to_ruby_response(@client.post(to_url(path), data, headers: headers, options: )) end end |
#put(path, data, headers = nil, options = {}) ⇒ Object
options not recognized by this class will be ignored - no error will be thrown.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/puppet/network/http/connection.rb', line 158 def put(path, data, headers = nil, = {}) headers ||= {} headers['Content-Type'] ||= "application/x-www-form-urlencoded" data ||= '' [:ssl_context] ||= resolve_ssl_context [:redirect_limit] ||= @redirect_limit with_error_handling do to_ruby_response(@client.put(to_url(path), data, headers: headers, options: )) end end |
#request_get(*args, &block) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/puppet/network/http/connection.rb', line 170 def request_get(*args, &block) path, headers = *args headers ||= {} = { ssl_context: resolve_ssl_context, redirect_limit: @redirect_limit } ruby_response = nil @client.get(to_url(path), headers: headers, options: ) do |response| ruby_response = to_ruby_response(response) yield ruby_response if block_given? end ruby_response end |
#request_head(*args) {|ruby_response| ... } ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/puppet/network/http/connection.rb', line 186 def request_head(*args, &block) path, headers = *args headers ||= {} = { ssl_context: resolve_ssl_context, redirect_limit: @redirect_limit } response = @client.head(to_url(path), headers: headers, options: ) ruby_response = to_ruby_response(response) yield ruby_response if block_given? ruby_response end |
#request_post(*args, &block) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/puppet/network/http/connection.rb', line 200 def request_post(*args, &block) path, data, headers = *args headers ||= {} headers['Content-Type'] ||= "application/x-www-form-urlencoded" = { ssl_context: resolve_ssl_context, redirect_limit: @redirect_limit } ruby_response = nil @client.post(to_url(path), data, headers: headers, options: ) do |response| ruby_response = to_ruby_response(response) yield ruby_response if block_given? end ruby_response end |
#use_ssl? ⇒ Boolean
Whether to use ssl
76 77 78 |
# File 'lib/puppet/network/http/connection.rb', line 76 def use_ssl? @site.use_ssl? end |
#verifier ⇒ 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.
81 82 83 |
# File 'lib/puppet/network/http/connection.rb', line 81 def verifier @verifier end |