MCPcopy Create free account
hub / github.com/ccxt/node-binance-api / promiseRequest

Function promiseRequest

deprecated/node-binance-api.js:555–618  ·  view source on GitHub ↗
(url, data = {}, flags = {})

Source from the content-addressed store, hash-verified

553 return promiseRequest('v1/order', params, { base: dapi, type: 'TRADE', method: 'POST' });
554 };
555 const promiseRequest = async (url, data = {}, flags = {}) => {
556 return new Promise((resolve, reject) => {
557 let query = '', headers = {
558 'User-Agent': userAgent,
559 'Content-type': 'application/x-www-form-urlencoded'
560 };
561 if (typeof flags.method === 'undefined') flags.method = 'GET'; // GET POST PUT DELETE
562 if (typeof flags.type === 'undefined') flags.type = false; // TRADE, SIGNED, MARKET_DATA, USER_DATA, USER_STREAM
563 else {
564 if (typeof data.recvWindow === 'undefined') data.recvWindow = Binance.options.recvWindow;
565 requireApiKey('promiseRequest');
566 headers['X-MBX-APIKEY'] = Binance.options.APIKEY;
567 }
568 let baseURL = typeof flags.base === 'undefined' ? base : flags.base;
569 if (Binance.options.test && baseURL === base) baseURL = baseTest;
570 if (Binance.options.test && baseURL === fapi) baseURL = fapiTest;
571 if (Binance.options.test && baseURL === dapi) baseURL = dapiTest;
572 let opt = {
573 headers,
574 url: baseURL + url,
575 method: flags.method,
576 timeout: Binance.options.recvWindow,
577 followAllRedirects: true
578 };
579 if (flags.type === 'SIGNED' || flags.type === 'TRADE' || flags.type === 'USER_DATA') {
580 if (!requireApiSecret('promiseRequest')) return reject('promiseRequest: Invalid API Secret!');
581 data.timestamp = new Date().getTime() + Binance.info.timeOffset;
582 query = makeQueryString(data);
583 data.signature = crypto.createHmac('sha256', Binance.options.APISECRET).update(query).digest('hex'); // HMAC hash header
584 opt.url = `${baseURL}${url}?${query}&signature=${data.signature}`;
585 }
586 opt.qs = data;
587 /*if ( flags.method === 'POST' ) {
588 opt.form = data;
589 } else {
590 opt.qs = data;
591 }*/
592 try {
593 request(addProxy(opt), (error, response, body) => {
594 if (error) return reject(error);
595 try {
596 Binance.info.lastRequest = new Date().getTime();
597 if (response) {
598 Binance.info.statusCode = response.statusCode || 0;
599 if (response.request) Binance.info.lastURL = response.request.uri.href;
600 if (response.headers) {
601 Binance.info.usedWeight = response.headers['x-mbx-used-weight-1m'] || 0;
602 Binance.info.futuresLatency = response.headers['x-response-time'] || 0;
603 }
604 }
605 if (!error && response.statusCode == 200) return resolve(JSONbig.parse(body));
606 if (typeof response.body !== 'undefined') {
607 return resolve(JSONbig.parse(response.body));
608 }
609 return reject(response);
610 } catch (err) {
611 return reject(`promiseRequest error #${response.statusCode}`);
612 }

Callers 5

futuresOrderFunction · 0.85
deliveryOrderFunction · 0.85
apiFunction · 0.85
getFuturesKlineSnapshotFunction · 0.85
getDeliveryKlineSnapshotFunction · 0.85

Calls 4

requireApiKeyFunction · 0.85
requireApiSecretFunction · 0.85
makeQueryStringFunction · 0.85
addProxyFunction · 0.85

Tested by

no test coverage detected