Comparing version 2.3.0 to 2.3.1
'use strict'; | ||
var request = require('request'); | ||
var DEFAULT_FIELDS = ['callback_url', 'instruction', 'urgency', 'metadata']; | ||
var ALLOWED_FIELDS = { | ||
'categorization': | ||
['attachment', 'attachment_type', 'categories', 'category_ids', 'allow_multiple', 'layers'], | ||
'transcription': | ||
['attachment', 'attachment_type', 'fields', 'repeatable_fields'], | ||
'comparison': | ||
['attachments', 'attachment_type', 'fields', 'choices'], | ||
'annotation': | ||
['attachment', 'attachment_type', 'instruction', 'objects_to_annotate', 'with_labels', 'examples', 'min_width', 'min_height', 'layers', 'annotation_attributes'], | ||
'polygonannotation': | ||
['attachment', 'attachment_type', 'instruction', 'objects_to_annotate', 'with_labels', 'examples', 'layers', 'annotation_attributes'], | ||
'lineannotation': | ||
['attachment', 'attachment_type', 'instruction', 'objects_to_annotate', 'with_labels', 'examples', 'splines', 'layers', 'annotation_attributes'], | ||
'datacollection': ['attachment', 'attachment_type', 'fields'], | ||
'audiotranscription': ['attachment', 'attachment_type', 'verbatim'], | ||
'pointannotation': ['attachment_type','attachment', 'objects_to_annotate','with_labels', 'examples', 'layers','annotation_attributes'], | ||
'segmentannotation': ['attachment_type','attachment', 'labels', 'allow_unlabeled'] | ||
}; | ||
const TASK_TYPES = [ | ||
'categorization', | ||
'transcription', | ||
'comparison', | ||
'annotation', | ||
'polygonannotation', | ||
'lineannotation', | ||
'datacollection', | ||
'audiotranscription', | ||
'pointannotation', | ||
'segmentannotation' | ||
]; | ||
var SCALE_ENDPOINT = 'https://api.scaleapi.com/v1/'; | ||
@@ -61,3 +54,3 @@ var DEFAULT_LIMIT = 100; | ||
getrequest('task/' + taskId, this.apiKey, {}, (err, json) => { | ||
if (cb) cb(err, new Task(this, json)); | ||
cb && cb(err, new Task(this, json)); | ||
}); | ||
@@ -74,3 +67,3 @@ }; | ||
postrequest('task/' + taskId + '/cancel', this.apiKey, {}, (err, json) => { | ||
if (cb) cb(err, new Task(this, json)); | ||
cb && cb(err, new Task(this, json)); | ||
}); | ||
@@ -101,84 +94,40 @@ }; | ||
if (allowedKwargs.indexOf(property) < 0) { | ||
cb(new ScaleInvalidRequest('Illegal parameter ' + property + | ||
' for ScaleClient.tasks'), null); | ||
cb && cb( | ||
new ScaleInvalidRequest('Illegal parameter ' + property + ' for ScaleClient.tasks') | ||
); | ||
} | ||
}); | ||
if (allowedKwargs.limit === undefined) allowedKwargs.limit = DEFAULT_LIMIT; | ||
if (allowedKwargs.offset === undefined) allowedKwargs.offset = DEFAULT_OFFSET; | ||
if (allowedKwargs.limit === undefined) { | ||
allowedKwargs.limit = DEFAULT_LIMIT; | ||
} | ||
if (allowedKwargs.offset === undefined) { | ||
allowedKwargs.offset = DEFAULT_OFFSET; | ||
} | ||
getrequest('tasks', this.apiKey, params, (err, json) => { | ||
if (err) { | ||
cb && cb(err); | ||
return; | ||
} | ||
json.docs = json.docs.map(json => new Task(this, json)); | ||
if (cb) cb(err, json); | ||
cb && cb(err, json); | ||
}); | ||
}; | ||
ScaleClient.prototype.createCategorizationTask = function(params, cb) { | ||
validatePayload('categorization', params); | ||
postrequest('task/categorize', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
ScaleClient.prototype.createTask = function(taskType, params, cb) { | ||
let endpoint = `task/${taskType}`; | ||
return postrequest(endpoint, this.apiKey, params, (err, json) => { | ||
if (err) { | ||
cb && cb(err); | ||
return; | ||
} | ||
cb && cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
} | ||
ScaleClient.prototype.createTranscriptionTask = function(params, cb) { | ||
validatePayload('transcription', params); | ||
postrequest('task/transcription', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
TASK_TYPES.forEach(function(taskType) { | ||
ScaleClient.prototype['create' + capitalizeFirstLetter(taskType) + 'Task'] = function(params, cb) { | ||
return this.createTask(taskType, params, cb); | ||
}; | ||
}); | ||
ScaleClient.prototype.createComparisonTask = function(params, cb) { | ||
validatePayload('comparison', params); | ||
postrequest('task/comparison', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
ScaleClient.prototype.createAnnotationTask = function(params, cb) { | ||
validatePayload('annotation', params); | ||
postrequest('task/annotation', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
ScaleClient.prototype.createPolygonannotationTask = function(params, cb) { | ||
validatePayload('polygonannotation', params); | ||
postrequest('task/polygonannotation', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
ScaleClient.prototype.createLineannotationTask = function(params, cb) { | ||
validatePayload('lineannotation', params); | ||
postrequest('task/lineannotation', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
ScaleClient.prototype.createDatacollectionTask = function(params, cb) { | ||
validatePayload('datacollection', params); | ||
postrequest('task/datacollection', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
ScaleClient.prototype.createAudiotranscriptionTask = function(params, cb) { | ||
validatePayload('audiotranscription', params); | ||
postrequest('task/audiotranscription', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
ScaleClient.prototype.createPointannotationTask = function(params, cb) { | ||
validatePayload('pointannotation', params); | ||
postrequest('task/pointannotation', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
ScaleClient.prototype.createSegmentannotationTask = function(params, cb) { | ||
validatePayload('segmentannotation', params); | ||
postrequest('task/segmentannotation', this.apiKey, params, (err, json) => { | ||
if(cb) cb(err, new Task(this, json)); | ||
}); | ||
}; | ||
/** | ||
@@ -201,5 +150,9 @@ * A task object, containing task information. | ||
Task.prototype.refresh = function(cb) { | ||
this.client.fetchTask(this.id, task => { | ||
this.client.fetchTask(this.id, (err, task) => { | ||
if (err) { | ||
cb && cb(err); | ||
return; | ||
} | ||
Object.assign(this, task); | ||
cb(this); | ||
cb && cb(null, this); | ||
}); | ||
@@ -227,9 +180,10 @@ }; | ||
sendImmediately: true | ||
}} | ||
, function(error, response, body) { | ||
} | ||
}, | ||
function(error, response, body) { | ||
var err = null; | ||
if (error || response.statusCode != 200) { | ||
err = new ScaleException(body['error'], response.statusCode); | ||
if (error || response.statusCode !== 200) { | ||
err = new ScaleException(body && body['error'], response.statusCode); | ||
} | ||
if (cb) cb(err, body); | ||
cb && cb(err, body); | ||
}); | ||
@@ -251,7 +205,7 @@ } | ||
if (error || response.statusCode != 200) { | ||
err = new ScaleException(body['error'], response.statusCode); | ||
err = new ScaleException(body && body['error'], response.statusCode); | ||
} | ||
if (cb) cb(err, body); | ||
cb && cb(err, body); | ||
} catch (e) { | ||
if (cb) cb(e, body); | ||
cb && cb(e, body); | ||
} | ||
@@ -261,2 +215,6 @@ }); | ||
function capitalizeFirstLetter(str) { | ||
return str.substr(0,1).toUpperCase() + str.substr(1); | ||
} | ||
function ScaleException(message, errcode) { | ||
@@ -266,2 +224,4 @@ this.message = message; | ||
} | ||
ScaleException.prototype = Object.create(Error.prototype); | ||
ScaleException.prototype.constructor = ScaleException; | ||
@@ -273,16 +233,8 @@ function ScaleInvalidRequest(message) { | ||
ScaleInvalidRequest.prototype = Object.create(ScaleException.prototype); | ||
ScaleInvalidRequest.prototype.constructor = ScaleInvalidRequest; | ||
function validatePayload(taskType, kwargs) { | ||
Object.keys(kwargs).forEach(property => { | ||
if (DEFAULT_FIELDS.indexOf(property) < 0 && | ||
ALLOWED_FIELDS[taskType].indexOf(property) < 0) { | ||
throw new ScaleInvalidRequest('Illegal parameter ' + property + | ||
' for task type ' + taskType); | ||
} | ||
}); | ||
} | ||
module.exports = {ScaleClient: ScaleClient, | ||
module.exports = { | ||
ScaleClient: ScaleClient, | ||
ScaleException: ScaleException, | ||
ScaleInvalidRequest: ScaleInvalidRequest}; | ||
ScaleInvalidRequest: ScaleInvalidRequest | ||
}; |
@@ -25,3 +25,3 @@ { | ||
"description": "Official Node SDK for Scale API", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"main": "lib/scaleapi.js", | ||
@@ -28,0 +28,0 @@ "directories": { |
@@ -12,4 +12,6 @@ /* eslint-env mocha */ | ||
var client = new scaleapi.ScaleClient(testApiKey); | ||
const TIMEOUT = 4000; | ||
describe('task creation', () => { | ||
describe('task creation', function() { | ||
this.timeout(TIMEOUT); | ||
it('categorize', done => { | ||
@@ -259,3 +261,4 @@ client.createCategorizationTask({ | ||
describe('task methods', () => { | ||
describe('task methods', function() { | ||
this.timeout(TIMEOUT); | ||
it('test cancel', done => { | ||
@@ -334,3 +337,3 @@ async.waterfall([ | ||
it('test tasks invalid', done => { | ||
it('test tasks invalid', (done) => { | ||
client.tasks({bogus: 0}, err => { | ||
@@ -337,0 +340,0 @@ expect(err).to.be.an.instanceof(scaleapi.ScaleException); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21169
560
1