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

Send an HTTP DELETE request to the path

Parameters

path

path part of the request URL

# File lib/chef/target_io/train/http.rb, line 50
def delete(path, headers = {})
  request(:DELETE, path, headers)
end
get(path, headers = {}) click to toggle source

Send an HTTP GET request to the path

Parameters

path

The path to GET

# File lib/chef/target_io/train/http.rb, line 26
def get(path, headers = {})
  request(:GET, path, headers)
end
head(path, headers = {}) click to toggle source

Send an HTTP HEAD request to the path

Parameters

path

path part of the request URL

# File lib/chef/target_io/train/http.rb, line 18
def head(path, headers = {})
  request(:HEAD, path, headers)
end
post(path, json, headers = {}) click to toggle source

Send an HTTP POST request to the path

Parameters

path

path part of the request URL

# File lib/chef/target_io/train/http.rb, line 42
def post(path, json, headers = {})
  request(:POST, path, headers, json)
end
put(path, json, headers = {}) click to toggle source

Send an HTTP PUT request to the path

Parameters

path

path part of the request URL

# File lib/chef/target_io/train/http.rb, line 34
def put(path, json, headers = {})
  request(:PUT, path, headers, json)
end
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