Class: Chef::HTTP
- Defined in:
- lib/chef/http.rb,
lib/chef/rest.rb,
lib/chef/http/simple.rb,
lib/chef/http/cookie_jar.rb,
lib/chef/http/json_input.rb,
lib/chef/http/json_output.rb,
lib/chef/http/basic_client.rb,
lib/chef/http/decompressor.rb,
lib/chef/http/http_request.rb,
lib/chef/http/ssl_policies.rb,
lib/chef/http/authenticator.rb,
lib/chef/http/cookie_manager.rb,
lib/chef/http/auth_credentials.rb,
lib/chef/http/remote_request_id.rb,
lib/chef/http/json_to_model_output.rb,
lib/chef/http/validate_content_length.rb
Overview
Chef::HTTP
Basic HTTP client, with support for adding features via middleware
Direct Known Subclasses
Defined Under Namespace
Classes: APISSLPolicy, AuthCredentials, Authenticator, BasicClient, CookieJar, CookieManager, Decompressor, DefaultSSLPolicy, HTTPRequest, JSONInput, JSONOutput, JSONToModelOutput, RemoteRequestID, Simple, StreamHandler, ValidateContentLength
Instance Attribute Summary collapse
-
#middlewares ⇒ Object
readonly
Returns the value of attribute middlewares.
-
#redirect_limit ⇒ Object
readonly
Returns the value of attribute redirect_limit.
-
#sign_on_redirect ⇒ Object
readonly
Returns the value of attribute sign_on_redirect.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
-
#delete(path, headers = {}) ⇒ Object
Send an HTTP DELETE request to the path.
-
#get(path, headers = {}) ⇒ Object
Send an HTTP GET request to the path.
-
#head(path, headers = {}) ⇒ Object
Send an HTTP HEAD request to the path.
- #http_client(base_url = nil) ⇒ Object
-
#initialize(url, options = {}) ⇒ HTTP
constructor
Create a HTTP client object.
-
#last_response ⇒ Object
This is only kept around to provide access to cache control data in lib/chef/provider/remote_file/http.rb Find a better API.
-
#post(path, json, headers = {}) ⇒ Object
Send an HTTP POST request to the path.
-
#put(path, json, headers = {}) ⇒ Object
Send an HTTP PUT request to the path.
-
#request(method, path, headers = {}, data = false) ⇒ Object
Makes an HTTP request to
path
with the givenmethod
,headers
, anddata
(if applicable). -
#streaming_request(path, headers = {}, &block) ⇒ Object
Makes a streaming download request, streaming the response body to a tempfile.
Constructor Details
#initialize(url, options = {}) ⇒ HTTP
Create a HTTP client object. The supplied url
is used as the base for all subsequent requests. For example, when initialized with a base url localhost:4000, a call to get
with ‘nodes’ will make an HTTP GET request to localhost:4000/nodes
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/chef/http.rb', line 84 def initialize(url, ={}) @url = url @default_headers = [:headers] || {} @sign_on_redirect = true @redirects_followed = 0 @redirect_limit = 10 @middlewares = [] self.class.middlewares.each do |middleware_class| @middlewares << middleware_class.new() end end |
Instance Attribute Details
#middlewares ⇒ Object (readonly)
Returns the value of attribute middlewares.
78 79 80 |
# File 'lib/chef/http.rb', line 78 def middlewares @middlewares end |
#redirect_limit ⇒ Object (readonly)
Returns the value of attribute redirect_limit.
76 77 78 |
# File 'lib/chef/http.rb', line 76 def redirect_limit @redirect_limit end |
#sign_on_redirect ⇒ Object (readonly)
Returns the value of attribute sign_on_redirect.
75 76 77 |
# File 'lib/chef/http.rb', line 75 def sign_on_redirect @sign_on_redirect end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
74 75 76 |
# File 'lib/chef/http.rb', line 74 def url @url end |
Class Method Details
.middlewares ⇒ Object
66 67 68 |
# File 'lib/chef/http.rb', line 66 def self.middlewares @middlewares ||= [] end |
.use(middleware_class) ⇒ Object
70 71 72 |
# File 'lib/chef/http.rb', line 70 def self.use(middleware_class) middlewares << middleware_class end |
Instance Method Details
#delete(path, headers = {}) ⇒ Object
Send an HTTP DELETE request to the path
Parameters
- path
-
path part of the request URL
133 134 135 |
# File 'lib/chef/http.rb', line 133 def delete(path, headers={}) request(:DELETE, path, headers) end |
#get(path, headers = {}) ⇒ Object
Send an HTTP GET request to the path
Parameters
- path
-
The path to GET
109 110 111 |
# File 'lib/chef/http.rb', line 109 def get(path, headers={}) request(:GET, path, headers) end |
#head(path, headers = {}) ⇒ Object
Send an HTTP HEAD request to the path
Parameters
- path
-
path part of the request URL
101 102 103 |
# File 'lib/chef/http.rb', line 101 def head(path, headers={}) request(:HEAD, path, headers) end |
#http_client(base_url = nil) ⇒ Object
198 199 200 201 |
# File 'lib/chef/http.rb', line 198 def http_client(base_url=nil) base_url ||= url BasicClient.new(base_url) end |
#last_response ⇒ Object
This is only kept around to provide access to cache control data in lib/chef/provider/remote_file/http.rb Find a better API.
400 401 402 |
# File 'lib/chef/http.rb', line 400 def last_response @last_response end |
#post(path, json, headers = {}) ⇒ Object
Send an HTTP POST request to the path
Parameters
- path
-
path part of the request URL
125 126 127 |
# File 'lib/chef/http.rb', line 125 def post(path, json, headers={}) request(:POST, path, headers, json) end |
#put(path, json, headers = {}) ⇒ Object
Send an HTTP PUT request to the path
Parameters
- path
-
path part of the request URL
117 118 119 |
# File 'lib/chef/http.rb', line 117 def put(path, json, headers={}) request(:PUT, path, headers, json) end |
#request(method, path, headers = {}, data = false) ⇒ Object
Makes an HTTP request to path
with the given method
, headers
, and data
(if applicable).
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/chef/http.rb', line 139 def request(method, path, headers={}, data=false) url = create_url(path) method, url, headers, data = apply_request_middleware(method, url, headers, data) response, rest_request, return_value = send_http_request(method, url, headers, data) response, rest_request, return_value = apply_response_middleware(response, rest_request, return_value) response.error! unless success_response?(response) return_value rescue Exception => exception log_failed_request(response, return_value) unless response.nil? if exception.respond_to?(:chef_rest_request=) exception.chef_rest_request = rest_request end raise end |
#streaming_request(path, headers = {}, &block) ⇒ Object
Makes a streaming download request, streaming the response body to a tempfile. If a block is given, the tempfile is passed to the block and the tempfile will automatically be unlinked after the block is executed.
If no block is given, the tempfile is returned, which means it’s up to you to unlink the tempfile when you’re done with it.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/chef/http.rb', line 162 def streaming_request(path, headers={}, &block) url = create_url(path) response, rest_request, return_value = nil, nil, nil tempfile = nil method = :GET method, url, headers, data = apply_request_middleware(method, url, headers, data) response, rest_request, return_value = send_http_request(method, url, headers, data) do |http_response| if http_response.kind_of?(Net::HTTPSuccess) tempfile = stream_to_tempfile(url, http_response) end apply_stream_complete_middleware(http_response, rest_request, return_value) end return nil if response.kind_of?(Net::HTTPRedirection) unless response.kind_of?(Net::HTTPSuccess) response.error! end if block_given? begin yield tempfile ensure tempfile && tempfile.close! end end tempfile rescue Exception => e log_failed_request(response, return_value) unless response.nil? if e.respond_to?(:chef_rest_request=) e.chef_rest_request = rest_request end raise end |