deprecate request wrapper completely

This commit is contained in:
tiagosiebler
2022-05-11 18:13:16 +01:00
parent 61c278c318
commit a89cb5148e
4 changed files with 44 additions and 323 deletions

View File

@@ -5,7 +5,6 @@ import {
RestClientOptions,
REST_CLIENT_TYPE_ENUM,
} from './util/requestUtils';
import RequestWrapper from './util/requestWrapper';
import {
APIResponseWithTime,
AssetExchangeRecordsReq,
@@ -21,9 +20,6 @@ import {
import BaseRestClient from './util/BaseRestClient';
export class InverseClient extends BaseRestClient {
/** @deprecated, */
protected requestWrapper: RequestWrapper;
/**
* @public Creates an instance of the inverse REST API client.
*
@@ -48,13 +44,6 @@ export class InverseClient extends BaseRestClient {
requestOptions,
REST_CLIENT_TYPE_ENUM.inverse
);
this.requestWrapper = new RequestWrapper(
key,
secret,
getRestBaseUrl(useLivenet, restClientOptions),
{ ...restClientOptions, disable_time_sync: true },
requestOptions
);
return this;
}
@@ -211,7 +200,7 @@ export class InverseClient extends BaseRestClient {
close_on_trigger?: boolean;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/order/create', orderRequest);
return this.postPrivate('v2/private/order/create', orderRequest);
}
getActiveOrderList(params: {
@@ -229,11 +218,11 @@ export class InverseClient extends BaseRestClient {
order_id?: string;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/order/cancel', params);
return this.postPrivate('v2/private/order/cancel', params);
}
cancelAllActiveOrders(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.post('v2/private/order/cancelAll', params);
return this.postPrivate('v2/private/order/cancelAll', params);
}
replaceActiveOrder(params: {
@@ -247,7 +236,7 @@ export class InverseClient extends BaseRestClient {
tp_trigger_by?: string;
sl_trigger_by?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/order/replace', params);
return this.postPrivate('v2/private/order/replace', params);
}
queryActiveOrder(params: {
@@ -275,7 +264,7 @@ export class InverseClient extends BaseRestClient {
close_on_trigger?: boolean;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/stop-order/create', params);
return this.postPrivate('v2/private/stop-order/create', params);
}
/** get conditional order list. This may see delays, use queryConditionalOrder() for real-time queries */
@@ -294,11 +283,11 @@ export class InverseClient extends BaseRestClient {
stop_order_id?: string;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/stop-order/cancel', params);
return this.postPrivate('v2/private/stop-order/cancel', params);
}
cancelAllConditionalOrders(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.post('v2/private/stop-order/cancelAll', params);
return this.postPrivate('v2/private/stop-order/cancelAll', params);
}
replaceConditionalOrder(params: {
@@ -309,7 +298,7 @@ export class InverseClient extends BaseRestClient {
p_r_price?: string;
p_r_trigger_price?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/stop-order/replace', params);
return this.postPrivate('v2/private/stop-order/replace', params);
}
queryConditionalOrder(params: {
@@ -332,7 +321,7 @@ export class InverseClient extends BaseRestClient {
symbol: string;
margin: string;
}): GenericAPIResponse {
return this.requestWrapper.post('position/change-position-margin', params);
return this.postPrivate('position/change-position-margin', params);
}
setTradingStop(params: {
@@ -344,7 +333,7 @@ export class InverseClient extends BaseRestClient {
sl_trigger_by?: string;
new_trailing_active?: number;
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/position/trading-stop', params);
return this.postPrivate('v2/private/position/trading-stop', params);
}
setUserLeverage(params: {
@@ -352,10 +341,7 @@ export class InverseClient extends BaseRestClient {
leverage: number;
leverage_only?: boolean;
}): GenericAPIResponse {
return this.requestWrapper.post(
'v2/private/position/leverage/save',
params
);
return this.postPrivate('v2/private/position/leverage/save', params);
}
getTradeRecords(params: {
@@ -384,7 +370,7 @@ export class InverseClient extends BaseRestClient {
symbol: string;
tp_sl_mode: 'Full' | 'Partial';
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/tpsl/switch-mode', params);
return this.postPrivate('v2/private/tpsl/switch-mode', params);
}
setMarginType(params: {
@@ -393,10 +379,7 @@ export class InverseClient extends BaseRestClient {
buy_leverage: number;
sell_leverage: number;
}): GenericAPIResponse {
return this.requestWrapper.post(
'v2/private/position/switch-isolated',
params
);
return this.postPrivate('v2/private/position/switch-isolated', params);
}
/**
@@ -411,7 +394,7 @@ export class InverseClient extends BaseRestClient {
symbol: string;
risk_id: string;
}): GenericAPIResponse {
return this.requestWrapper.post('open-api/wallet/risk-limit', params);
return this.postPrivate('open-api/wallet/risk-limit', params);
}
/**

View File

@@ -5,7 +5,6 @@ import {
RestClientOptions,
REST_CLIENT_TYPE_ENUM,
} from './util/requestUtils';
import RequestWrapper from './util/requestWrapper';
import {
APIResponseWithTime,
AssetExchangeRecordsReq,
@@ -21,9 +20,6 @@ import {
import BaseRestClient from './util/BaseRestClient';
export class InverseFuturesClient extends BaseRestClient {
/** @deprecated, */
protected requestWrapper: RequestWrapper;
/**
* @public Creates an instance of the inverse futures REST API client.
*
@@ -48,13 +44,6 @@ export class InverseFuturesClient extends BaseRestClient {
requestOptions,
REST_CLIENT_TYPE_ENUM.inverseFutures
);
this.requestWrapper = new RequestWrapper(
key,
secret,
getRestBaseUrl(useLivenet, restClientOptions),
{ ...restClientOptions, disable_time_sync: true },
requestOptions
);
return this;
}
@@ -212,10 +201,7 @@ export class InverseFuturesClient extends BaseRestClient {
close_on_trigger?: boolean;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post(
'futures/private/order/create',
orderRequest
);
return this.postPrivate('futures/private/order/create', orderRequest);
}
getActiveOrderList(params: {
@@ -233,11 +219,11 @@ export class InverseFuturesClient extends BaseRestClient {
order_id?: string;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('futures/private/order/cancel', params);
return this.postPrivate('futures/private/order/cancel', params);
}
cancelAllActiveOrders(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.post('futures/private/order/cancelAll', params);
return this.postPrivate('futures/private/order/cancelAll', params);
}
replaceActiveOrder(params: {
@@ -247,7 +233,7 @@ export class InverseFuturesClient extends BaseRestClient {
p_r_qty?: string;
p_r_price?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('futures/private/order/replace', params);
return this.postPrivate('futures/private/order/replace', params);
}
queryActiveOrder(params: {
@@ -275,10 +261,7 @@ export class InverseFuturesClient extends BaseRestClient {
close_on_trigger?: boolean;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post(
'futures/private/stop-order/create',
params
);
return this.postPrivate('futures/private/stop-order/create', params);
}
getConditionalOrder(params: {
@@ -296,17 +279,11 @@ export class InverseFuturesClient extends BaseRestClient {
stop_order_id?: string;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post(
'futures/private/stop-order/cancel',
params
);
return this.postPrivate('futures/private/stop-order/cancel', params);
}
cancelAllConditionalOrders(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.post(
'futures/private/stop-order/cancelAll',
params
);
return this.postPrivate('futures/private/stop-order/cancelAll', params);
}
replaceConditionalOrder(params: {
@@ -317,10 +294,7 @@ export class InverseFuturesClient extends BaseRestClient {
p_r_price?: string;
p_r_trigger_price?: string;
}): GenericAPIResponse {
return this.requestWrapper.post(
'futures/private/stop-order/replace',
params
);
return this.postPrivate('futures/private/stop-order/replace', params);
}
queryConditionalOrder(params: {
@@ -346,7 +320,7 @@ export class InverseFuturesClient extends BaseRestClient {
symbol: string;
margin: string;
}): GenericAPIResponse {
return this.requestWrapper.post(
return this.postPrivate(
'futures/private/position/change-position-margin',
params
);
@@ -361,10 +335,7 @@ export class InverseFuturesClient extends BaseRestClient {
sl_trigger_by?: string;
new_trailing_active?: number;
}): GenericAPIResponse {
return this.requestWrapper.post(
'futures/private/position/trading-stop',
params
);
return this.postPrivate('futures/private/position/trading-stop', params);
}
setUserLeverage(params: {
@@ -372,10 +343,7 @@ export class InverseFuturesClient extends BaseRestClient {
buy_leverage: number;
sell_leverage: number;
}): GenericAPIResponse {
return this.requestWrapper.post(
'futures/private/position/leverage/save',
params
);
return this.postPrivate('futures/private/position/leverage/save', params);
}
/**
@@ -385,10 +353,7 @@ export class InverseFuturesClient extends BaseRestClient {
symbol: string;
mode: number;
}): GenericAPIResponse {
return this.requestWrapper.post(
'futures/private/position/switch-mode',
params
);
return this.postPrivate('futures/private/position/switch-mode', params);
}
/**
@@ -400,10 +365,7 @@ export class InverseFuturesClient extends BaseRestClient {
buy_leverage: number;
sell_leverage: number;
}): GenericAPIResponse {
return this.requestWrapper.post(
'futures/private/position/switch-isolated',
params
);
return this.postPrivate('futures/private/position/switch-isolated', params);
}
getTradeRecords(params: {
@@ -443,7 +405,7 @@ export class InverseFuturesClient extends BaseRestClient {
symbol: string;
risk_id: string;
}): GenericAPIResponse {
return this.requestWrapper.post('open-api/wallet/risk-limit', params);
return this.postPrivate('open-api/wallet/risk-limit', params);
}
/**

View File

@@ -5,7 +5,6 @@ import {
RestClientOptions,
REST_CLIENT_TYPE_ENUM,
} from './util/requestUtils';
import RequestWrapper from './util/requestWrapper';
import {
APIResponse,
APIResponseWithTime,
@@ -23,9 +22,6 @@ import { linearPositionModeEnum, positionTpSlModeEnum } from './constants/enum';
import BaseRestClient from './util/BaseRestClient';
export class LinearClient extends BaseRestClient {
/** @deprecated, */
protected requestWrapper: RequestWrapper;
/**
* @public Creates an instance of the linear (USD Perps) REST API client.
*
@@ -50,14 +46,6 @@ export class LinearClient extends BaseRestClient {
requestOptions,
REST_CLIENT_TYPE_ENUM.linear
);
this.requestWrapper = new RequestWrapper(
key,
secret,
getRestBaseUrl(useLivenet, restClientOptions),
{ ...restClientOptions, disable_time_sync: true },
requestOptions
);
return this;
}
@@ -215,7 +203,7 @@ export class LinearClient extends BaseRestClient {
order_link_id?: string;
position_idx?: number;
}): GenericAPIResponse {
return this.requestWrapper.post('private/linear/order/create', params);
return this.postPrivate('private/linear/order/create', params);
}
getActiveOrderList(params: {
@@ -235,11 +223,11 @@ export class LinearClient extends BaseRestClient {
order_id?: string;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('private/linear/order/cancel', params);
return this.postPrivate('private/linear/order/cancel', params);
}
cancelAllActiveOrders(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.post('private/linear/order/cancel-all', params);
return this.postPrivate('private/linear/order/cancel-all', params);
}
replaceActiveOrder(params: {
@@ -253,7 +241,7 @@ export class LinearClient extends BaseRestClient {
tp_trigger_by?: string;
sl_trigger_by?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('private/linear/order/replace', params);
return this.postPrivate('private/linear/order/replace', params);
}
queryActiveOrder(params: {
@@ -286,7 +274,7 @@ export class LinearClient extends BaseRestClient {
tp_trigger_by?: string;
sl_trigger_by?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('private/linear/stop-order/create', params);
return this.postPrivate('private/linear/stop-order/create', params);
}
getConditionalOrder(params: {
@@ -306,14 +294,11 @@ export class LinearClient extends BaseRestClient {
stop_order_id?: string;
order_link_id?: string;
}): GenericAPIResponse {
return this.requestWrapper.post('private/linear/stop-order/cancel', params);
return this.postPrivate('private/linear/stop-order/cancel', params);
}
cancelAllConditionalOrders(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.post(
'private/linear/stop-order/cancel-all',
params
);
return this.postPrivate('private/linear/stop-order/cancel-all', params);
}
replaceConditionalOrder(params: {
@@ -328,10 +313,7 @@ export class LinearClient extends BaseRestClient {
tp_trigger_by?: string;
sl_trigger_by?: string;
}): GenericAPIResponse {
return this.requestWrapper.post(
'private/linear/stop-order/replace',
params
);
return this.postPrivate('private/linear/stop-order/replace', params);
}
queryConditionalOrder(params: {
@@ -355,7 +337,7 @@ export class LinearClient extends BaseRestClient {
side: string;
auto_add_margin: boolean;
}): GenericAPIResponse {
return this.requestWrapper.post(
return this.postPrivate(
'private/linear/position/set-auto-add-margin',
params
);
@@ -367,10 +349,7 @@ export class LinearClient extends BaseRestClient {
buy_leverage: number;
sell_leverage: number;
}): GenericAPIResponse {
return this.requestWrapper.post(
'private/linear/position/switch-isolated',
params
);
return this.postPrivate('private/linear/position/switch-isolated', params);
}
/**
@@ -380,10 +359,7 @@ export class LinearClient extends BaseRestClient {
symbol: string;
mode: typeof linearPositionModeEnum[keyof typeof linearPositionModeEnum];
}): GenericAPIResponse {
return this.requestWrapper.post(
'private/linear/position/switch-mode',
params
);
return this.postPrivate('private/linear/position/switch-mode', params);
}
/**
@@ -394,7 +370,7 @@ export class LinearClient extends BaseRestClient {
symbol: string;
tp_sl_mode: typeof positionTpSlModeEnum[keyof typeof positionTpSlModeEnum];
}): GenericAPIResponse {
return this.requestWrapper.post('private/linear/tpsl/switch-mode', params);
return this.postPrivate('private/linear/tpsl/switch-mode', params);
}
setAddReduceMargin(params?: {
@@ -402,10 +378,7 @@ export class LinearClient extends BaseRestClient {
side: string;
margin: number;
}): GenericAPIResponse {
return this.requestWrapper.post(
'private/linear/position/add-margin',
params
);
return this.postPrivate('private/linear/position/add-margin', params);
}
setUserLeverage(params: {
@@ -413,10 +386,7 @@ export class LinearClient extends BaseRestClient {
buy_leverage: number;
sell_leverage: number;
}): GenericAPIResponse {
return this.requestWrapper.post(
'private/linear/position/set-leverage',
params
);
return this.postPrivate('private/linear/position/set-leverage', params);
}
setTradingStop(params: {
@@ -430,10 +400,7 @@ export class LinearClient extends BaseRestClient {
sl_size?: number;
tp_size?: number;
}): GenericAPIResponse {
return this.requestWrapper.post(
'private/linear/position/trading-stop',
params
);
return this.postPrivate('private/linear/position/trading-stop', params);
}
getTradeRecords(params: {
@@ -471,7 +438,7 @@ export class LinearClient extends BaseRestClient {
side: string;
risk_id: number;
}): GenericAPIResponse {
return this.requestWrapper.post('private/linear/position/set-risk', params);
return this.postPrivate('private/linear/position/set-risk', params);
}
/**

View File

@@ -1,191 +0,0 @@
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
import { signMessage } from './node-support';
import { serializeParams, RestClientOptions, GenericAPIResponse, isPublicEndpoint } from './requestUtils';
export default class RequestUtil {
private timeOffset: number | null;
private syncTimePromise: null | Promise<any>;
private options: RestClientOptions;
private baseUrl: string;
private globalRequestOptions: AxiosRequestConfig;
private key: string | undefined;
private secret: string | undefined;
constructor(
key: string | undefined,
secret: string | undefined,
baseUrl: string,
options: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {}
) {
this.timeOffset = null;
this.syncTimePromise = null;
this.options = {
recv_window: 5000,
// how often to sync time drift with bybit servers
sync_interval_ms: 3600000,
// if true, we'll throw errors if any params are undefined
strict_param_validation: false,
...options
};
this.globalRequestOptions = {
// in ms == 5 minutes by default
timeout: 1000 * 60 * 5,
// custom request options based on axios specs - see: https://github.com/axios/axios#request-config
...requestOptions,
headers: {
'x-referer': 'bybitapinode'
},
};
this.baseUrl = baseUrl;
if (key && !secret) {
throw new Error('API Key & Secret are both required for private enpoints')
}
if (this.options.disable_time_sync !== true) {
this.syncTime();
setInterval(this.syncTime.bind(this), +this.options.sync_interval_ms!);
}
this.key = key;
this.secret = secret;
}
get<T>(endpoint: string, params?: any): Promise<T> {
return this._call('GET', endpoint, params);
}
post<T>(endpoint: string, params?: any): Promise<T> {
return this._call('POST', endpoint, params);
}
/**
* @private Make a HTTP request to a specific endpoint. Private endpoints are automatically signed.
*/
async _call<T>(method: Method, endpoint: string, params?: any): Promise<T> {
if (!isPublicEndpoint(endpoint)) {
if (!this.key || !this.secret) {
throw new Error('Private endpoints require api and private keys set');
}
if (this.timeOffset === null) {
await this.syncTime();
}
params = await this.signRequest(params);
}
const options = {
...this.globalRequestOptions,
url: [this.baseUrl, endpoint].join('/'),
method: method,
json: true
};
if (method === 'GET') {
options.params = params;
} else {
options.data = params;
}
return axios(options).then(response => {
if (response.status == 200) {
return response.data;
}
throw response;
}).catch(e => this.parseException(e));
}
/**
* @private generic handler to parse request exceptions
*/
parseException(e: any): unknown {
if (this.options.parse_exceptions === false) {
throw e;
}
// Something happened in setting up the request that triggered an Error
if (!e.response) {
if (!e.request) {
throw e.message;
}
// request made but no response received
throw e;
}
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const response: AxiosResponse = e.response;
throw {
code: response.status,
message: response.statusText,
body: response.data,
headers: response.headers,
requestOptions: this.options
};
}
/**
* @private sign request and set recv window
*/
async signRequest(data: any): Promise<any> {
const params = {
...data,
api_key: this.key,
timestamp: Date.now() + (this.timeOffset || 0)
};
// 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) {
const serializedParams = serializeParams(params, this.options.strict_param_validation);
params.sign = await signMessage(serializedParams, this.secret);
}
return params;
}
/**
* @private trigger time sync and store promise
*/
syncTime(): GenericAPIResponse {
if (this.options.disable_time_sync === true) {
return Promise.resolve(false);
}
if (this.syncTimePromise !== null) {
return this.syncTimePromise;
}
this.syncTimePromise = this.getTimeOffset().then(offset => {
this.timeOffset = offset;
this.syncTimePromise = null;
});
return this.syncTimePromise;
}
/**
* @deprecated move this somewhere else, because v2/public/time shouldn't be hardcoded here
*
* @returns {Promise<number>}
* @memberof RequestUtil
*/
async getTimeOffset(): Promise<number> {
const start = Date.now();
const result = await this.get<any>('v2/public/time');
const end = Date.now();
return Math.ceil((result.time_now * 1000) - end + ((end - start) / 2));
}
};