Skip to content

Instantly share code, notes, and snippets.

@nikhilv
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikhilv/76cc9698d0afa4f1d9ab to your computer and use it in GitHub Desktop.
Save nikhilv/76cc9698d0afa4f1d9ab to your computer and use it in GitHub Desktop.
May 2014 hackathon, commander + dashing
require "./lib/ElectricCommander"
ec = ElectricCommander.new("192.168.158.14", "8000","json","0");
ec.login("admin","changeme")
result = ec.countObjects("job","2014-05-29T00:00:00.000Z")
puts result
ec.logout
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
class ElectricCommander
$sessionId = ""
def initialize(server, port,format,debug)
# Instance variables
@server = server
@port = port
@format = format
@debug = debug
@uri = URI("https://#{@server}:#{@port}/")
end
def login (user,password)
http = Net::HTTP.new(@uri.host,@uri.port)
# Create the request for the login
req = Net::HTTP::Post.new(@uri.path, initheader = {'Content-Type' =>'application/json'})
req.body = <<EOF
{"version":"2.2","timeout":180,"sessionId":"123","requests":[{"parameters":{"password":"#{password}","userName":"#{user}"},"requestId":1,"operation":"login"}]}
EOF
# Send the request
res = http.request(req)
# Response is in the form of
# {
# "requestId" : "1",
# "sessionId" : "SESSION_ID",
# "userName" : "admin"
#}
if !res.code == "200"
puts "ERROR: Could not login"
end
# Parse response from the commander server
resultJSON = JSON.parse(res.body)
# Get the sessionId from the response and store it in a global variable
$sessionId = resultJSON["responses"][0]["sessionId"]
end
def logout
http = Net::HTTP.new(@uri.host,@uri.port)
# Create the request for the login
req = Net::HTTP::Post.new(@uri.path, initheader = {'Content-Type' =>'application/json'})
req.body = <<EOF
{"version":"2.2","timeout":180,"sessionId":"#{$sessionId}","requests":[{"requestId":1,"operation":"logout"}]}
EOF
# Send the request
res = http.request(req)
if !res.code == "200"
puts "ERROR: Could not login"
end
end
def countObjects(objectType, createdAfterDate)
http = Net::HTTP.new(@uri.host,@uri.port)
# Create the request for the login
req = Net::HTTP::Post.new(@uri.path, initheader = {'Content-Type' =>'application/json'})
req.body = <<EOF
{"version":"2.2","timeout":180,"sessionId":"#{$sessionId}","requests":[{"parameters":{"filter":[{"operator":"and","filter":[{"operator":"equals","operand1":"1","propertyName":"preflight"},{"operator":"greaterThan","operand1":"#{createdAfterDate}","propertyName":"createTime"}]}],"objectType":"#{objectType}"},"requestId":1,"operation":"countObjects"}]}
EOF
# Send the request
res = http.request(req)
if !res.code == "200"
puts "ERROR: Could not login"
end
resultJSON = JSON.parse(res.body)
return resultJSON["responses"][0]["count"]
end
# DEBUG purposes only
def emitSessionId
puts "The sessionId is: #{$sessionId}"
end
end
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
commanderServer = "FILLMEIN"
httpPort = "8000"
uri = URI("https://#{commanderServer}:#{httpPort}/")
http = Net::HTTP.new(uri.host,uri.port)
# Create the request for the login
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
req.body = <<EOF
{"version":"2.2","timeout":180,"sessionId":"123","requests":[{"parameters":{"password":"PASSWORDHERE","userName":"USERNAMEHERE"},"requestId":1,"operation":"login"}]}
EOF
# Send the request
res = http.request(req)
# Response is in the form of
# {
# "requestId" : "1",
# "sessionId" : "SESSION_ID",
# "userName" : "admin"
#}
if !res.code == "200"
puts "ERROR: Could not login"
end
# Parse response from the commander server
resultJSON = JSON.parse(res.body)
# Get the sessionId from the response
sessionId = resultJSON["responses"][0]["sessionId"]
## DEBUG print out the sessionId
## puts sessionId
# Create the request for logout
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
req.body = <<EOF
{"version":"2.2","timeout":180,"sessionId":"#{sessionId}","requests":[{"requestId":1,"operation":"logout"}]}
EOF
# Send the logout request
res = http.request(req)
# Check for error
if !res.code == "200"
puts "ERROR: Could not logout"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment