Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Commit

Permalink
Simplify OrderbookSync constructor signature (#177)
Browse files Browse the repository at this point in the history
* Add checkAuth() utility function to check if auth object is valid

* Simplify OrderbookSync constructor to take auth object

...instead of full instance of AuthenticatedClient

* Add OrderbookSync test w/ AuthenticationClient for backwards compat
  • Loading branch information
rmm5t authored and fb55 committed Dec 25, 2017
1 parent 17619ca commit 9802f7f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
7 changes: 1 addition & 6 deletions lib/clients/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ class WebsocketClient extends EventEmitter {
super();
this.productIDs = Utils.determineProductIDs(productIDs);
this.websocketURI = websocketURI;
if (auth && !(auth.secret && auth.key && auth.passphrase)) {
throw new Error(
'Invalid or incomplete authentication credentials. You should either provide all of the secret, key and passphrase fields, or leave auth null'
);
}
this.channels = channels;
this.auth = auth || {};
this.auth = Utils.checkAuth(auth);
this.heartbeat = heartbeat;
this.connect();
}
Expand Down
21 changes: 16 additions & 5 deletions lib/orderbook_sync.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
const WebsocketClient = require('./clients/websocket.js');
const AuthenticatedClient = require('./clients/authenticated.js');
const PublicClient = require('./clients/public.js');
const Orderbook = require('./orderbook.js');
const Utils = require('./utilities.js');

// Orderbook syncing
class OrderbookSync extends WebsocketClient {
constructor(
productIDs,
apiURI = 'https://api.gdax.com',
websocketURI = 'wss://ws-feed.gdax.com',
authenticatedClient = null,
auth = null,
{ heartbeat = false } = {}
) {
super(productIDs, websocketURI, authenticatedClient, { heartbeat });
super(productIDs, websocketURI, auth, { heartbeat });
this.apiURI = apiURI;
this.authenticatedClient = authenticatedClient;
this.auth = Utils.checkAuth(auth);

this._queues = {}; // []
this._sequences = {}; // -1
this._public_clients = {};
this.books = {};

if (this.auth.secret) {
this._authenticatedClient = new AuthenticatedClient(
this.auth.key,
this.auth.secret,
this.auth.passphrase,
this.apiURI
);
}

this.productIDs.forEach(productID => {
this._queues[productID] = [];
this._sequences[productID] = -2;
Expand Down Expand Up @@ -55,8 +66,8 @@ class OrderbookSync extends WebsocketClient {
const bookLevel = 3;
const args = { level: bookLevel };

if (this.authenticatedClient) {
this.authenticatedClient
if (this._authenticatedClient) {
this._authenticatedClient
.getProductOrderBook(args, productID)
.then(onData.bind(this))
.catch(onError.bind(this));
Expand Down
10 changes: 10 additions & 0 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ function determineProductIDs(productIDs) {
return [productIDs];
}

function checkAuth(auth) {
if (auth && !(auth.secret && auth.key && auth.passphrase)) {
throw new Error(
'Invalid or incomplete authentication credentials. You should either provide all of the secret, key and passphrase fields, or leave auth null'
);
}
return auth || {};
}

module.exports = {
determineProductIDs,
checkAuth,
};
42 changes: 29 additions & 13 deletions tests/orderbook_sync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,13 @@ suite('OrderbookSync', () => {
});
});

test('passes authentication details to websocket (with AuthenticatedClient)', done => {
test('passes authentication details to websocket', done => {
const server = testserver(++port, () => {
const authClient = new Gdax.AuthenticatedClient(
'suchkey',
'suchsecret',
'muchpassphrase'
);
new Gdax.OrderbookSync(
'BTC-USD',
EXCHANGE_API_URL,
'ws://localhost:' + port,
authClient
{ key: 'suchkey', secret: 'suchsecret', passphrase: 'muchpassphrase' }
);
});

Expand All @@ -59,6 +54,29 @@ suite('OrderbookSync', () => {
});
});

test('passes authentication details to websocket (via AuthenticationClient for backwards compatibility)', done => {
const server = testserver(++port, () => {
new Gdax.OrderbookSync(
'BTC-USD',
EXCHANGE_API_URL,
'ws://localhost:' + port,
new Gdax.AuthenticatedClient('mykey', 'mysecret', 'mypassphrase')
);
});

server.on('connection', socket => {
socket.on('message', data => {
const msg = JSON.parse(data);
assert.equal(msg.type, 'subscribe');
assert.equal(msg.key, 'mykey');
assert.equal(msg.passphrase, 'mypassphrase');

server.close();
done();
});
});
});

test('emits a message event', done => {
nock(EXCHANGE_API_URL)
.get('/products/BTC-USD/book?level=3')
Expand Down Expand Up @@ -89,7 +107,7 @@ suite('OrderbookSync', () => {
});
});

test('emits a message event (with AuthenticatedClient)', done => {
test('emits a message event (with auth)', done => {
nock(EXCHANGE_API_URL)
.get('/products/BTC-USD/book?level=3')
.times(2)
Expand All @@ -99,12 +117,11 @@ suite('OrderbookSync', () => {
});

const server = testserver(++port, () => {
const authClient = new Gdax.AuthenticatedClient('key', 'secret', 'pass');
const orderbookSync = new Gdax.OrderbookSync(
'BTC-USD',
EXCHANGE_API_URL,
'ws://localhost:' + port,
authClient
{ key: 'key', secret: 'secret', passphrase: 'pass' }
);
orderbookSync.on('message', data => {
assert.deepEqual(data, {
Expand Down Expand Up @@ -147,18 +164,17 @@ suite('OrderbookSync', () => {
});
});

test('emits an error event on error (with AuthenticatedClient)', done => {
test('emits an error event on error (with auth)', done => {
nock(EXCHANGE_API_URL)
.get('/products/BTC-USD/book?level=3')
.replyWithError('whoops');

const server = testserver(++port, () => {
const authClient = new Gdax.AuthenticatedClient('key', 'secret', 'pass');
const orderbookSync = new Gdax.OrderbookSync(
'BTC-USD',
EXCHANGE_API_URL,
'ws://localhost:' + port,
authClient
{ key: 'key', secret: 'secret', passphrase: 'pass' }
);

orderbookSync.on('message', () =>
Expand Down

0 comments on commit 9802f7f

Please sign in to comment.