Class: Chef::REST
Overview
Chef::REST
Chef’s custom REST client with built-in JSON support and RSA signed header authentication.
Direct Known Subclasses
Constant Summary collapse
- RESTRequest =
Backwards compatibility for things that use Chef::REST::RESTRequest or its constants
HTTP::HTTPRequest
Instance Attribute Summary collapse
-
#authenticator ⇒ Object
readonly
Returns the value of attribute authenticator.
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#redirect_limit ⇒ Object
Returns the value of attribute redirect_limit.
-
#sign_on_redirect ⇒ Object
Returns the value of attribute sign_on_redirect.
-
#url ⇒ Object
Returns the value of attribute url.
Attributes inherited from HTTP
Instance Method Summary collapse
- #auth_credentials ⇒ Object
- #authentication_headers(method, url, json_body = nil) ⇒ Object
- #client_name ⇒ Object
-
#decompress_body(body) ⇒ Object
DEPRECATED.
-
#fetch(path, headers = {}) ⇒ Object
Streams a download to a tempfile, then yields the tempfile to a block.
- #follow_redirect ⇒ Object
-
#get(path, raw = false, headers = {}) ⇒ Object
(also: #get_rest)
Send an HTTP GET request to the path.
- #http_client(base_url = nil) ⇒ Object
-
#initialize(url, client_name = Chef::Config[:node_name], signing_key_filename = Chef::Config[:client_key], options = {}) ⇒ REST
constructor
Create a REST client object.
-
#raw_http_request(method, path, headers, data) ⇒ Object
Do a HTTP request where no middleware is loaded (e.g. JSON input/output conversion) but the standard Chef Authentication headers are added to the request.
-
#retriable_http_request(method, url, req_body, headers) ⇒ Object
(also: #retriable_rest_request)
Deprecated: Responsibilities of this method have been split up.
- #sign_requests? ⇒ Boolean
- #signing_key ⇒ Object
- #signing_key_filename ⇒ Object
-
#streaming_request(url, headers, &block) ⇒ Object
Customized streaming behavior; sets the accepted content type to “/” if not otherwise specified for compatibility purposes.
Methods inherited from HTTP
#delete, #head, #last_response, middlewares, #post, #put, #request, use
Constructor Details
#initialize(url, client_name = Chef::Config[:node_name], signing_key_filename = Chef::Config[:client_key], options = {}) ⇒ REST
Create a REST 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_rest with ‘nodes’ will make an HTTP GET request to localhost:4000/nodes
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/chef/rest.rb', line 57 def initialize(url, client_name=Chef::Config[:node_name], signing_key_filename=Chef::Config[:client_key], ={}) [:client_name] = client_name [:signing_key_filename] = signing_key_filename super(url, ) @decompressor = Decompressor.new() @authenticator = Authenticator.new() @middlewares << JSONInput.new() @middlewares << JSONToModelOutput.new() @middlewares << CookieManager.new() @middlewares << @decompressor @middlewares << @authenticator end |
Instance Attribute Details
#authenticator ⇒ Object (readonly)
Returns the value of attribute authenticator.
51 52 53 |
# File 'lib/chef/rest.rb', line 51 def authenticator @authenticator end |
#cookies ⇒ Object
Returns the value of attribute cookies.
49 50 51 |
# File 'lib/chef/rest.rb', line 49 def @cookies end |
#redirect_limit ⇒ Object
Returns the value of attribute redirect_limit.
49 50 51 |
# File 'lib/chef/rest.rb', line 49 def redirect_limit @redirect_limit end |
#sign_on_redirect ⇒ Object
Returns the value of attribute sign_on_redirect.
49 50 51 |
# File 'lib/chef/rest.rb', line 49 def sign_on_redirect @sign_on_redirect end |
#url ⇒ Object
Returns the value of attribute url.
49 50 51 |
# File 'lib/chef/rest.rb', line 49 def url @url end |
Instance Method Details
#auth_credentials ⇒ Object
76 77 78 |
# File 'lib/chef/rest.rb', line 76 def auth_credentials authenticator.auth_credentials end |
#authentication_headers(method, url, json_body = nil) ⇒ Object
193 194 195 |
# File 'lib/chef/rest.rb', line 193 def authentication_headers(method, url, json_body=nil) authenticator.authentication_headers(method, url, json_body) end |
#client_name ⇒ Object
80 81 82 |
# File 'lib/chef/rest.rb', line 80 def client_name authenticator.client_name end |
#decompress_body(body) ⇒ Object
DEPRECATED
189 190 191 |
# File 'lib/chef/rest.rb', line 189 def decompress_body(body) @decompressor.decompress_body(body) end |
#fetch(path, headers = {}) ⇒ Object
Streams a download to a tempfile, then yields the tempfile to a block. After the download, the tempfile will be closed and unlinked. If you rename the tempfile, it will not be deleted. Beware that if the server streams infinite content, this method will stream it until you run out of disk space.
121 122 123 |
# File 'lib/chef/rest.rb', line 121 def fetch(path, headers={}) streaming_request(create_url(path), headers) {|tmp_file| yield tmp_file } end |
#follow_redirect ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/chef/rest.rb', line 169 def follow_redirect unless @sign_on_redirect @authenticator.sign_request = false end super ensure @authenticator.sign_request = true end |
#get(path, raw = false, headers = {}) ⇒ Object Also known as: get_rest
Send an HTTP GET request to the path
Using this method to fetch a file is considered deprecated.
Parameters
- path
-
The path to GET
- raw
-
Whether you want the raw body returned, or JSON inflated. Defaults
to JSON inflated.
100 101 102 103 104 105 106 |
# File 'lib/chef/rest.rb', line 100 def get(path, raw=false, headers={}) if raw streaming_request(path, headers) else request(:GET, path, headers) end end |
#http_client(base_url = nil) ⇒ Object
180 181 182 183 |
# File 'lib/chef/rest.rb', line 180 def http_client(base_url=nil) base_url ||= url BasicClient.new(base_url, :ssl_policy => Chef::HTTP::APISSLPolicy) end |
#raw_http_request(method, path, headers, data) ⇒ Object
Do a HTTP request where no middleware is loaded (e.g. JSON input/output conversion) but the standard Chef Authentication headers are added to the request.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/chef/rest.rb', line 130 def raw_http_request(method, path, headers, data) url = create_url(path) method, url, headers, data = @authenticator.handle_request(method, url, headers, data) response, rest_request, return_value = send_http_request(method, url, headers, data) 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 |
#retriable_http_request(method, url, req_body, headers) ⇒ Object Also known as: retriable_rest_request
Deprecated: Responsibilities of this method have been split up. The #http_client is now responsible for making individual requests, while #retrying_http_errors handles error/retry logic.
150 151 152 153 154 155 156 157 158 |
# File 'lib/chef/rest.rb', line 150 def retriable_http_request(method, url, req_body, headers) rest_request = Chef::HTTP::HTTPRequest.new(method, url, req_body, headers) Chef::Log.debug("Sending HTTP Request via #{method} to #{url.host}:#{url.port}#{rest_request.path}") (url) do yield rest_request end end |
#sign_requests? ⇒ Boolean
88 89 90 |
# File 'lib/chef/rest.rb', line 88 def sign_requests? authenticator.sign_requests? end |
#signing_key ⇒ Object
84 85 86 |
# File 'lib/chef/rest.rb', line 84 def signing_key authenticator.raw_key end |
#signing_key_filename ⇒ Object
72 73 74 |
# File 'lib/chef/rest.rb', line 72 def signing_key_filename authenticator.signing_key_filename end |
#streaming_request(url, headers, &block) ⇒ Object
Customized streaming behavior; sets the accepted content type to “/” if not otherwise specified for compatibility purposes
162 163 164 165 |
# File 'lib/chef/rest.rb', line 162 def streaming_request(url, headers, &block) headers["Accept"] ||= "*/*" super end |