Screen 1
In CMD.exe
npm install node-fetch
In node.exe
var fetch = require("node-fetch");
fetch("http://dewin.me/vcoffee/jobs.json").then( function(res) {
return res.text()
}).then( function(body) {
console.log(body)
}).catch( function(err) {
console.log("Err! "+err) }
)
Optional error
fetch("http://tdewin.me/vcoffee/jobs.json").then( function(res) {
return res.text()
}).then( function(body) {
console.log(body)
}).catch( function(err) {
console.log("Err! "+err) }
)
Screen 2
fetch("http://dewin.me/vcoffee/jobs.json").then( res => res.text() ).then( body => { console.log(body) }).catch( err => { console.log("Err! "+err) })
Screen 3
fetch("http://dewin.me/vcoffee/jobs.json").then( res => res.json() ).then( body => { console.log(body[0].name) }).catch( err => { console.log("Err! "+err) })
Screen 4
var maincontext = {
apiurl:"https://vac.internal.dewin.me:1281",
login:"administrator",
password:"Root123!",
sessionid:null
}
function restLogin(maincontext,success,failure) {
fetch(maincontext.apiurl+"/v2/jobs", {
method: "GET",
headers: {"Content-Type": "application/json"}
}).then(res => res.text()).then(body => {
fetch(maincontext.apiurl+"/token",{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer"
},
body: "grant_type=password&username="+maincontext.login+"&password="+maincontext.password
}).then(r => { return r.json(); }).then(function (r) {
if(r.access_token !== undefined) {
maincontext.sessionid = r.access_token
success(maincontext.sessionid)
} else {
failure("Could not extract access token from api")
}
}).catch(error =>failure("Could not login"+error));
}).catch(error=> {
failure("Something went wrong when fetching initial api page "+error)
});
}
function success(id) { console.log("I got logged in "+id) }
function failure(err) { console.log("Error "+err) }
restLogin(maincontext,success,failure)
Screen 5
function getAuthenticatedHeader(sessid) {
return { "Content-Type": "application/json",
"Authorization":"Bearer "+sessid
}
}
function getList(maincontext,success,failure,type) {
var ref = maincontext.apiurl+"/v2/"+type
fetch(ref, {
method: "GET",
headers: getAuthenticatedHeader(maincontext.sessionid)
}).then(res => res.json()).then(body => {
success(body);
}).catch(err=> {
failure(err)
})
}
function getJobs(maincontext,success,failure) {
getList(maincontext,success,failure,"Jobs")
}
var jobs = null
function successJobs(incomingjobs) {
jobs = incomingjobs
console.log("I got some jobs "+jobs[0].name+" "+jobs[0].id)
}
function failureJobs(err) {
console.log("Error "+err)
}
getJobs(maincontext,successJobs,failureJobs)
Screen 6
function actionJob(maincontext,job,jobaction,success,failure) {
var posting = '{"'+jobaction+'": null }'
var uri = maincontext.apiurl+"/v2/Jobs/"+job.id+"/action"
fetch(uri, {
method: "POST",
headers: getAuthenticatedHeader(maincontext.sessionid),
body: posting
}).then(res => res.json()).then(body => {
success(body);
}).catch(err=> {
failure(err);
})
}
function success(reply) {
console.log("Job action")
console.dir(reply)
}
function failure(err) {
console.log("Error "+err)
}
actionJob(maincontext,jobs[0],"start",success,failure)
actionJob(maincontext,jobs[0],"enable",success,failure)
actionJob(maincontext,jobs[0],"disable",success,failure)