node-telegram-api/lib/fetch.js

36 lines
768 B
JavaScript
Raw Normal View History

import restler from 'restler';
export default function fetch(path, data = {}) {
2015-06-26 17:20:54 +00:00
return new Promise((resolve, reject) => {
const method = Object.keys(data).length ? 'POST' : 'GET';
const multipart = method === 'POST' ? true : false;
restler.request('https://api.telegram.org/bot' + path, {
data, method, multipart
}).on('complete', response => {
try {
let json = JSON.parse(response);
resolve(json);
} catch(e) {
reject(e);
2015-06-26 17:20:54 +00:00
}
});
2015-06-26 17:20:54 +00:00
});
}
2015-06-29 22:20:34 +00:00
export function getBody(stream) {
let data = '';
return new Promise((resolve, reject) => {
stream.on('data', chunk => {
data += chunk;
});
stream.on('end', () => {
resolve(data);
});
stream.on('error', reject);
});
}