Skip to content

Commit

Permalink
start decisions API
Browse files Browse the repository at this point in the history
  • Loading branch information
officert committed Jun 13, 2017
1 parent f5b4b36 commit 3e269bb
Show file tree
Hide file tree
Showing 40 changed files with 632 additions and 545 deletions.
31 changes: 31 additions & 0 deletions lib/client/decisions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Promise = require('bluebird');
const v204HttpClient = require('./v204HttpClient');
const _ = require('lodash');

function getDecisionsApi(siftScienceClient) {
const key = siftScienceClient._key;
const logger = siftScienceClient._logger;

return {
/**
* @param {Object} [data]
* @param {Object} [params]
* https://siftscience.com/developers/docs/curl/score-api/score-api
*/
getByUserId(userId, params = {}) {
if (!userId) return Promise.reject(new Error('userId is required'));

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

return Promise.resolve(v204HttpClient.get(`/users/${userId}/score`, {
params
}))
.then(response => response.data)
.catch(error => logger.logRequestError(error));
}
};
}

module.exports = getDecisionsApi;
4 changes: 2 additions & 2 deletions lib/client/events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Promise = require('bluebird');
const v204 = require('./v204');
const v204HttpClient = require('./v204HttpClient');
const _ = require('lodash');

function getEventsApi(siftScienceClient) {
Expand All @@ -17,7 +17,7 @@ function getEventsApi(siftScienceClient) {
$api_key: key
});

