Skip to content

Commit

Permalink
Allowing to overload actor task input and options
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrunkat committed Nov 16, 2018
1 parent 18e2828 commit dc1d545
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
xxxxxxxxxxxxxxxxxx
==================
- `tasks.runTask()` method now allows to overload input and options from actor task configuration.

0.4.0 / 2018/11/06
==================
- All key-value store records with content type `text/*` are now parsed into string.
Expand Down
21 changes: 20 additions & 1 deletion src/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,29 +273,48 @@ export default {
* @param [options.token]
* @param {Number} [options.waitForFinish] - Number of seconds to wait for task to finish. Maximum value is 120s.
If task doesn't finish in time then task run in RUNNING state is returned.
* @param {String} [options.body] - Actor input stringified as JSON, passed as HTTP POST payload
* @param {String} [options.contentType] - Content type of act input e.g 'application/json'
* @param {Number} [options.timeout] - Timeout for the act run in seconds. Zero value means there is no timeout.
* @param {Number} [options.memory] - Amount of memory allocated for the act run, in megabytes.
* @param {String} [options.build] - Tag or number of the build to run (e.g. <code>latest</code> or <code>1.2.34</code>).
* @param callback
* @returns {ActRun}
*/
runTask: (requestPromise, options) => {
const { baseUrl, token, taskId, waitForFinish } = options;
const { baseUrl, token, taskId, waitForFinish, body, contentType, timeout, memory, build } = options;

checkParamOrThrow(baseUrl, 'baseUrl', 'String');
checkParamOrThrow(token, 'token', 'String');
checkParamOrThrow(taskId, 'taskId', 'String');
checkParamOrThrow(waitForFinish, 'waitForFinish', 'Maybe Number');
checkParamOrThrow(contentType, 'contentType', 'Maybe String');
checkParamOrThrow(timeout, 'timeout', 'Maybe Number');
checkParamOrThrow(memory, 'memory', 'Maybe Number');
checkParamOrThrow(build, 'build', 'Maybe String');

const safeTaskId = replaceSlashWithTilde(taskId);
const query = {};

if (waitForFinish) query.waitForFinish = waitForFinish;
if (token) query.token = token;
if (timeout) query.timeout = timeout;
if (memory) query.memory = memory;
if (build) query.build = build;

const opts = {
url: `${baseUrl}${BASE_PATH}/${safeTaskId}/runs`,
method: 'POST',
qs: query,
};

if (contentType) opts.headers = { 'Content-Type': contentType };

if (body) {
checkParamOrThrow(body, 'body', 'String');
opts.body = body;
}

return requestPromise(opts)
.then(response => JSON.parse(response))
.then(pluckData)
Expand Down
27 changes: 27 additions & 0 deletions test/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,33 @@ describe('Tasks method', () => {
.then(response => expect(response).to.be.eql(run));
});

it('runTask() works with input and options overrides', () => {
const taskId = 'some-id';
const token = 'some-token';
const run = { foo: 'bar' };
const apiResponse = JSON.stringify({ data: run });
const memory = 512;
const contentType = 'application/json; charset=utf-8';
const body = '{ "foo": "bar" }';

const waitForFinish = 120;

requestExpectCall({
method: 'POST',
url: `${BASE_URL}${BASE_PATH}/${taskId}/runs`,
headers: { 'Content-Type': contentType },
qs: { token, waitForFinish, memory },
body,
}, apiResponse);

const apifyClient = new ApifyClient(OPTIONS);

return apifyClient
.tasks
.runTask({ taskId, token, waitForFinish, memory, body, contentType })
.then(response => expect(response).to.be.eql(run));
});

/*
NOTE: not allowed currently.
Expand Down

0 comments on commit dc1d545

Please sign in to comment.