Skip to content

Instantly share code, notes, and snippets.

@jmervine
Last active December 16, 2016 13:10
Show Gist options
  • Save jmervine/837bdd303798ad122fda to your computer and use it in GitHub Desktop.
Save jmervine/837bdd303798ad122fda to your computer and use it in GitHub Desktop.
Catchpoint API Examples
require 'net/https'
require 'base64'
require 'json'
require 'uri'
class Catchpoint
@@host = 'https://io.catchpoint.com'
@@prefix = '/ui/api'
@@version = 1
def initialize opts={}
@key = opts["key"]
@secret = opts["secret"]
end
def api_uri endpoint
return URI(@@host + @@prefix + "/v#{@@version}/" + endpoint)
end
def token_uri
return URI("#{@@host}#{@@prefix}/token")
end
def fetch_token
opts = {
:grant_type => 'client_credentials',
:client_id => @key,
:client_secret => @secret
}
res = Net::HTTP.post_form(token_uri, opts)
body = JSON.parse(res.body)
@token = body["access_token"]
end
def headers
return {
"Authorization" => "Bearer #{Base64.urlsafe_encode64(@token)}"
}
end
def get endpoint
# remove preceeding slash
endpoint.gsub!(/^\//, '')
fetch_token
uri = api_uri(endpoint)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
JSON.parse(http.get(uri.path, headers).body)
end
end
require 'yaml'
DEFAULT_FILE = ENV['HOME'] + '/.catchpoint'
###
# option: default :: description
# ------------------------------------
# "file": undefined :: config file to be used, default is
# '$HOME/.catchpoint'
# "[opt]": n/a :: set or overide [opt]
#
# Example:
#
# require 'maxconf'
#
# conf = MaxConf.load()
# puts conf["alias"]
# => YOUR_ALIAS
#
# conf = MaxConf.load("/path/to/catchpoint")
# puts conf["alias"]
# => YOUR_ALIAS
#
# opts = {
# "file" => "/path/to/catchpoint",
# "alias" => "ALIAS_OVERIDE"
# }
# conf = MaxConf.load(opts)
# puts conf["alias"]
# => ALIAS_OVERIDE
###
module Configurator
def self.load opts={}
if opts.class == String
opts = { "file" => opts }
end
file = opts.delete("file") || opts.delete(:file) || DEFAULT_FILE
conf = YAML.load_file(file)
# overide config with passed options
opts.each do |key, val|
conf[key] = val
end
return conf
end
end
#!/usr/bin/env ruby
require 'pp'
require './configurator'
require './catchpoint'
conf = Configurator.load()
catchpoint = Catchpoint.new(conf)
nodes = catchpoint.get("nodes")["items"]
puts "First node:"
pp nodes.first
puts "Last node:"
pp nodes.last
tests = catchpoint.get("tests")["items"]
puts "First test:"
pp tests.first
puts "Last test:"
pp tests.last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment