/
get-job-results.ts
64 lines (52 loc) · 1.61 KB
/
get-job-results.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import * as fs from 'node:fs';
import ApiCommand from '../../api-command';
import _fetch from 'node-fetch';
export default class BulkTagGetJobResults extends ApiCommand {
static description = `Get bulk tag job results`;
static flags = {
...ApiCommand.flags,
};
static args = [
{
name: 'job_id',
description: 'bulk tag job to get results of',
required: true,
},
{
name: 'filename',
description: 'file to write jobs results to',
required: true,
},
];
static examples = [
'$ lf-cli bulk-tag-get-job-results:get [job_id] [filename]',
];
async run() {
const opts = await this.parse(BulkTagGetJobResults);
if (opts.args.ID === 'help') {
await BulkTagGetJobResults.run(['-h']);
this.exit(0);
}
const job_id = opts.args.job_id;
const reqOpts = {
method: 'GET',
};
const res = await this.fetch(
`/v20200626/bulk_tagging_job/${job_id}`,
reqOpts,
`Fetching job results`
);
const { results_url } = res;
const filename = opts.args.filename;
await this.download_file(results_url, filename);
}
async download_file(url: string, path: string) {
const res = await _fetch(url);
const file_stream = fs.createWriteStream(path);
await new Promise((resolve, reject) => {
res.body.pipe(file_stream);
res.body.on('error', reject);
file_stream.on('finish', resolve);
});
}
}