UNPKG

1.53 kBJavaScriptView Raw
1const https = require('https');
2
3module.exports = {
4 // custom made post function
5 post: (apiPath, apiKey, sendObj, logStuff) => new Promise((resolve, reject) => {
6 const postData = JSON.stringify(sendObj);
7 const options = {
8 port: 443,
9 method: 'POST',
10 headers: {
11 'Content-Type': 'application/json',
12 'Content-Length': postData.length,
13 'Authorization': apiKey
14 }
15 };
16 const req = https.request(apiPath, options, res => {
17 if (logStuff) {
18 console.log(`BLAPI: posted to ${apiPath}`);
19 console.log('BLAPI: statusCode:', res.statusCode);
20 console.log('BLAPI: headers:', res.headers);
21 res.on('data', d => {
22 console.log(`BLAPI: data: ${d}`);
23 });
24 }
25 });
26 req.on('error', e => {
27 console.error(`BLAPI: ${e}`);
28 reject(new Error(`Request to ${req.url} failed with Errorcode ${req.status}:\n${req.statusText}`));
29 });
30 req.write(postData);
31 req.end();
32 resolve();
33 }),
34 // custom made get function
35 get: url => new Promise((resolve, reject) => {
36 https.get(url, resp => {
37 let data = '';
38 resp.on('data', chunk => {
39 data += chunk;
40 });
41 resp.on('end', () => {
42 resolve(JSON.parse(data));
43 });
44 resp.on('error', e => {
45 console.error(`BLAPI: ${e}`);
46 reject(new Error(`Request to ${resp.url} failed with Errorcode ${resp.status}:\n${resp.statusText}`));
47 });
48 });
49 })
50};