Skip to content

Commit

Permalink
sift science's V3 API uses Basic Auth header instead of query strings
Browse files Browse the repository at this point in the history
  • Loading branch information
officert committed Jun 19, 2017
1 parent 6cd7103 commit 1d29eff
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 34 deletions.
41 changes: 20 additions & 21 deletions lib/client/decisions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const Promise = require('bluebird');
const v3HttpClient = require('./v3HttpClient');
const _ = require('lodash');

function getDecisionsApi(siftScienceClient) {
const key = siftScienceClient._key;
Expand All @@ -16,11 +15,11 @@ function getDecisionsApi(siftScienceClient) {
if (!accountId) return Promise.reject(new Error('accountId is required'));
if (!userId) return Promise.reject(new Error('userId is required'));

_.extend(data, {
$api_key: key
});

return Promise.resolve(v3HttpClient.post(`/accounts/${accountId}/users/${userId}/decisions`, data, params))
return Promise.resolve(v3HttpClient.post(`/accounts/${accountId}/users/${userId}/decisions`, data, params, {
auth: {
username: key
}
}))
.then(response => response.data)
.catch(error => {
logger.logRequestError(error);
Expand All @@ -36,11 +35,11 @@ function getDecisionsApi(siftScienceClient) {
if (!accountId) return Promise.reject(new Error('accountId is required'));
if (!userId) return Promise.reject(new Error('userId is required'));

_.extend(params, {
api_key: key
});

return Promise.resolve(v3HttpClient.get(`/accounts/${accountId}/users/${userId}/decisions`, params))
return Promise.resolve(v3HttpClient.get(`/accounts/${accountId}/users/${userId}/decisions`, params, {
auth: {
username: key
}
}))
.then(response => response.data)
.catch(error => {
logger.logRequestError(error);
Expand All @@ -56,11 +55,11 @@ function getDecisionsApi(siftScienceClient) {
if (!accountId) return Promise.reject(new Error('accountId is required'));
if (!orderId) return Promise.reject(new Error('orderId is required'));

_.extend(params, {
api_key: key
});

return Promise.resolve(v3HttpClient.get(`/accounts/${accountId}/orders/${orderId}/decisions`, params))
return Promise.resolve(v3HttpClient.get(`/accounts/${accountId}/orders/${orderId}/decisions`, params, {
auth: {
username: key
}
}))
.then(response => response.data)
.catch(error => {
logger.logRequestError(error);
Expand All @@ -75,11 +74,11 @@ function getDecisionsApi(siftScienceClient) {
listByAccountId(accountId, params = {}) {
if (!accountId) return Promise.reject(new Error('accountId is required'));

_.extend(params, {
api_key: key
});

return Promise.resolve(v3HttpClient.get(`/accounts/${accountId}/decisions`, params))
return Promise.resolve(v3HttpClient.get(`/accounts/${accountId}/decisions`, params, {
auth: {
username: key
}
}))
.then(response => response.data)
.catch(error => {
logger.logRequestError(error);
Expand Down
39 changes: 27 additions & 12 deletions lib/client/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,43 @@ class HttpClient {
});
}

get(path, params) {
/**
* @param {String} path
* @param {Object} [params]
* @param {Object} [options]
*/
get(path, params, options = {}) {
if (!path) return Promise.reject(new Error('path is required'));

return this._client.get(path, (params ? {
params
} : null));
options.params = params;

return this._client.get(path, options);
}

post(path, body = {}, params) {
/**
* @param {String} path
* @param {Object} [params]
* @param {Object} [options]
*/
post(path, body = {}, params, options = {}) {
if (!path) return Promise.reject(new Error('path is required'));

return this._client.post(path, body, (params ? {
params
} : null));
options.params = params;

return this._client.post(path, body, options);
}

delete(path, params) {
/**
* @param {String} path
* @param {Object} [params]
* @param {Object} [options]
*/
delete(path, params, options = {}) {
if (!path) return Promise.reject(new Error('path is required'));

return this._client.delete(path, (params ? {
params
} : null));
options.params = params;

return this._client.delete(path, options);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-siftscience",
"version": "0.0.15",
"version": "0.0.16",
"description": "Node HTTP client for Sift Science's API",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/client/decision/applyByAccountIdAndUserId-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ describe('lib', () => {
postStub.callCount.should.equal(1);
postStub.args[0][0].should.equal(`/accounts/${accountId}/users/${userId}/decisions`);
postStub.args[0][1].should.deepEqual(data);
postStub.args[0][2].should.deepEqual({});
postStub.args[0][3].should.deepEqual({
auth: {
username: siftScienceClient._key
}
});
});
});
});
Expand Down Expand Up @@ -115,6 +121,11 @@ describe('lib', () => {
$api_key: key
}));
postStub.args[0][2].should.deepEqual(params);
postStub.args[0][3].should.deepEqual({
auth: {
username: siftScienceClient._key
}
});
});
});
});
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/client/decision/getByAccountIdAndOrderId-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ describe('lib', () => {

getStub.callCount.should.equal(1);
getStub.args[0][0].should.equal(`/accounts/${accountId}/orders/${orderId}/decisions`);
getStub.args[0][1].should.deepEqual({});
getStub.args[0][2].should.deepEqual({
auth: {
username: siftScienceClient._key
}
});
});
});
});
Expand Down Expand Up @@ -108,6 +114,11 @@ describe('lib', () => {
getStub.callCount.should.equal(1);
getStub.args[0][0].should.equal(`/accounts/${accountId}/orders/${orderId}/decisions`);
getStub.args[0][1].should.deepEqual(params);
getStub.args[0][2].should.deepEqual({
auth: {
username: siftScienceClient._key
}
});
});
});
});
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/client/decision/getByAccountIdAndUserId-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ describe('lib', () => {

getStub.callCount.should.equal(1);
getStub.args[0][0].should.equal(`/accounts/${accountId}/users/${userId}/decisions`);
getStub.args[0][1].should.deepEqual({});
getStub.args[0][2].should.deepEqual({
auth: {
username: siftScienceClient._key
}
});
});
});
});
Expand Down Expand Up @@ -108,6 +114,11 @@ describe('lib', () => {
getStub.callCount.should.equal(1);
getStub.args[0][0].should.equal(`/accounts/${accountId}/users/${userId}/decisions`);
getStub.args[0][1].should.deepEqual(params);
getStub.args[0][2].should.deepEqual({
auth: {
username: siftScienceClient._key
}
});
});
});
});
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/client/decision/listByAccountId-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('lib', () => {

getStub.callCount.should.equal(1);
getStub.args[0][0].should.equal(`/accounts/${accountId}/decisions`);
getStub.args[0][1].should.deepEqual({});
getStub.args[0][2].should.deepEqual({
auth: {
username: siftScienceClient._key
}
});
});
});
});
Expand Down Expand Up @@ -91,6 +97,11 @@ describe('lib', () => {
getStub.callCount.should.equal(1);
getStub.args[0][0].should.equal(`/accounts/${accountId}/decisions`);
getStub.args[0][1].should.deepEqual(params);
getStub.args[0][2].should.deepEqual({
auth: {
username: siftScienceClient._key
}
});
});
});
});
Expand Down
35 changes: 35 additions & 0 deletions tests/lib/client/httpClient/delete-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,41 @@ describe('lib', () => {
});
});
});

