diff --git a/modules/1plusXRtdProvider.js b/modules/1plusXRtdProvider.js index dc833c46a37..01b66d08795 100644 --- a/modules/1plusXRtdProvider.js +++ b/modules/1plusXRtdProvider.js @@ -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 @@ -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 diff --git a/test/spec/modules/1plusXRtdProvider_spec.js b/test/spec/modules/1plusXRtdProvider_spec.js index 16b711b9523..9b6eb917a80 100644 --- a/test/spec/modules/1plusXRtdProvider_spec.js +++ b/test/spec/modules/1plusXRtdProvider_spec.js @@ -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); + } + }) }) })