Skip to content

Commit

Permalink
prebidServerBidAdapter cleanup (prebid#2844)
Browse files Browse the repository at this point in the history
* prebidServerBidAdapter cleanup

* move module

* wip

* remove commented code
  • Loading branch information
jaiminpanchal27 authored and mkendall07 committed Aug 7, 2018
1 parent 752e234 commit 4682ae3
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 136 deletions.
24 changes: 24 additions & 0 deletions modules/appnexusBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ export const spec = {
url: '//acdn.adnxs.com/ib/static/usersync/v3/async_usersync.html'
}];
}
},

transformBidParams: function(params, isOpenRtb) {
params = utils.convertTypes({
'member': 'string',
'invCode': 'string',
'placementId': 'number',
'keywords': utils.transformBidderParamKeywords
}, params);

if (isOpenRtb) {
params.use_pmt_rule = (typeof params.usePaymentRule === 'boolean') ? params.usePaymentRule : false;
if (params.usePaymentRule) { delete params.usePaymentRule; }

Object.keys(params).forEach(paramKey => {
let convertedKey = utils.convertCamelToUnderscore(paramKey);
if (convertedKey !== paramKey) {
params[convertedKey] = params[paramKey];
delete params[paramKey];
}
});
}

return params;
}
}

Expand Down
17 changes: 15 additions & 2 deletions modules/audienceNetworkBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { registerBidder } from 'src/adapters/bidderFactory';
import { config } from 'src/config';
import { formatQS } from 'src/url';
import { generateUUID, getTopWindowUrl, isSafariBrowser } from 'src/utils';
import { generateUUID, getTopWindowUrl, isSafariBrowser, convertTypes } from 'src/utils';
import findIndex from 'core-js/library/fn/array/find-index';
import includes from 'core-js/library/fn/array/includes';

Expand Down Expand Up @@ -242,12 +242,25 @@ const interpretResponse = ({ body }, { adformats, requestIds, sizes }) => {
});
};

/**
* Covert bid param types for S2S
* @param {Object} params bid params
* @param {Boolean} isOpenRtb boolean to check openrtb2 protocol
* @return {Object} params bid params
*/
const transformBidParams = (params, isOpenRtb) => {
return convertTypes({
'placementId': 'string'
}, params);
}

export const spec = {
code,
supportedMediaTypes,
isBidRequestValid,
buildRequests,
interpretResponse
interpretResponse,
transformBidParams
};

registerBidder(spec);
14 changes: 14 additions & 0 deletions modules/conversantBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ export const spec = {
}

return bidResponses;
},

/**
* Covert bid param types for S2S
* @param {Object} params bid params
* @param {Boolean} isOpenRtb boolean to check openrtb2 protocol
* @return {Object} params bid params
*/
transformBidParams: function(params, isOpenRtb) {
return utils.convertTypes({
'site_id': 'string',
'secure': 'number',
'mobile': 'number'
}, params);
}
};

Expand Down
12 changes: 12 additions & 0 deletions modules/ixBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ export const spec = {
}

return bids;
},

/**
* Covert bid param types for S2S
* @param {Object} params bid params
* @param {Boolean} isOpenRtb boolean to check openrtb2 protocol
* @return {Object} params bid params
*/
transformBidParams: function(params, isOpenRtb) {
return utils.convertTypes({
'siteID': 'number'
}, params);
}
};

Expand Down
6 changes: 6 additions & 0 deletions modules/openxBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export const spec = {
url: url
}];
}
},
transformBidParams: function(params, isOpenRtb) {
return utils.convertTypes({
'unit': 'string',
'customFloor': 'number'
}, params);
}
};

