Class: Chef::REST

Inherits:
HTTP
  • Object
show all
Defined in:
lib/chef/rest.rb,
lib/chef/rest.rb

Overview

Chef::REST

Chef’s custom REST client with built-in JSON support and RSA signed header authentication.

Constant Summary collapse

RESTRequest =

Backwards compatibility for things that use Chef::REST::RESTRequest or its constants

HTTP::HTTPRequest

Instance Attribute Summary collapse

Attributes inherited from HTTP

#keepalives, #middlewares, #options

Instance Method Summary collapse

Methods inherited from HTTP

#delete, #head, #http_client, #last_response, middlewares, #post, #put, #request, #streaming_request_with_progress, 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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/rest.rb', line 61

def initialize(url, client_name = Chef::Config[:node_name], signing_key_filename = Chef::Config[:client_key], options = {})
  Chef.deprecated(:chef_rest, "Chef::REST is deprecated. Please use Chef::ServerAPI, or investigate Ridley or ChefAPI.")

  signing_key_filename = nil if chef_zero_uri?(url)

  options = options.dup
  options[:client_name] = client_name
  options[:signing_key_filename] = signing_key_filename

  super(url, options)

  @decompressor = Decompressor.new(options)
  @authenticator = Authenticator.new(options)
  @request_id = RemoteRequestID.new(options)

  @middlewares << JSONInput.new(options)
  @middlewares << JSONToModelOutput.new(options)
  @middlewares << CookieManager.new(options)
  @middlewares << @decompressor
  @middlewares << @authenticator
  @middlewares << @request_id

  # ValidateContentLength should come after Decompressor
  # because the order of middlewares is reversed when handling
  # responses.
  @middlewares << ValidateContentLength.new(options)
end

Instance Attribute Details

#authenticatorObject (readonly)

Returns the value of attribute authenticator.



55
56
57
# File 'lib/chef/rest.rb', line 55

def authenticator
  @authenticator
end

#cookiesObject

Returns the value of attribute cookies.



53
54
55
# File 'lib/chef/rest.rb', line 53

def cookies
  @cookies
end

#redirect_limitObject

Returns the value of attribute redirect_limit.



53
54
55
# File 'lib/chef/rest.rb', line 53

def redirect_limit
  @redirect_limit
end

#sign_on_redirectObject

Returns the value of attribute sign_on_redirect.



53
54
55
# File 'lib/chef/rest.rb', line 53

def sign_on_redirect
  @sign_on_redirect
end

#urlObject

Returns the value of attribute url.



53
54
55
# File 'lib/chef/rest.rb', line 53

def url
  @url
end

Instance Method Details

#auth_credentialsObject



93
94
95
# File 'lib/chef/rest.rb', line 93

def auth_credentials
  authenticator.auth_credentials
end

#authentication_headers(method, url, json_body = nil) ⇒ Object



205
206
207
# File 'lib/chef/rest.rb', line 205

def authentication_headers(method, url, json_body = nil)
  authenticator.authentication_headers(method, url, json_body)
end

#client_nameObject



97
98
99
# File 'lib/chef/rest.rb', line 97

def client_name
  authenticator.client_name
end

#decompress_body(body) ⇒ Object

DEPRECATED



201
202
203
# File 'lib/chef/rest.rb', line 201

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.



138
139
140
# File 'lib/chef/rest.rb', line 138

def fetch(path, headers = {})
  streaming_request(create_url(path), headers) { |tmp_file| yield tmp_file }
end

#follow_redirectObject



186
187
188
189
190
191
192
193
# File 'lib/chef/rest.rb', line 186

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.


117
118
119
120
121
122
123
# File 'lib/chef/rest.rb', line 117

def get(path, raw = false, headers = {})
  if raw
    streaming_request(path, headers)
  else
    request(:GET, path, headers)
  end
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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/chef/rest.rb', line 147

def raw_http_request(method, path, headers, data)
  url = create_url(path)
  method, url, headers, data = @authenticator.handle_request(method, url, headers, data)
  method, url, headers, data = @request_id.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.



167
168
169
170
171
172
173
174
175
# File 'lib/chef/rest.rb', line 167

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}")

  retrying_http_errors(url) do
    yield rest_request
  end
end

#sign_requests?Boolean



105
106
107
# File 'lib/chef/rest.rb', line 105

def sign_requests?
  authenticator.sign_requests?
end

#signing_keyObject



101
102
103
# File 'lib/chef/rest.rb', line 101

def signing_key
  authenticator.raw_key
end

#signing_key_filenameObject



89
90
91
# File 'lib/chef/rest.rb', line 89

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



179
180
181
182
# File 'lib/chef/rest.rb', line 179

def streaming_request(url, headers, &block)
  headers["Accept"] ||= "*/*"
  super
end