return Promise.resolve(v204.post('/events', data, {
return Promise.resolve(v204HttpClient.post('/events', data, {
params
}))
.then(response => response.data)
Expand Down
10 changes: 5 additions & 5 deletions lib/client/v204/httpClient.js → lib/client/httpClient.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const axios = require('axios');
const Promise = require('bluebird');

const SIFT_SCIENCE_API_V204_BASE_URL = 'https://api.siftscience.com/v204';

class HttpClient {
constructor() {
constructor(baseURL) {
if (!baseURL) throw new Error('baseURL is required');

this._client = axios.create({
baseURL: SIFT_SCIENCE_API_V204_BASE_URL
baseURL: baseURL
});
}

Expand Down Expand Up @@ -35,4 +35,4 @@ class HttpClient {
}
}

module.exports = new HttpClient();
module.exports = HttpClient;
2 changes: 2 additions & 0 deletions lib/client/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const getEventsApi = require('./events');
const getLabelsApi = require('./labels');
const getScoreApi = require('./score');
const getDecisionsApi = require('./decisions');

const Logger = require('../logger');

Expand All @@ -17,6 +18,7 @@ class SiftScienceClient {
this.events = getEventsApi(this);
this.labels = getLabelsApi(this);
this.score = getScoreApi(this);
this.decisions = getDecisionsApi(this);
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/client/labels.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Promise = require('bluebird');
const v204 = require('./v204');
const v204HttpClient = require('./v204HttpClient');
const _ = require('lodash');

function getLabelsApi(siftScienceClient) {
Expand All @@ -20,7 +20,7 @@ function getLabelsApi(siftScienceClient) {
$api_key: key
});

return Promise.resolve(v204.post(`/users/${userId}/labels`, data, {
return Promise.resolve(v204HttpClient.post(`/users/${userId}/labels`, data, {
params
}))
.then(response => response.data)
Expand All @@ -38,7 +38,7 @@ function getLabelsApi(siftScienceClient) {
$api_key: key
});

return Promise.resolve(v204.delete(`/users/${userId}/labels`, {
return Promise.resolve(v204HttpClient.delete(`/users/${userId}/labels`, {
params
}))
.then(response => response.data)
Expand Down
4 changes: 2 additions & 2 deletions lib/client/score.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Promise = require('bluebird');
const v204 = require('./v204');
const v204HttpClient = require('./v204HttpClient');
const _ = require('lodash');

function getScoreApi(siftScienceClient) {
Expand All @@ -19,7 +19,7 @@ function getScoreApi(siftScienceClient) {
$api_key: key
});

return Promise.resolve(v204.get(`/users/${userId}/score`, {
return Promise.resolve(v204HttpClient.get(`/users/${userId}/score`, {
params
}))
.then(response => response.data)
Expand Down
1 change: 0 additions & 1 deletion lib/client/v204/index.js

This file was deleted.

5 changes: 5 additions & 0 deletions lib/client/v204HttpClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const HttpClient = require('./httpClient');

const V204_BASE_URL = 'https://api.siftscience.com/v204';

module.exports = new HttpClient(V204_BASE_URL);
5 changes: 5 additions & 0 deletions lib/client/v3HttpClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const HttpClient = require('./httpClient');

const V3_BASE_URL = 'https://api.siftscience.com/v3';

module.exports = new HttpClient(V3_BASE_URL);
16 changes: 8 additions & 8 deletions tests/lib/client/events/addItemToCart-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const sinon = require('sinon');
const _ = require('lodash');

let siftScience;
let v204;
let v204HttpClient;
let sandbox;

before(() => {
siftScience = require('../../../../lib');
v204 = require('../../../../lib/client/v204');
v204HttpClient = require('../../../../lib/client/v204HttpClient');

sandbox = sinon.sandbox.create();
});
Expand All @@ -36,12 +36,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.addItemToCart()
.then(result => {
should.exist(result);
Expand All @@ -67,12 +67,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.addItemToCart(data)
.then(result => {
should.exist(result);
Expand Down
16 changes: 8 additions & 8 deletions tests/lib/client/events/addPromotion-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const sinon = require('sinon');
const _ = require('lodash');

let siftScience;
let v204;
let v204HttpClient;
let sandbox;

before(() => {
siftScience = require('../../../../lib');
v204 = require('../../../../lib/client/v204');
v204HttpClient = require('../../../../lib/client/v204HttpClient');

sandbox = sinon.sandbox.create();
});
Expand All @@ -36,12 +36,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.addPromotion()
.then(result => {
should.exist(result);
Expand All @@ -67,12 +67,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.addPromotion(data)
.then(result => {
should.exist(result);
Expand Down
16 changes: 8 additions & 8 deletions tests/lib/client/events/chargeback-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const sinon = require('sinon');
const _ = require('lodash');

let siftScience;
let v204;
let v204HttpClient;
let sandbox;

before(() => {
siftScience = require('../../../../lib');
v204 = require('../../../../lib/client/v204');
v204HttpClient = require('../../../../lib/client/v204HttpClient');

sandbox = sinon.sandbox.create();
});
Expand All @@ -36,12 +36,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.chargeback()
.then(result => {
should.exist(result);
Expand All @@ -67,12 +67,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.chargeback(data)
.then(result => {
should.exist(result);
Expand Down
16 changes: 8 additions & 8 deletions tests/lib/client/events/contentStatus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const sinon = require('sinon');
const _ = require('lodash');

let siftScience;
let v204;
let v204HttpClient;
let sandbox;

before(() => {
siftScience = require('../../../../lib');
v204 = require('../../../../lib/client/v204');
v204HttpClient = require('../../../../lib/client/v204HttpClient');

sandbox = sinon.sandbox.create();
});
Expand All @@ -36,12 +36,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.contentStatus()
.then(result => {
should.exist(result);
Expand All @@ -67,12 +67,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.contentStatus(data)
.then(result => {
should.exist(result);
Expand Down
22 changes: 11 additions & 11 deletions tests/lib/client/events/create-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const sinon = require('sinon');
const _ = require('lodash');

let siftScience;
let v204;
let v204HttpClient;
let sandbox;

before(() => {
siftScience = require('../../../../lib');
v204 = require('../../../../lib/client/v204');
v204HttpClient = require('../../../../lib/client/v204HttpClient');

sandbox = sinon.sandbox.create();
});
Expand All @@ -36,12 +36,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.create()
.then(result => {
should.exist(result);
Expand All @@ -66,12 +66,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.create(data)
.then(result => {
should.exist(result);
Expand Down Expand Up @@ -103,12 +103,12 @@ describe('lib', () => {
data: {}
};

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

it('should call v204.post()', () => {
it('should call v204HttpClient.post()', () => {
return siftScienceClient.events.create(data, params)
.then(result => {
should.exist(result);
Expand Down
Loading

0 comments on commit 3e269bb

Please sign in to comment.