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

Commit

Permalink
Add a default timeout to each request (#198)
Browse files Browse the repository at this point in the history
* Add a default timeout to each request

* Address some comments

* bump prettier

* formatting
  • Loading branch information
CjS77 authored Jan 9, 2018
1 parent cc0e932 commit 100abd2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/clients/authenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const { signRequest } = require('../../lib/request_signer');
const PublicClient = require('./public.js');

class AuthenticatedClient extends PublicClient {
constructor(key, secret, passphrase, apiURI) {
super(apiURI);
constructor(key, secret, passphrase, apiURI, options = {}) {
super(apiURI, options);
this.key = key;
this.secret = secret;
this.passphrase = passphrase;
Expand Down
5 changes: 4 additions & 1 deletion lib/clients/public.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const request = require('request');
const { Readable } = require('stream');
const DEFAULT_TIMEOUT = 10 * 1000; // 10 sec

class PublicClient {
constructor(apiURI = 'https://api.gdax.com') {
constructor(apiURI = 'https://api.gdax.com', options = {}) {
this.productID = 'BTC-USD';
if (apiURI && !apiURI.startsWith('http')) {
process.emitWarning(
Expand All @@ -15,6 +16,7 @@ class PublicClient {

this.apiURI = apiURI;
this.API_LIMIT = 100;
this.timeout = +options.timeout > 0 ? options.timeout : DEFAULT_TIMEOUT;
}

get(...args) {
Expand Down Expand Up @@ -86,6 +88,7 @@ class PublicClient {
method: method.toUpperCase(),
uri: this.makeAbsoluteURI(this.makeRelativeURI(uriParts)),
qsStringifyOptions: { arrayFormat: 'repeat' },
timeout: this.timeout,
});
this.addHeaders(opts);
const p = new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"lint-staged": "^3.6.1",
"mocha": "3.4.2",
"nock": "9.0.13",
"prettier": "^1.4.4"
"prettier": "^1.9.2"
},
"directories": {
"lib": "./lib"
Expand Down
1 change: 1 addition & 0 deletions tests/public_client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ suite('PublicClient', () => {
assert.equal(client.apiURI, EXCHANGE_API_URL);
assert.equal(client.API_LIMIT, 100);
assert.equal(client.productID, 'BTC-USD'); // deprecated
assert.equal(client.timeout, 10000);

client = new Gdax.PublicClient('https://api-public.sandbox.gdax.com');
assert.equal(client.apiURI, 'https://api-public.sandbox.gdax.com');
Expand Down

0 comments on commit 100abd2

Please sign in to comment.