describe('when options are passed', () => {
let path = '/foobar';
let params = {
query: 'string'
};
let options = {
auth: {
username: 'FOOBAR'
}
};
let expectedResponse = {};

let deleteStub;

before('stub httpClient._client.delete()', () => {
deleteStub = sandbox.stub(httpClient._client, 'delete')
.returns(Promise.resolve(expectedResponse));
});

it('should resolve and call httpClientClient._client.delete()', () => {
return httpClient.delete(path, params, options)
.then(response => {
should.exist(response);
response.should.deepEqual(expectedResponse);

deleteStub.callCount.should.equal(1);
deleteStub.args[0][0].should.equal(path);
deleteStub.args[0][1].should.deepEqual({
auth: options.auth,
params
});
});
});
});
});
});
});
Expand Down
35 changes: 35 additions & 0 deletions tests/lib/client/httpClient/get-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,41 @@ describe('lib', () => {
});
});
});

describe('when options are passed', () => {
let path = '/foobar';
let params = {
query: 'string'
};
let options = {
auth: {
username: 'FOOBAR'
}
};
let expectedResponse = {};

let getStub;

before('stub httpClient._client.get()', () => {
getStub = sandbox.stub(httpClient._client, 'get')
.returns(Promise.resolve(expectedResponse));
});

it('should resolve and call v204Client._client.get()', () => {
return httpClient.get(path, params, options)
.then(response => {
should.exist(response);
response.should.deepEqual(expectedResponse);

getStub.callCount.should.equal(1);
getStub.args[0][0].should.equal(path);
getStub.args[0][1].should.deepEqual({
params,
auth: options.auth
});
});
});
});
});
});
});
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/client/httpClient/post-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,45 @@ describe('lib', () => {
});
});
});

describe('when options are passed', () => {
let path = '/foobar';
let body = {
key: 'value'
};
let params = {
query: 'string'
};
let options = {
auth: {
username: 'FOOBAR'
}
};
let response = {};

let postStub;

before('stub httpClient._client.post()', () => {
postStub = sandbox.stub(httpClient._client, 'post')
.returns(Promise.resolve(response));
});

it('should resolve and call httpClient._client.post()', () => {
return httpClient.post(path, body, params, options)
.then(result => {
should.exist(result);
result.should.deepEqual(response);

postStub.callCount.should.equal(1);
postStub.args[0][0].should.equal(path);
postStub.args[0][1].should.deepEqual(body);
postStub.args[0][2].should.deepEqual({
auth: options.auth,
params
});
});
});
});
});
});
});
Expand Down

0 comments on commit 1d29eff

Please sign in to comment.