-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_file_to_task.js
58 lines (48 loc) · 2.43 KB
/
upload_file_to_task.js
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
#!/usr/bin/env node
require('dotenv').config();
const FormData = require('form-data');
const axios = require('axios');
const fs = require('fs');
const APP_KEY = process.env.APP_KEY;
const USER_TOKEN = process.env.USER_TOKEN;
const createTask = async (title, description, board_id, project_id) => {
const url = "https://runrun.it/api/v1.0/tasks";
const payload = { task: { title, description, board_id, project_id } };
const headers = { 'app-key': APP_KEY, 'user-token': USER_TOKEN, 'content-type': "application/json;charset=UTF-8" };
try {
const response = await axios.post(url, payload, { headers });
console.log(response.data);
return response.data;
} catch (error) { console.error(error); }
};
const createDocument = async (task_id, filename) => {
const url = `https://runrun.it/api/v1.0/documents?task_id=${task_id}`;
const size = fs.statSync(`./${filename}`).size;
const payload = { document: { data_file_name: filename, data_file_size: size, warning_duplicate: false } };
const headers = { 'app-key': APP_KEY, 'user-token': USER_TOKEN, 'content-type': "application/json", 'cache-control': "no-cache" };
try {
let response = await axios.post(url, payload, { headers, params: { task_id } });
const { id: document_id, fields } = response.data;
const form = new FormData();
form.append('name', filename);
form.append('key', fields.key);
form.append('acl', 'private');
form.append('policy', fields.policy);
form.append('signature', fields.signature);
form.append('AWSAccessKeyId', fields.AWSAccessKeyId);
form.append('content_type', fields.content_type);
form.append('filename', filename);
form.append('success_action_status', 201);
form.append('file', fs.createReadStream(`./${filename}`));
const formHeaders = form.getHeaders(); // Include multipart/form-data
await axios.post('https://s3.amazonaws.com/runrunit', form, { headers: {...formHeaders, 'app-key': APP_KEY, 'user-token': USER_TOKEN} });
await axios.post(`https://runrun.it/api/v1.0/documents/${document_id}/mark_as_uploaded`, {}, { headers });
} catch (error) { console.error(error); }
};
const title = "First Task";
const description = "My description";
const board_id = 283180;
const project_id = null;
createTask(title, description, board_id, project_id).then(task => {
createDocument(task.id, "example.csv");
});