Add support for recv_window parameter and custom options for REST client (#7)
* remove unused/unexisting method. Add missing semi-colon. Add support for recvWindow parameter, optional, with default value. Add support for custom sync time interval. * add new params to rest client constructor * add recv and sync as options object, optional, with updated docs * cleaning * whitespace * allow override via params * param consistency Co-authored-by: tiagosiebler <ts@github.com>
This commit is contained in:
@@ -4,9 +4,13 @@
|
|||||||
## Class: RestClient
|
## Class: RestClient
|
||||||
|
|
||||||
|
|
||||||
### new RestClient([key][, secret])
|
### new RestClient([key][, secret][, livenet][, options])
|
||||||
- `key` {String} Bybit API Key
|
- `key` {String} Bybit API Key
|
||||||
- `secret` {String} Bybit private key
|
- `secret` {String} Bybit private key
|
||||||
|
- `livenet` {Boolean} If false (default), use testnet.
|
||||||
|
- `options` {Object} Optional settings for custom behaviour.
|
||||||
|
- `recv_window` {Number} Optional, default 5000. Increase if recv errors are seen.
|
||||||
|
- `sync_interval_ms` {Number} Optional, default 3600000. Interval at which syncTime is performed.
|
||||||
|
|
||||||
If you only use the [public endpoints](#public-endpoints) you can ommit key and secret.
|
If you only use the [public endpoints](#public-endpoints) you can ommit key and secret.
|
||||||
|
|
||||||
|
|||||||
@@ -3,24 +3,30 @@ const assert = require('assert');
|
|||||||
|
|
||||||
const request = require('request');
|
const request = require('request');
|
||||||
|
|
||||||
const {signMessage, getServerTimeOffset} = require('./utility.js');
|
const {signMessage} = require('./utility.js');
|
||||||
|
|
||||||
const baseUrls = {
|
const baseUrls = {
|
||||||
livenet: 'https://api.bybit.com',
|
livenet: 'https://api.bybit.com',
|
||||||
testnet: 'https://api-testnet.bybit.com'
|
testnet: 'https://api-testnet.bybit.com'
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = class Request {
|
module.exports = class Request {
|
||||||
|
|
||||||
constructor(key, secret, livenet=false) {
|
constructor(key, secret, livenet=false, options={}) {
|
||||||
this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet'];
|
this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet'];
|
||||||
this._timeOffset = null;
|
this._timeOffset = null;
|
||||||
this._syncTimePromise = null;
|
this._syncTimePromise = null;
|
||||||
|
|
||||||
|
this.options = {
|
||||||
|
recv_window: 5000,
|
||||||
|
sync_interval_ms: 3600000,
|
||||||
|
...options
|
||||||
|
}
|
||||||
|
|
||||||
if(key) assert(secret, 'Secret is required for private enpoints');
|
if(key) assert(secret, 'Secret is required for private enpoints');
|
||||||
|
|
||||||
this._syncTime();
|
this._syncTime();
|
||||||
setInterval(this._syncTime.bind(this), 3600000);
|
setInterval(this._syncTime.bind(this), parseInt(this.options.sync_interval_ms));
|
||||||
|
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.secret = secret;
|
this.secret = secret;
|
||||||
@@ -91,6 +97,11 @@ module.exports = class Request {
|
|||||||
timestamp: Date.now() + this._timeOffset
|
timestamp: Date.now() + this._timeOffset
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
|
||||||
|
if(this.options.recv_window && !params.recv_window) {
|
||||||
|
params.recv_window = this.options.recv_window;
|
||||||
|
}
|
||||||
|
|
||||||
if(this.key && this.secret) {
|
if(this.key && this.secret) {
|
||||||
params.sign = signMessage(this._serializeParams(params), this.secret);
|
params.sign = signMessage(this._serializeParams(params), this.secret);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const Request = require('./request.js');
|
|||||||
|
|
||||||
module.exports = class RestClient {
|
module.exports = class RestClient {
|
||||||
|
|
||||||
constructor(key, secret, livenet=false) {
|
constructor(key, secret, livenet=false, options={}) {
|
||||||
this.request = new Request(...arguments);
|
this.request = new Request(...arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user