Skip to content

Commit

Permalink
Requesting Profile API for Data (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwhisp committed Jul 13, 2022
1 parent a54ccc5 commit d07ef85
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 18 deletions.
82 changes: 64 additions & 18 deletions modules/1plusXRtdProvider.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
import { submodule } from '../src/hook.js'
import { logMessage } from '../src/utils.js';
import { ajax } from '../src/ajax.js'
import { logMessage, logError, deepAccess, isNumber } from '../src/utils.js';

// Constants
const REAL_TIME_MODULE = 'realTimeData'
const MODULE_NAME = '1plusX'
const REAL_TIME_MODULE = 'realTimeData';
const MODULE_NAME = '1plusX';
const PAPI_VERSION = 'v1.0';

// Functions
const getBidRequestDataAsync = (reqBidsConfigObj, config, userConsent) => {
// We don't
// Maybe treat the case where we already have the audiences & segments in local storage
// Get the required config
// Call PAPI
// -- Then :
// ---- extract relevant data
// ---- set the data to the bid
// -- Catch : print err & do nothing
const extractConfig = (config) => {
// CustomerId
const customerId = deepAccess(config, 'params.customerId');
if (!customerId) {
throw new Error('REQUIRED CUSTOMER ID');
}
// Timeout
const tempTimeout = deepAccess(config, 'params.timeout');
const timeout = isNumber(tempTimeout) && tempTimeout > 300 ? tempTimeout : 1000;

return { customerId, timeout };
}

const getPapiUrl = ({ customerId }) => {
logMessage('GET PAPI URL');
// https://[yourClientId].profiles.tagger.opecloud.com/[VERSION]/targeting?url=
const currentUrl = encodeURIComponent(window.location.href);
const papiUrl = `https://${customerId}.profiles.tagger.opecloud.com/${PAPI_VERSION}/targeting?url=${currentUrl}`;
return papiUrl;
}

const getTargetingDataFromPapi = (papiUrl) => {
return new Promise((resolve, reject) => {
ajax(papiUrl, {
success(responseText, response) {
logMessage(responseText);
resolve(response.response);
},
error(errorText, error) {
console.log(errorText)
console.log(JSON.stringify(error, null, 2))
reject(error);
}
})
})
}

// Functions exported in submodule object
Expand All @@ -24,12 +52,30 @@ const init = (config, userConsent) => {
}

const getBidRequestData = (reqBidsConfigObj, callback, config, userConsent) => {
getBidRequestDataAsync(reqBidsConfigObj, config, userConsent)
.then(() => callback())
.catch((err) => {
logMessage(err);
callback();
})
try {
// Get the required config
const { customerId } = extractConfig(config);
// Get PAPI URL
const papiUrl = getPapiUrl({ customerId })
// Call PAPI
getTargetingDataFromPapi(papiUrl)
.then((response) => {
// -- Then :
// ---- extract relevant data
// ---- set the data to the bid
console.log('REQUEST TO PAPI SUCCESS');
callback();
})
.catch((error) => {
// -- Catch : print err & do nothing
console.log('REQUEST TO PAPI ERROR');
// logError(error);
callback();
})
} catch (error) {
logError(error);
callback();
}
}

// The RTD submodule object to be exported
Expand Down
36 changes: 36 additions & 0 deletions test/spec/modules/1plusXRtdProvider_spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,53 @@
import { config } from 'src/config';
import { logMessage } from 'src/utils';
import { server } from 'test/mocks/xhr.js';
import { onePlusXSubmodule } from 'modules/1plusXRtdProvider';

describe('1plusXRtdProvider', () => {
const reqBidsConfigObj = {};
let fakeServer;

before(() => {
config.resetConfig();
})

after(() => { })

beforeEach(() => {
fakeServer = sinon.createFakeServer();
fakeServer.respondWith("GET", "*", [200, {}, '']);
fakeServer.respondImmediately = true;
fakeServer.autoRespond = true;
})

describe('onePlusXSubmodule', () => {
it('init is successfull', () => {
const initResult = onePlusXSubmodule.init();
expect(initResult).to.be.true;
})

it('callback is called after getBidRequestData', () => {

// Nice case; everything runs as expected
{
const callbackSpy = sinon.spy();
const config = { params: { customerId: 'test' } };
onePlusXSubmodule.getBidRequestData(reqBidsConfigObj, callbackSpy, config);
setTimeout(() => {
expect(callbackSpy.calledOnce).to.be.true

}, 100)
}
// No customer id in config => error but still callback called
{
const callbackSpy = sinon.spy();
const config = {}
onePlusXSubmodule.getBidRequestData(reqBidsConfigObj, callbackSpy, config);
setTimeout(() => {
expect(callbackSpy.calledOnce).to.be.true

}, 100);
}
})
})
})

0 comments on commit d07ef85

Please sign in to comment.