-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathblitline.js
More file actions
executable file
·75 lines (59 loc) · 1.77 KB
/
blitline.js
File metadata and controls
executable file
·75 lines (59 loc) · 1.77 KB
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
65
66
67
68
69
70
71
72
73
74
75
// jscs:disable requireTrailingComma,requirePaddingNewLinesBeforeLineComments
// Library version.
exports.version = '2.1.1';
// core modules
var https = require('https');
var jobs = [];
// exports
module.exports = {
addJob: function(jobHash) {
if (typeof jobHash !== 'object') {
return console.error('The job submitted must be an object!');
}
jobs.push(jobHash);
return jobs;
},
postJobs: function() {
return new Promise(function(resolve, reject) {
var result = null;
// stringify the jobs Array to prep sending it to Blitline API
var body = JSON.stringify({json: jobs});
// after creating the body, clear the jobs Array
jobs = [];
// create the options Object for the https.Agent
options = {
host: 'api.blitline.com',
port: 443,
path: '/job',
method: 'POST',
headers: {
'Content-Length': body.length,
'Content-Type': 'application/json'
},
rejectUnauthorized: true
// jscs:enable
};
// create a https Agent to keep the connection alive
options.agent = new https.Agent(options);
request = https.request(options, function(res) {
var result = [];
res
.on('data', function(chunk) {
return result.push(chunk.toString());
})
.on('end', function() {
// when the response ends, send the response back to the callback
return resolve(JSON.parse(result.join('')));
});
res.resume();
return res;
});
request.on('error', function(error) {
console.error('ERROR:Blitline:ErrorHandler: ', error);
return reject(error);
});
request.write(body);
return request.end();
});
}
};