2015-07-02 21:49:36 +00:00
|
|
|
import restler from 'restler';
|
2015-07-02 00:40:13 +00:00
|
|
|
|
|
|
|
export default function fetch(path, data = {}) {
|
2015-06-26 17:20:54 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2015-07-02 21:49:36 +00:00
|
|
|
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-07-02 00:40:13 +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);
|
|
|
|
});
|
|
|
|
}
|