class TargetIO::TrainCompat::HTTP
Constants
- SUPPORTED_COMMANDS
Attributes
last_response[R]
Public Class Methods
new(url, options = {})
click to toggle source
# File lib/chef/target_io/train/http.rb, line 8 def initialize(url, options = {}) @url = url.is_a?(URI) ? url.to_s : url @options = options @last_response = "" end
Public Instance Methods
curl(cmd, method, url, headers, _data)
click to toggle source
Sending data is not yet supported
# File lib/chef/target_io/train/http.rb, line 96 def curl(cmd, method, url, headers, _data) cmd += headers.map { |name, value| " --header '#{name}: #{value}'" }.join cmd += " --request #{method} " cmd += url end
delete(path, headers = {})
click to toggle source
get(path, headers = {})
click to toggle source
head(path, headers = {})
click to toggle source
post(path, json, headers = {})
click to toggle source
put(path, json, headers = {})
click to toggle source
request(method, path, headers = {}, data = false)
click to toggle source
# File lib/chef/target_io/train/http.rb, line 65 def request(method, path, headers = {}, data = false) cmd = nil path = path.is_a?(URI) ? path.to_s : path headers.merge!(@options[:headers] || {}) SUPPORTED_COMMANDS.each do |command_name| executable = which(command_name).chop next if !executable || executable.empty? # There are different ways to call (constructor, argument, combination of both) full_url = if path.start_with?("http") path elsif path.empty? || @url.end_with?(path) @url else File.join(@url, path) end cmd = send(command_name.to_sym, executable, method.to_s.upcase, full_url, headers, data) break end raise "Target needs one of #{SUPPORTED_COMMANDS.join("/")} for HTTP requests to work" unless cmd connection = Chef.run_context&.transport_connection connection.run_command(cmd).stdout end
streaming_request(path, headers = {}, tempfile = nil)
click to toggle source
Used inside Chef::Provider::RemoteFile::HTTPS
# File lib/chef/target_io/train/http.rb, line 55 def streaming_request(path, headers = {}, tempfile = nil) content = get(path, headers) @last_response = content tempfile.write(content) tempfile.close tempfile end
wget(cmd, method, url, headers, _data)
click to toggle source
Sending data is not yet supported
# File lib/chef/target_io/train/http.rb, line 103 def wget(cmd, method, url, headers, _data) cmd += headers.map { |name, value| " --header '#{name}: #{value}'" }.join cmd += " --method #{method}" cmd += " --output-document=- " cmd += url end
which(cmd)
click to toggle source
extend Chef::Mixin::Which
# File lib/chef/target_io/train/http.rb, line 111 def which(cmd) connection = Chef.run_context&.transport_connection connection.run_command("which #{cmd}").stdout end