Skip to content

Instantly share code, notes, and snippets.

@alexbfree
Last active May 12, 2016 17:05
Show Gist options
  • Save alexbfree/ad6a8ba66483f403dcb717099764ee13 to your computer and use it in GitHub Desktop.
Save alexbfree/ad6a8ba66483f403dcb717099764ee13 to your computer and use it in GitHub Desktop.
An example of how to connect to the Panoptes Javascript client (see https://github.com/zooniverse/panoptes-javascript-client )
#!/usr/bin/env node
require('es6-promise').polyfill();
// staging
var PROJECTID = "1611";
var WORKFLOWID = "2315";
var SUBJECTSETID = "3700";
var glob = require('glob');
var argv = require('yargs').argv;
var request = require('request');
var auth = require('panoptes-client/lib/auth');
var api = require('panoptes-client/lib/api-client');
// this should work and tell us staging or production:
// console.log ("Connected to ",api.host);
// (can change by setting NODE_ENV=staging or NODE_ENV=production)
var subject_set_name = null;
var workflow_name = null;
function signIn() {
login = {
login: argv.u,
password: argv.p,
remember_me: true
}
return auth.signIn(login);
}
function testGetWorkflow() {
console.log(`Doing a test GET of workflow ${WORKFLOWID}...`);
var subjectSet = api.type('workflows').get(WORKFLOWID)
.then( function (workflow) {
workflow_name = workflow.display_name
console.log(` Got workflow ${workflow.id}: '${workflow_name}'`);
})
.catch(function (error) {
console.log(` Error getting workflow: ${error}`);
});
}
function testGetSubjectSet() {
console.log(`Doing a test GET of subject set ${SUBJECTSETID}...`);
var subjectSet = api.type('subject_sets').get(SUBJECTSETID)
.then( function (subject_set) {
subject_set_name = subject_set.display_name
console.log(` Got subject set ${subject_set.id}: '${subject_set_name}'`);
})
.catch(function (error) {
console.log(` Error getting subject set ${error}`);
});
}
function testGetSubjectMessageSet() {
console.log(`Doing a test get of subject message set for subject set ${SUBJECTSETID} and workflow '${WORKFLOWID}...`);
api.type('set_member_subjects').get({subject_set_id: SUBJECTSETID, workflow: WORKFLOWID})
.then( function (setMemberSubjects) {
console.log(`Found subject message set for subject set '${subject_set_name}' and workflow '${workflow_name}' with ${setMemberSubjects.length} linked subjects.`);
console.log("Getting subjects...");
setMemberSubjects.forEach( function (setMemberSubject) {
sms_id = setMemberSubject.id;
subject_id = setMemberSubject.links.subject;
// note these are equal
api.type('subjects').get(subject_id)
.then( function (subject) {
console.log(` ID: ${subject.id}`);
console.log(" Media:");
subject.locations.forEach( function (location) {
for (var type in location) {
if (location.hasOwnProperty(type)) {
console.log(` ${location[type]} (${type})`);
}
}
});
console.log(" Metadata:");
console.log(` ${JSON.stringify(subject.metadata)}`);
})
.catch(function (error) {
console.log(`Error getting subject ${subject_id}`, error);
});
});
})
.catch(function (error) {
console.log(`Error getting subject message set: {$error}`);
});
}
signIn()
.then( function (user) {
api.update({'params.admin' : user.admin})
Promise.all([
api.type('workflows').get(WORKFLOWID),
api.type('projects').get(PROJECTID)
])
.then( function (values) {
console.log(`WORKFLOW ${values[0].display_name}`);
console.log(`PROJECT ${values[1].display_name}`);
testGetSubjectSet();
testGetWorkflow();
testGetSubjectMessageSet();
})
.catch(function (error) {
console.log(`Error getting workflow and project: ${error}`);
});
})
.catch(function(error){
console.log('Done', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment