Skip to main content
Kinetic Community

Sample Client - Ruby

Sample Ruby client used to make Kinetic Task API requests.

Source

The sample code below utilizes Ruby standard libraries, and does not require any supporting packages.

 

Download: kinetic_task_rest_client.rb

require 'net/http'
require 'uri'

# Initialize the URI (path to the API)
@url = URI.parse('http://SERVER:PORT/kineticTask/rest/v1/Trigger/createStart')
# Initialize the headers necessary for the request (in this case, use the 
# X-Simulate-Success header to simulate a successful API request).
headers = {
  'X-Simulate-Success' => 'KS0000000_SIMULATED_TRIGGER_ID'
}

# Build the parameters necessary for the request
parameters = {
  'source_name' => 'Sample',
  'source_group' => 'Kinetic Service Center > iPad Request',
  'source_id' => 'AG005056b50003ralGTwMDGfEgBLzN',
  'tree_name' => 'Procure iPad'
}
# Make the API request and store the response
response = Net::HTTP.new(@url.host, @url.port).start do |http|
  # Initialize the request
  request = Net::HTTP::Post.new(@url.path, headers)
  # Set the form data
  request.form_data = parameters
  # Execute the API request
  http.request(request)
end

# If the response code returned is 200, indicating the API responded with a success
if response.is_a?(Net::HTTPSuccess)
  # Reference to the API message
  message = response['X-Message']
  # Reference to the generated ID value
  trigger_id = response['X-Id']
  # Here is where additional logic would go if the calling application needs to do
  # anything with the generated trigger ID or the API response message
  puts "#{trigger_id}: #{message}"
# If the response code returned is 400, indicating the API responded with a failure
elsif response.is_a?(Net::HTTPBadRequest)
  # Reference to the API Message
  message = response['X-Error-Message']

  # Here is where additional logic should go to handle invalid API requests (such as
  # an invalid token, or an invalid source_name/source_group/tree_name combination).
  $stderr.puts(message)
# If the response code returned is 401 (unauthorized), indicating the applied KSL policies
# denied the request
elsif response.is_a?(Net::HTTPUnauthorized)
  # Reference to the API Message
  message = response['X-Error-Message']
  # Here is where additional logic should go to handle unauthorized API requests (IE a
  # request that is denied by the applied KSL policies).  The 'message' will be a comma
  # separated list of failed policy messages.
  $stderr.puts("You are not authorized to access this resource: #{message}")
# If the status code returned is anything else, such as when the server is not accessible
else
  # Here is where logic dealing with an unexpected server exception would go
  $stderr.puts(
    "Response\n" <<
    "    HTTP/#{response.http_version} #{response.code} #{response.message}\n" <<
    "    #{response.to_hash.collect{|name,value| "#{name}: #{value}"}.join("\n    ")}\n" <<
    "Content\n" <<
    "    #{response.body.gsub(/\n/, "\n    ")}"
  )
end