Expand Down
19 changes: 19 additions & 0 deletions modules/prebidServerBidAdapter/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// accountId and bidders params are not included here, should be configured by end-user
export const S2S_VENDORS = {
'appnexus': {
adapter: 'prebidServer',
cookieSet: false,
enabled: true,
endpoint: '//prebid.adnxs.com/pbs/v1/openrtb2/auction',
syncEndpoint: '//prebid.adnxs.com/pbs/v1/cookie_sync',
timeout: 1000
},
'rubicon': {
adapter: 'prebidServer',
cookieSet: false,
enabled: true,
endpoint: '//prebid-server.rubiconproject.com/auction',
syncEndpoint: '//prebid-server.rubiconproject.com/cookie_sync',
timeout: 500
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { VIDEO } from 'src/mediaTypes';
import { isValid } from 'src/adapters/bidderFactory';
import events from 'src/events';
import includes from 'core-js/library/fn/array/includes';
import { S2S_VENDORS } from './config.js';

const getConfig = config.getConfig;

Expand All @@ -32,26 +33,6 @@ config.setDefaults({
's2sConfig': s2sDefaultConfig
});

// accountId and bidders params are not included here, should be configured by end-user
const availVendorDefaults = {
'appnexus': {
adapter: 'prebidServer',
cookieSet: false,
enabled: true,
endpoint: '//prebid.adnxs.com/pbs/v1/openrtb2/auction',
syncEndpoint: '//prebid.adnxs.com/pbs/v1/cookie_sync',
timeout: 1000
},
'rubicon': {
adapter: 'prebidServer',
cookieSet: false,
enabled: true,
endpoint: '//prebid-server.rubiconproject.com/auction',
syncEndpoint: '//prebid-server.rubiconproject.com/cookie_sync',
timeout: 500
}
};

/**
* Set config for server to server header bidding
* @typedef {Object} options - required
Expand All @@ -69,13 +50,12 @@ function setS2sConfig(options) {
if (options.defaultVendor) {
let vendor = options.defaultVendor;
let optionKeys = Object.keys(options);

if (availVendorDefaults.hasOwnProperty(vendor)) {
if (S2S_VENDORS[vendor]) {
// vendor keys will be set if either: the key was not specified by user
// or if the user did not set their own distinct value (ie using the system default) to override the vendor
Object.keys(availVendorDefaults[vendor]).forEach(function(vendorKey) {
Object.keys(S2S_VENDORS[vendor]).forEach((vendorKey) => {
if (s2sDefaultConfig[vendorKey] === options[vendorKey] || !includes(optionKeys, vendorKey)) {
options[vendorKey] = availVendorDefaults[vendor][vendorKey];
options[vendorKey] = S2S_VENDORS[vendor][vendorKey];
}
});
} else {
Expand Down Expand Up @@ -184,93 +164,6 @@ function doClientSideSyncs(bidders) {
});
}

/**
* Try to convert a value to a type.
* If it can't be done, the value will be returned.
*
* @param {string} typeToConvert The target type. e.g. "string", "number", etc.
* @param {*} value The value to be converted into typeToConvert.
*/
function tryConvertType(typeToConvert, value) {
if (typeToConvert === 'string') {
return value && value.toString();
} else if (typeToConvert === 'number') {
return Number(value);
} else {
return value;
}
}

const tryConvertString = tryConvertType.bind(null, 'string');
const tryConvertNumber = tryConvertType.bind(null, 'number');

const paramTypes = {
'appnexus': {
'member': tryConvertString,
'invCode': tryConvertString,
'placementId': tryConvertNumber,
'keywords': utils.transformBidderParamKeywords
},
'rubicon': {
'accountId': tryConvertNumber,
'siteId': tryConvertNumber,
'zoneId': tryConvertNumber
},
'indexExchange': {
'siteID': tryConvertNumber
},
'audienceNetwork': {
'placementId': tryConvertString
},
'pubmatic': {
'publisherId': tryConvertString,
'adSlot': tryConvertString
},
'districtm': {
'member': tryConvertString,
'invCode': tryConvertString,
'placementId': tryConvertNumber
},
'pulsepoint': {
'cf': tryConvertString,
'cp': tryConvertNumber,
'ct': tryConvertNumber
},
'conversant': {
'site_id': tryConvertString,
'secure': tryConvertNumber,
'mobile': tryConvertNumber
},
'openx': {
'unit': tryConvertString,
'customFloor': tryConvertNumber
},
};

/*
* Modify an adunit's bidder parameters to match the expected parameter types
*/
function convertTypes(adUnits) {
adUnits.forEach(adUnit => {
adUnit.bids.forEach(bid => {
// aliases use the base bidder's paramTypes
const bidder = adaptermanager.aliasRegistry[bid.bidder] || bid.bidder;
const types = paramTypes[bidder] || [];

Object.keys(types).forEach(key => {
if (bid.params[key]) {
bid.params[key] = types[key](bid.params[key]);

// don't send invalid values
if (isNaN(bid.params[key])) {
delete bid.params.key;
}
}
});
});
});
}

function _getDigiTrustQueryParams() {
function getDigiTrustId() {
let digiTrustUser = window.DigiTrust && (config.getConfig('digiTrustId') || window.DigiTrust.getUser({member: 'T9QSFKPDN9'}));
Expand Down Expand Up @@ -327,6 +220,15 @@ function transformHeightWidth(adUnit) {
const LEGACY_PROTOCOL = {

buildRequest(s2sBidRequest, bidRequests, adUnits) {
adUnits.forEach(adUnit => {
adUnit.bids.forEach(bid => {
const adapter = adaptermanager.bidderRegistry[bid.bidder];
if (adapter && adapter.getSpec().transformBidParams) {
bid.params = adapter.getSpec().transformBidParams(bid.params, isOpenRtb());
}
});
});

// pbs expects an ad_unit.video attribute if the imp is video
adUnits.forEach(adUnit => {
adUnit.sizes = transformHeightWidth(adUnit);
Expand Down Expand Up @@ -504,19 +406,9 @@ const OPEN_RTB_PROTOCOL = {

// get bidder params in form { <bidder code>: {...params} }
const ext = adUnit.bids.reduce((acc, bid) => {
// TODO: move this bidder specific out to a more ideal location (submodule?); https://github.com/prebid/Prebid.js/issues/2420
// convert all AppNexus keys to underscore format for pbs
if (bid.bidder === 'appnexus') {
bid.params.use_pmt_rule = (typeof bid.params.usePaymentRule === 'boolean') ? bid.params.usePaymentRule : false;
if (bid.params.usePaymentRule) { delete bid.params.usePaymentRule; }

Object.keys(bid.params).forEach(paramKey => {
let convertedKey = utils.convertCamelToUnderscore(paramKey);
if (convertedKey !== paramKey) {
bid.params[convertedKey] = bid.params[paramKey];
delete bid.params[paramKey];
}
});
const adapter = adaptermanager.bidderRegistry[bid.bidder];
if (adapter && adapter.getSpec().transformBidParams) {
bid.params = adapter.getSpec().transformBidParams(bid.params, isOpenRtb());
}
acc[bid.bidder] = bid.params;
return acc;
Expand Down Expand Up @@ -643,6 +535,13 @@ const OPEN_RTB_PROTOCOL = {
}
};

const isOpenRtb = () => {
const OPEN_RTB_PATH = '/openrtb2/';

const endpoint = (_s2sConfig && _s2sConfig.endpoint) || '';
return ~endpoint.indexOf(OPEN_RTB_PATH);
}

/*
* Returns the required protocol adapter to communicate with the configured
* endpoint. The adapter is an object containing `buildRequest` and
Expand All @@ -656,12 +555,7 @@ const OPEN_RTB_PROTOCOL = {
* const bids = protocol().interpretResponse(response, bidRequests, requestedBidders);
*/
const protocolAdapter = () => {
const OPEN_RTB_PATH = '/openrtb2/';

const endpoint = (_s2sConfig && _s2sConfig.endpoint) || '';
const isOpenRtb = ~endpoint.indexOf(OPEN_RTB_PATH);

return isOpenRtb ? OPEN_RTB_PROTOCOL : LEGACY_PROTOCOL;
return isOpenRtb() ? OPEN_RTB_PROTOCOL : LEGACY_PROTOCOL;
};

/**
Expand All @@ -674,8 +568,6 @@ export function PrebidServer() {
baseAdapter.callBids = function(s2sBidRequest, bidRequests, addBidResponse, done, ajax) {
const adUnits = utils.deepClone(s2sBidRequest.ad_units);

convertTypes(adUnits);

// at this point ad units should have a size array either directly or mapped so filter for that
const adUnitsWithSizes = adUnits.filter(unit => unit.sizes && unit.sizes.length);

Expand Down
13 changes: 13 additions & 0 deletions modules/pubmaticBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,19 @@ export const spec = {
} else {
utils.logWarn('PubMatic: Please enable iframe based user sync.');
}
},

/**
* Covert bid param types for S2S
* @param {Object} params bid params
* @param {Boolean} isOpenRtb boolean to check openrtb2 protocol
* @return {Object} params bid params
*/
transformBidParams: function(params, isOpenRtb) {
return utils.convertTypes({
'publisherId': 'string',
'adSlot': 'string'
}, params);
}
};

Expand Down
8 changes: 7 additions & 1 deletion modules/pulsepointBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ export const spec = {
url: '//bh.contextweb.com/visitormatch/prebid'
}];
}
},
transformBidParams: function(params, isOpenRtb) {
return utils.convertTypes({
'cf': 'string',
'cp': 'number',
'ct': 'number'
}, params);
}

};

/**
Expand Down
Loading

0 comments on commit 4682ae3

Please sign in to comment.