Skip to content

Commit 8c4e78a

Browse files
committed
Added Workflows, moved methods out of Workflow and made them deprecated.
1 parent 1958b68 commit 8c4e78a

File tree

4 files changed

+167
-17
lines changed

4 files changed

+167
-17
lines changed

spec/workflow-spec.js renamed to spec/workflows-spec.js

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Clarifai = require('./../src');
22
const {errorHandler} = require('./helpers');
33
const {sampleImages} = require('./test-data');
44
const generalModelVersionId = 'aa9ca48295b37401f8af92ad1af0d91d';
5+
const Workflows = require('./../src/Workflows');
56

67
let app;
78
let testWorkflowId;
@@ -14,9 +15,30 @@ describe('Workflow', () => {
1415
});
1516
});
1617

18+
it('Gets all workflows', done => {
19+
app.workflows.list({
20+
page: 1,
21+
perPage: 5
22+
})
23+
.then(workflows => {
24+
expect(workflows).toBeDefined();
25+
expect(workflows instanceof Workflows).toBe(true);
26+
27+
for (let i = 0; i < workflows.length; i++) {
28+
let workflow = workflows[i];
29+
expect(workflow.id).toBeDefined();
30+
expect(workflow.appId).toBeDefined();
31+
expect(workflow.createdAt).toBeDefined();
32+
}
33+
34+
done();
35+
})
36+
.catch(errorHandler.bind(done));
37+
});
38+
1739
it('Call given workflow id with one input', done => {
1840
testWorkflowId = 'big-bang' + Date.now();
19-
app.workflow.create(testWorkflowId, {
41+
app.workflows.create(testWorkflowId, {
2042
modelId: Clarifai.GENERAL_MODEL,
2143
modelVersionId: generalModelVersionId
2244
})
@@ -36,22 +58,32 @@ describe('Workflow', () => {
3658
expect(output.created_at).toBeDefined();
3759
expect(output.model).toBeDefined();
3860
expect(output.model.model_version).toBeDefined();
61+
})
62+
.then(() => {
63+
app.workflows.delete(testWorkflowId);
3964
done();
4065
})
4166
.catch(errorHandler.bind(done));
4267
});
4368

4469
it('Call given workflow id with multiple inputs with specified types', done => {
45-
app.workflow.predict(testWorkflowId, [
46-
{
47-
url: sampleImages[0],
48-
allowDuplicateUrl: true
49-
},
50-
{
51-
url: sampleImages[1],
52-
allowDuplicateUrl: true
53-
}
54-
])
70+
testWorkflowId = 'big-bang' + Date.now();
71+
app.workflows.create(testWorkflowId, {
72+
modelId: Clarifai.GENERAL_MODEL,
73+
modelVersionId: generalModelVersionId
74+
})
75+
.then(() => {
76+
return app.workflow.predict(testWorkflowId, [
77+
{
78+
url: sampleImages[0],
79+
allowDuplicateUrl: true
80+
},
81+
{
82+
url: sampleImages[1],
83+
allowDuplicateUrl: true
84+
}
85+
]);
86+
})
5587
.then(response => {
5688
expect(response.workflow).toBeDefined();
5789
const results = response.results;
@@ -66,16 +98,25 @@ describe('Workflow', () => {
6698
expect(output.created_at).toBeDefined();
6799
expect(output.model).toBeDefined();
68100
expect(output.model.model_version).toBeDefined();
101+
})
102+
.then(() => {
103+
app.workflows.delete(testWorkflowId);
69104
done();
70105
})
71106
.catch(errorHandler.bind(done));
72107
});
73108

74109
it('Call given workflow id with multiple inputs without specified types', done => {
75-
app.workflow.predict(testWorkflowId, [
76-
sampleImages[2],
77-
sampleImages[3]
78-
])
110+
testWorkflowId = 'big-bang' + Date.now();
111+
app.workflows.create(testWorkflowId, {
112+
modelId: Clarifai.GENERAL_MODEL,
113+
modelVersionId: generalModelVersionId
114+
})
115+
.then(() => {
116+
return app.workflow.predict(testWorkflowId, [
117+
sampleImages[2], sampleImages[3]
118+
]);
119+
})
79120
.then(response => {
80121
expect(response.workflow).toBeDefined();
81122
const results = response.results;
@@ -92,7 +133,8 @@ describe('Workflow', () => {
92133
expect(output.model.model_version).toBeDefined();
93134
return app.workflow.delete(testWorkflowId);
94135
})
95-
.then(response => {
136+
.then(() => {
137+
app.workflows.delete(testWorkflowId);
96138
done();
97139
})
98140
.catch(errorHandler.bind(done));

src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let Models = require('./Models');
44
let Inputs = require('./Inputs');
55
let Concepts = require('./Concepts');
66
let Workflow = require('./Workflow');
7+
let Workflows = require('./Workflows');
78
let {API, ERRORS, getBasePath} = require('./constants');
89
let {TOKEN_PATH} = API;
910

@@ -104,6 +105,7 @@ class App {
104105
this.inputs = new Inputs(this._config);
105106
this.concepts = new Concepts(this._config);
106107
this.workflow = new Workflow(this._config);
108+
this.workflows = new Workflows(this._config);
107109
}
108110

109111
_getToken(resolve, reject) {

src/Workflow.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ class Workflow {
1212
constructor(_config, rawData=[]) {
1313
this._config = _config;
1414
this.rawData = rawData;
15+
this.id = rawData.id;
16+
this.createdAt = rawData.created_at || rawData.createdAt;
17+
this.appId = rawData.app_id || rawData.appId;
1518
}
1619

20+
/**
21+
* @deprecated
22+
*/
1723
create(workflowId, config) {
1824
const url = `${this._config.basePath}${WORKFLOWS_PATH}`;
1925
const modelId = config.modelId;
@@ -45,6 +51,9 @@ class Workflow {
4551
});
4652
}
4753

54+
/**
55+
* @deprecated
56+
*/
4857
delete(workflowId, config) {
4958
const url = `${this._config.basePath}${replaceVars(WORKFLOW_PATH, [workflowId])}`;
5059
return wrapToken(this._config, (headers) => {
@@ -87,4 +96,4 @@ class Workflow {
8796
}
8897
}
8998

90-
module.exports = Workflow;
99+
module.exports = Workflow;

src/Workflows.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
let axios = require('axios');
2+
let Workflow = require('./Workflow');
3+
let {API, replaceVars} = require('./constants');
4+
let {WORKFLOWS_PATH, WORKFLOW_PATH,} = API;
5+
let {wrapToken,} = require('./utils');
6+
let {isSuccess,} = require('./helpers');
7+
8+
/**
9+
* class representing a collection of workflows
10+
* @class
11+
*/
12+
class Workflows {
13+
constructor(_config, rawData = []) {
14+
this._config = _config;
15+
this.rawData = rawData;
16+
rawData.forEach((workflowData, index) => {
17+
this[index] = new Workflow(this._config, workflowData);
18+
});
19+
this.length = rawData.length;
20+
}
21+
22+
/**
23+
* Get all workflows in app
24+
* @param {Object} options Object with keys explained below: (optional)
25+
* @param {Number} options.page The page number (optional, default: 1)
26+
* @param {Number} options.perPage Number of images to return per page (optional, default: 20)
27+
* @return {Promise(Workflows, error)} A Promise that is fulfilled with an instance of Workflows or rejected with an error
28+
*/
29+
list(options = {page: 1, perPage: 20}) {
30+
let url = `${this._config.basePath}${WORKFLOWS_PATH}`;
31+
return wrapToken(this._config, (headers) => {
32+
return new Promise((resolve, reject) => {
33+
axios.get(url, {
34+
headers,
35+
params: {
36+
page: options.page,
37+
per_page: options.perPage,
38+
}
39+
}).then((response) => {
40+
if (isSuccess(response)) {
41+
resolve(new Workflows(this._config, response.data.workflows));
42+
} else {
43+
reject(response);
44+
}
45+
}, reject);
46+
});
47+
});
48+
}
49+
50+
create(workflowId, config) {
51+
const url = `${this._config.basePath}${WORKFLOWS_PATH}`;
52+
const modelId = config.modelId;
53+
const modelVersionId = config.modelVersionId;
54+
const body = {
55+
workflows: [{
56+
id: workflowId,
57+
nodes: [{
58+
id: 'concepts',
59+
model: {
60+
id: modelId,
61+
model_version: {
62+
id: modelVersionId
63+
}
64+
}
65+
}]
66+
}]
67+
};
68+
69+
return wrapToken(this._config, (headers) => {
70+
return new Promise((resolve, reject) => {
71+
axios.post(url, body, {
72+
headers
73+
}).then(response => {
74+
const workflowId = response.data.workflows[0].id;
75+
resolve(workflowId);
76+
}, reject);
77+
});
78+
});
79+
}
80+
81+
delete(workflowId) {
82+
const url = `${this._config.basePath}${replaceVars(WORKFLOW_PATH, [workflowId])}`;
83+
return wrapToken(this._config, (headers) => {
84+
return new Promise((resolve, reject) => {
85+
axios.delete(url, {
86+
headers
87+
}).then(response => {
88+
const data = response.data;
89+
resolve(data);
90+
}, reject);
91+
});
92+
});
93+
}
94+
}
95+
;
96+
97+
module.exports = Workflows;

0 commit comments

Comments
 (0)