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

Commit

Permalink
Removes doPopsicleRequest function, cleanup after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Schwarz committed Aug 10, 2017
1 parent abe6390 commit 60abee9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 55 deletions.
6 changes: 0 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,5 @@

<script src="dist-browser/lisk-js.min.js"></script>


<script>
lisk.api().listMultisignatureTransactions(function(res) {
console.log(res);
});
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/api/liskApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ LiskAPI.prototype.broadcastSignedTransaction = function broadcastSignedTransacti
requestParams: { transaction },
};

privateApi.doPopsicleRequest.call(this, 'POST', request).then(result => callback(result.body));
privateApi.sendRequestPromise.call(this, 'POST', request).then(result => callback(result.body));
};

module.exports = LiskAPI;
36 changes: 11 additions & 25 deletions src/api/privateApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,29 +187,30 @@ function serialiseHttpData(data) {

/**
* @method createRequestObject
* @param requestMethod
* @param method
* @param requestType
* @param options
* @param providedOptions
* @private
*
* @return request Object
*/

function createRequestObject(requestMethod, requestType, options) {
const requestUrl = requestMethod === 'GET'
? `${getFullUrl.call(this)}/api/${requestType}${serialiseHttpData.call(this, options, requestMethod)}`
function createRequestObject(method, requestType, providedOptions) {
const options = providedOptions || {};
const url = method === 'GET'
? `${getFullUrl.call(this)}/api/${requestType}${serialiseHttpData.call(this, options)}`
: `${getFullUrl.call(this)}/api/${requestType}`;

return {
method: requestMethod,
url: requestUrl,
method,
url,
headers: this.nethash,
body: requestMethod === 'GET' ? {} : options,
body: method === 'GET' ? {} : options,
};
}

/**
* @method doPopsicleRequest
* @method sendRequestPromise
* @param requestMethod
* @param requestType
* @param options
Expand All @@ -218,26 +219,12 @@ function createRequestObject(requestMethod, requestType, options) {
* @return APIcall Promise
*/

function doPopsicleRequest(requestMethod, requestType, options) {
function sendRequestPromise(requestMethod, requestType, options) {
const requestObject = createRequestObject.call(this, requestMethod, requestType, options);

return popsicle.request(requestObject).use(popsicle.plugins.parse(['json', 'urlencoded']));
}

/**
* @method sendRequestPromise
* @param requestMethod
* @param requestType
* @param options
* @private
*
* @return APIcall Promise
*/

function sendRequestPromise(requestMethod, requestType, options) {
return doPopsicleRequest.call(this, requestMethod, requestType, options);
}

module.exports = {
netHashOptions,
getFullUrl,
Expand All @@ -248,7 +235,6 @@ module.exports = {
checkReDial,
checkOptions,
sendRequestPromise,
doPopsicleRequest,
serialiseHttpData,
createRequestObject,
};
93 changes: 70 additions & 23 deletions test/api/liskApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('Lisk.api()', () => {
const localNode = 'localhost';
const externalNode = 'external';
const defaultSecret = 'secret';
const GET = 'GET';
const POST = 'POST';

describe('liskApi()', () => {
it('should create a new instance when using liskApi()', () => {
Expand Down Expand Up @@ -283,13 +285,13 @@ describe('Lisk.api()', () => {
describe('#checkOptions', () => {
it('should not accept falsy options like undefined', () => {
(function sendRequestWithUndefinedLimit() {
liskApi().sendRequest('GET', 'delegates/', { limit: undefined }, () => {});
liskApi().sendRequest(GET, 'delegates/', { limit: undefined }, () => {});
}).should.throw('parameter value "limit" should not be undefined');
});

it('should not accept falsy options like NaN', () => {
(function sendRequestWithNaNLimit() {
liskApi().sendRequest('GET', 'delegates/', { limit: NaN }, () => {});
liskApi().sendRequest(GET, 'delegates/', { limit: NaN }, () => {});
}).should.throw('parameter value "limit" should not be NaN');
});
});
Expand All @@ -300,7 +302,7 @@ describe('Lisk.api()', () => {
body: { success: true, height: 2850466 },
};
const stub = sinon.stub(privateApi, 'sendRequestPromise').resolves(expectedResponse);
return LSK.sendRequest('GET', 'blocks/getHeight', (data) => {
return LSK.sendRequest(GET, 'blocks/getHeight', (data) => {
(data).should.be.ok();
(data).should.be.type('object');
(data.success).should.be.true();
Expand Down Expand Up @@ -335,7 +337,7 @@ describe('Lisk.api()', () => {

LSK.listActiveDelegates('1', callback);

(LSK.sendRequest.calledWith('GET', 'delegates/', options)).should.be.true();
(LSK.sendRequest.calledWith(GET, 'delegates/', options)).should.be.true();
(callback.called).should.be.true();
(callback.calledWith(expectedResponse)).should.be.true();
LSK.sendRequest.restore();
Expand Down Expand Up @@ -368,7 +370,7 @@ describe('Lisk.api()', () => {

LSK.listStandbyDelegates('1', callback);

(LSK.sendRequest.calledWith('GET', 'delegates/', options)).should.be.true();
(LSK.sendRequest.calledWith(GET, 'delegates/', options)).should.be.true();
(callback.called).should.be.true();
(callback.calledWith(expectedResponse)).should.be.true();
LSK.sendRequest.restore();
Expand Down Expand Up @@ -458,7 +460,7 @@ describe('Lisk.api()', () => {

LSK.listForgedBlocks(key, callback);

(LSK.sendRequest.calledWith('GET', 'blocks', options)).should.be.true();
(LSK.sendRequest.calledWith(GET, 'blocks', options)).should.be.true();
(callback.called).should.be.true();
(callback.calledWith(expectedResponse)).should.be.true();
LSK.sendRequest.restore();
Expand Down Expand Up @@ -499,7 +501,7 @@ describe('Lisk.api()', () => {

LSK.getBlock(blockId, callback);

(LSK.sendRequest.calledWith('GET', 'blocks', options)).should.be.true();
(LSK.sendRequest.calledWith(GET, 'blocks', options)).should.be.true();
(callback.called).should.be.true();
(callback.calledWith(expectedResponse)).should.be.true();
LSK.sendRequest.restore();
Expand Down Expand Up @@ -546,7 +548,7 @@ describe('Lisk.api()', () => {

LSK.listTransactions(address, '1', '2', callback);

(LSK.sendRequest.calledWith('GET', 'transactions', options)).should.be.true();
(LSK.sendRequest.calledWith(GET, 'transactions', options)).should.be.true();
(callback.called).should.be.true();
(callback.calledWith(expectedResponse)).should.be.true();
LSK.sendRequest.restore();
Expand Down Expand Up @@ -587,7 +589,7 @@ describe('Lisk.api()', () => {

LSK.getTransaction(transactionId, callback);

(LSK.sendRequest.calledWith('GET', 'transactions/get', options)).should.be.true();
(LSK.sendRequest.calledWith(GET, 'transactions/get', options)).should.be.true();
(callback.called).should.be.true();
(callback.calledWith(expectedResponse)).should.be.true();
LSK.sendRequest.restore();
Expand Down Expand Up @@ -653,7 +655,7 @@ describe('Lisk.api()', () => {

LSK.listVoters(publicKey, callback);

(LSK.sendRequest.calledWith('GET', 'delegates/voters', options)).should.be.true();
(LSK.sendRequest.calledWith(GET, 'delegates/voters', options)).should.be.true();
(callback.called).should.be.true();
(callback.calledWith(expectedResponse)).should.be.true();
LSK.sendRequest.restore();
Expand Down Expand Up @@ -688,7 +690,7 @@ describe('Lisk.api()', () => {

LSK.getAccount(address, callback);

(LSK.sendRequest.calledWith('GET', 'accounts', options)).should.be.true();
(LSK.sendRequest.calledWith(GET, 'accounts', options)).should.be.true();
(callback.called).should.be.true();
(callback.calledWith(expectedResponse)).should.be.true();
LSK.sendRequest.restore();
Expand Down Expand Up @@ -735,7 +737,7 @@ describe('Lisk.api()', () => {

LSKnode.sendLSK(recipient, amount, secret, secondSecret, callback);

(LSKnode.sendRequest.calledWith('POST', 'transactions', {
(LSKnode.sendRequest.calledWith(POST, 'transactions', {
recipientId: recipient,
amount,
secret,
Expand Down Expand Up @@ -786,7 +788,7 @@ describe('Lisk.api()', () => {
thisLSK.bannedPeers = liskApi().defaultPeers;
thisLSK.currentPeer = '';

return thisLSK.sendRequest('GET', 'blocks/getHeight').then((e) => {
return thisLSK.sendRequest(GET, 'blocks/getHeight').then((e) => {
(e.message).should.be.equal('could not create http request to any of the given peers');
});
});
Expand Down Expand Up @@ -829,7 +831,7 @@ describe('Lisk.api()', () => {

describe('#sendRequest with promise', () => {
it('should be able to use sendRequest as a promise for GET', () => {
return liskApi().sendRequest('GET', 'blocks/getHeight', {}).then((result) => {
return liskApi().sendRequest(GET, 'blocks/getHeight', {}).then((result) => {
(result).should.be.type('object');
(result.success).should.be.equal(true);
(result.height).should.be.type('number');
Expand All @@ -851,7 +853,7 @@ describe('Lisk.api()', () => {
const recipient = '10279923186189318946L';
const amount = 100000000;

return LSKnode.sendRequest('GET', 'transactions', { recipientId: recipient, secret, secondSecret, amount }).then((result) => {
return LSKnode.sendRequest(GET, 'transactions', { recipientId: recipient, secret, secondSecret, amount }).then((result) => {
(result).should.be.type('object');
(result).should.be.ok();
});
Expand All @@ -868,7 +870,7 @@ describe('Lisk.api()', () => {
stub.resolves(futureTimestampResponse);
stub.onThirdCall().resolves(successResponse);

return thisLSK.sendRequest('POST', 'transactions')
return thisLSK.sendRequest(POST, 'transactions')
.then(() => {
(spy.callCount).should.equal(3);
(spy.args[1][2]).should.have.property('timeOffset').equal(10);
Expand All @@ -887,7 +889,7 @@ describe('Lisk.api()', () => {
const spy = sinon.spy(thisLSK, 'sendRequest');
stub.resolves(futureTimestampResponse);

return thisLSK.sendRequest('POST', 'transactions')
return thisLSK.sendRequest(POST, 'transactions')
.then((response) => {
(response).should.equal(futureTimestampResponse.body);
stub.restore();
Expand Down Expand Up @@ -927,12 +929,17 @@ describe('Lisk.api()', () => {
});

describe('#createRequestObject', () => {
const options = { limit: 5, offset: 3, details: 'moredetails' };
const LSKAPI = liskApi({ node: 'localhost' });
let options;
let LSKAPI;
beforeEach(() => {
options = { limit: 5, offset: 3, details: 'moredetails' };
LSKAPI = liskApi({ node: 'localhost' });
});

it('should create a valid request Object for GET request', () => {
const requestObject = privateApi.createRequestObject.call(LSKAPI, 'GET', 'transaction', options);
const requestObject = privateApi.createRequestObject.call(LSKAPI, GET, 'transaction', options);
const expectedObject = {
method: 'GET',
method: GET,
url: 'http://localhost:8000/api/transaction?limit=5&offset=3&details=moredetails',
headers: {
'Content-Type': 'application/json',
Expand All @@ -950,9 +957,9 @@ describe('Lisk.api()', () => {
});

it('should create a valid request Object for POST request', () => {
const requestObject = privateApi.createRequestObject.call(LSKAPI, 'POST', 'transaction', options);
const requestObject = privateApi.createRequestObject.call(LSKAPI, POST, 'transaction', options);
const expectedObject = {
method: 'POST',
method: POST,
url: 'http://localhost:8000/api/transaction',
headers: {
'Content-Type': 'application/json',
Expand All @@ -968,5 +975,45 @@ describe('Lisk.api()', () => {

(requestObject).should.be.eql(expectedObject);
});

it('should create a valid request Object for POST request without options', () => {
const requestObject = privateApi.createRequestObject.call(LSKAPI, POST, 'transaction');
const expectedObject = {
method: POST,
url: 'http://localhost:8000/api/transaction',
headers: {
'Content-Type': 'application/json',
nethash: 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511',
broadhash: 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511',
os: 'lisk-js-api',
version: '1.0.0',
minVersion: '>=0.5.0',
port: 8000,
},
body: {},
};

(requestObject).should.be.eql(expectedObject);
});

it('should create a valid request Object for undefined request without options', () => {
const requestObject = privateApi.createRequestObject.call(LSKAPI, undefined, 'transaction');
const expectedObject = {
method: undefined,
url: 'http://localhost:8000/api/transaction',
headers: {
'Content-Type': 'application/json',
nethash: 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511',
broadhash: 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511',
os: 'lisk-js-api',
version: '1.0.0',
minVersion: '>=0.5.0',
port: 8000,
},
body: {},
};

(requestObject).should.be.eql(expectedObject);
});
});
});

0 comments on commit 60abee9

Please sign in to comment.