Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #647 from LiskHQ/646-change_nethash_to_optional
Browse files Browse the repository at this point in the history
Change nethash to optional - Closes #646
  • Loading branch information
willclarktech committed Apr 18, 2018
2 parents ee58f3c + 784e7bb commit 765cd02
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
40 changes: 22 additions & 18 deletions src/api_client/api_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import {

const defaultOptions = {
bannedNode: [],
version: '1.0.0',
minVersion: '>=1.0.0',
randomizeNode: true,
};

Expand All @@ -45,16 +43,9 @@ const commonHeaders = {
os: 'lisk-js-api',
};

const getHeaders = (nethash, version, minVersion) =>
Object.assign({}, commonHeaders, {
nethash,
version,
minVersion,
});

export default class APIClient {
constructor(nodes, nethash, providedOptions = {}) {
this.initialize(nodes, nethash, providedOptions);
constructor(nodes, providedOptions = {}) {
this.initialize(nodes, providedOptions);

this.accounts = new AccountsResource(this);
this.blocks = new BlocksResource(this);
Expand All @@ -69,29 +60,42 @@ export default class APIClient {
}

static createMainnetAPIClient(options) {
return new APIClient(MAINNET_NODES, MAINNET_NETHASH, options);
return new APIClient(
MAINNET_NODES,
Object.assign({}, { nethash: MAINNET_NETHASH }, options),
);
}

static createTestnetAPIClient(options) {
return new APIClient(TESTNET_NODES, TESTNET_NETHASH, options);
return new APIClient(
TESTNET_NODES,
Object.assign({}, { nethash: TESTNET_NETHASH }, options),
);
}

static createBetanetAPIClient(options) {
return new APIClient(BETANET_NODES, BETANET_NETHASH, options);
return new APIClient(
BETANET_NODES,
Object.assign({}, { nethash: BETANET_NETHASH }, options),
);
}

initialize(nodes, nethash, providedOptions = {}) {
initialize(nodes, providedOptions = {}) {
if (!Array.isArray(nodes) || nodes.length <= 0) {
throw new Error('APIClient requires nodes for initialization.');
}

if (typeof nethash !== 'string' || nethash === '') {
throw new Error('APIClient requires nethash for initialization.');
if (typeof providedOptions !== 'object' || Array.isArray(providedOptions)) {
throw new Error(
'APIClient takes an optional object as the second parameter.',
);
}

const options = Object.assign({}, defaultOptions, providedOptions);

this.headers = getHeaders(nethash, options.version, options.minVersion);
this.headers = options.nethash
? Object.assign({}, commonHeaders, { nethash: options.nethash })
: Object.assign({}, commonHeaders);
this.nodes = nodes;
this.bannedNodes = [...(options.bannedNodes || [])];
this.currentNode = options.node || this.getNewNode();
Expand Down
49 changes: 22 additions & 27 deletions test/api_client/api_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,13 @@ describe('APIClient module', () => {
];
const defaultHeaders = {
'Content-Type': 'application/json',
nethash: mainnetHash,
os: 'lisk-js-api',
version: '1.0.0',
minVersion: '>=1.0.0',
};

const customHeaders = {
'Content-Type': 'application/json',
nethash: testnetHash,
os: 'lisk-js-api',
version: '0.5.0',
minVersion: '>=0.1.0',
nethash: testnetHash,
};

const localNode = 'http://localhost:7000';
Expand All @@ -62,7 +57,7 @@ describe('APIClient module', () => {
let apiClient;

beforeEach(() => {
apiClient = new APIClient(defaultNodes, mainnetHash);
apiClient = new APIClient(defaultNodes);
return Promise.resolve();
});

Expand All @@ -81,7 +76,7 @@ describe('APIClient module', () => {
});

it('should call initialize', () => {
apiClient = new APIClient(defaultNodes, mainnetHash);
apiClient = new APIClient(defaultNodes);
return expect(initializeStub).to.be.calledOnce;
});
});
Expand Down Expand Up @@ -168,22 +163,22 @@ describe('APIClient module', () => {
);
});

it('should throw an error if no second argument is passed to constructor', () => {
return expect(
apiClient.initialize.bind(apiClient, defaultNodes),
).to.throw(Error, 'APIClient requires nethash for initialization.');
});

it('should throw an error if second argument passed to constructor is not string', () => {
it('should throw an error if second argument passed to constructor is a string', () => {
return expect(
apiClient.initialize.bind(apiClient, defaultNodes, 123),
).to.throw(Error, 'APIClient requires nethash for initialization.');
apiClient.initialize.bind(apiClient, defaultNodes, 'option string'),
).to.throw(
Error,
'APIClient takes an optional object as the second parameter.',
);
});

it('should throw an error if second argument passed to constructor is empty string', () => {
it('should throw an error if second argument passed to constructor is an array', () => {
return expect(
apiClient.initialize.bind(apiClient, defaultNodes, ''),
).to.throw(Error, 'APIClient requires nethash for initialization.');
apiClient.initialize.bind(apiClient, defaultNodes, []),
).to.throw(
Error,
'APIClient takes an optional object as the second parameter.',
);
});

describe('headers', () => {
Expand All @@ -194,9 +189,9 @@ describe('APIClient module', () => {
});

it('should set custom headers with supplied options', () => {
apiClient = new APIClient(defaultNodes, testnetHash, {
apiClient = new APIClient(defaultNodes, {
version: customHeaders.version,
minVersion: customHeaders.minVersion,
nethash: testnetHash,
});
return expect(apiClient)
.to.have.property('headers')
Expand All @@ -221,7 +216,7 @@ describe('APIClient module', () => {

it('should set bannedNodes when passed as an option', () => {
const bannedNodes = ['a', 'b'];
apiClient = new APIClient(defaultNodes, mainnetHash, { bannedNodes });
apiClient = new APIClient(defaultNodes, { bannedNodes });
return expect(apiClient)
.to.have.property('bannedNodes')
.be.eql(bannedNodes);
Expand All @@ -235,7 +230,7 @@ describe('APIClient module', () => {
});

it('should set with supplied node if node is specified by options', () => {
apiClient = new APIClient(defaultNodes, mainnetHash, {
apiClient = new APIClient(defaultNodes, {
node: externalTestnetNode,
});
return expect(apiClient)
Expand All @@ -246,21 +241,21 @@ describe('APIClient module', () => {

describe('randomizeNodes', () => {
it('should set randomizeNodes to true when randomizeNodes not explicitly set', () => {
apiClient = new APIClient(defaultNodes, mainnetHash, {
apiClient = new APIClient(defaultNodes, {
randomizeNodes: undefined,
});
return expect(apiClient).to.have.property('randomizeNodes').be.true;
});

it('should set randomizeNodes to true on initialization when passed as an option', () => {
apiClient = new APIClient(defaultNodes, mainnetHash, {
apiClient = new APIClient(defaultNodes, {
randomizeNodes: true,
});
return expect(apiClient).to.have.property('randomizeNodes').be.true;
});

it('should set randomizeNodes to false on initialization when passed as an option', () => {
apiClient = new APIClient(defaultNodes, mainnetHash, {
apiClient = new APIClient(defaultNodes, {
randomizeNodes: false,
});
return expect(apiClient).to.have.property('randomizeNodes').be.false;
Expand Down

0 comments on commit 765cd02

Please sign in to comment.