Module: Chef::ChefFS::RawRequest

Defined in:
lib/chef/chef_fs/raw_request.rb

Class Method Summary collapse

Class Method Details

.api_request(chef_rest, method, url, headers = {}, data = false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chef/chef_fs/raw_request.rb', line 12

def self.api_request(chef_rest, method, url, headers={}, data=false)
  json_body = data
  #        json_body = data ? Chef::JSONCompat.to_json(data) : nil
  # Force encoding to binary to fix SSL related EOFErrors
  # cf. http://tickets.opscode.com/browse/CHEF-2363
  # http://redmine.ruby-lang.org/issues/5233
  #        json_body.force_encoding(Encoding::BINARY) if json_body.respond_to?(:force_encoding)
  headers = build_headers(chef_rest, method, url, headers, json_body)

  chef_rest.retriable_rest_request(method, url, json_body, headers) do |rest_request|
    response = rest_request.call {|r| r.read_body}

    response_body = chef_rest.decompress_body(response)

    if response.kind_of?(Net::HTTPSuccess)
      response_body
    elsif redirect_location = redirected_to(response)
      if [:GET, :HEAD].include?(method)
        chef_rest.follow_redirect do
          api_request(chef_rest, method, chef_rest.create_url(redirect_location))
        end
      else
        raise Exceptions::InvalidRedirect, "#{method} request was redirected from #{url} to #{redirect_location}. Only GET and HEAD support redirects."
      end
    else
      # have to decompress the body before making an exception for it. But the body could be nil.
      response.body.replace(chef_rest.decompress_body(response)) if response.body.respond_to?(:replace)

      if response['content-type'] =~ /json/
        exception = response_body
        msg = "HTTP Request Returned #{response.code} #{response.message}: "
        msg << (exception["error"].respond_to?(:join) ? exception["error"].join(", ") : exception["error"].to_s)
        Chef::Log.info(msg)
      end
      response.error!
    end
  end
end

.raw_json(chef_rest, api_path) ⇒ Object



4
5
6
# File 'lib/chef/chef_fs/raw_request.rb', line 4

def self.raw_json(chef_rest, api_path)
  JSON.parse(raw_request(chef_rest, api_path), :create_additions => false)
end

.raw_request(chef_rest, api_path) ⇒ Object



8
9
10
# File 'lib/chef/chef_fs/raw_request.rb', line 8

def self.raw_request(chef_rest, api_path)
  api_request(chef_rest, :GET, chef_rest.create_url(api_path), {}, false)
end