-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MobfoxBidAdapter compliant to prebid1.0 #1757
Changes from 7 commits
e4a6df4
777ce2e
b7829fe
35e8afd
b1d0929
9772f66
3971ab5
1d1830f
12edab1
6b780a2
b1b14db
fe2fe07
d702efd
805eda6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,24 @@ | ||
const bidfactory = require('src/bidfactory.js'); | ||
const bidmanager = require('src/bidmanager.js'); | ||
const ajax = require('src/ajax.js'); | ||
const CONSTANTS = require('src/constants.json'); | ||
import {registerBidder} from 'src/adapters/bidderFactory'; | ||
|
||
const utils = require('src/utils.js'); | ||
const adaptermanager = require('src/adaptermanager'); | ||
|
||
function MobfoxAdapter() { | ||
const BIDDER_CODE = 'mobfox'; | ||
const BID_REQUEST_BASE_URL = 'https://my.mobfox.com/request.php'; | ||
|
||
// request | ||
function buildQueryStringFromParams(params) { | ||
for (let key in params) { | ||
if (params.hasOwnProperty(key)) { | ||
if (params[key] === undefined) { | ||
delete params[key]; | ||
} else { | ||
params[key] = encodeURIComponent(params[key]); | ||
} | ||
} | ||
const BIDDER_CODE = 'mobfox'; | ||
const BID_REQUEST_BASE_URL = 'https://my.mobfox.com/request.php'; | ||
|
||
export const spec = { | ||
code: BIDDER_CODE, | ||
aliases: ['mf'], // short code | ||
isBidRequestValid: function (bid) { | ||
return bid.params.s !== null && bid.params.s !== undefined && bid.requestId !== null && bid.requestId !== undefined; | ||
}, | ||
buildRequests: function (validBidRequests) { | ||
if (validBidRequests.length > 1) { | ||
throw ('invalid number of valid bid requests, expected 1 element') | ||
} | ||
|
||
return utils._map(Object.keys(params), key => `${key}=${params[key]}`) | ||
.join('&'); | ||
} | ||
|
||
function buildBidRequest(bid) { | ||
let bidParams = bid.params; | ||
let bidParams = validBidRequests[0].params; | ||
let bid = validBidRequests[0]; | ||
|
||
let requestParams = { | ||
let params = { | ||
// -------------------- Mandatory Parameters ------------------ | ||
rt: bidParams.rt || 'api-fetchip', | ||
r_type: bidParams.r_type || 'banner', | ||
|
@@ -80,103 +70,64 @@ function MobfoxAdapter() { | |
n_rating_req: bidParams.n_rating_req || undefined | ||
}; | ||
|
||
return requestParams; | ||
} | ||
let payloadString = buildPayloadString(params); | ||
|
||
function sendBidRequest(bid) { | ||
let requestParams = buildBidRequest(bid); | ||
let queryString = buildQueryStringFromParams(requestParams); | ||
|
||
ajax.ajax(`${BID_REQUEST_BASE_URL}?${queryString}`, { | ||
success(resp, xhr) { | ||
if (xhr.getResponseHeader('Content-Type') == 'application/json') { | ||
try { | ||
resp = JSON.parse(resp) | ||
} catch (e) { | ||
resp = {error: resp} | ||
} | ||
} | ||
onBidResponse({ | ||
data: resp, | ||
xhr: xhr | ||
}, bid); | ||
}, | ||
error(err) { | ||
if (xhr.getResponseHeader('Content-Type') == 'application/json') { | ||
try { | ||
err = JSON.parse(err); | ||
} catch (e) { | ||
} | ||
; | ||
} | ||
onBidResponseError(bid, [err]); | ||
return { | ||
method: 'GET', | ||
url: BID_REQUEST_BASE_URL, | ||
data: payloadString, | ||
requestId: bid.bidId | ||
}; | ||
}, | ||
interpretResponse: function (serverResponse, bidRequest) { | ||
const bidResponses = []; | ||
|
||
if (!serverResponse || serverResponse.error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its LGTM except this one change. |
||
let errorMessage = `in response for ${bidRequest.bidderCode} adapter`; | ||
if (serverResponse && serverResponse.error) { | ||
errorMessage += `: ${serverResponse.error}`; | ||
} | ||
}); | ||
} | ||
|
||
// response | ||
function onBidResponseError(bid, err) { | ||
utils.logError('Bid Response Error', bid, ...err); | ||
let bidResponse = bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bid); | ||
bidResponse.bidderCode = BIDDER_CODE; | ||
bidmanager.addBidResponse(bid.placementCode, bidResponse); | ||
} | ||
|
||
function onBidResponse(bidderResponse, bid) { | ||
// transform the response to a valid prebid response | ||
try { | ||
let bidResponse = transformResponse(bidderResponse, bid); | ||
bidmanager.addBidResponse(bid.placementCode, bidResponse); | ||
} catch (e) { | ||
onBidResponseError(bid, [e]); | ||
} | ||
} | ||
|
||
function transformResponse(bidderResponse, bid) { | ||
let responseBody = bidderResponse.data; | ||
|
||
// Validate Request | ||
let err = responseBody.error; | ||
if (err) { | ||
throw err; | ||
utils.logError(errorMessage); | ||
return bidResponses; | ||
} | ||
|
||
let htmlString = responseBody.request && responseBody.request.htmlString; | ||
if (!htmlString) { | ||
throw [`htmlString is missing`, responseBody]; | ||
} | ||
|
||
let cpm; | ||
const cpmHeader = bidderResponse.xhr.getResponseHeader('X-Pricing-CPM'); | ||
try { | ||
cpm = Number(cpmHeader); | ||
let bidRequestData = bidRequest.data.split('&'); | ||
const bidResponse = { | ||
requestId: bidRequest.requestId, | ||
bidderCode: BIDDER_CODE, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
cpm: serverResponse.request.cpmPrice, | ||
width: bidRequestData[5].split('=')[1], | ||
height: bidRequestData[6].split('=')[1], | ||
creativeId: bidRequestData[3].split('=')[1], | ||
currency: 'USD', | ||
netRevenue: true, | ||
ttl: 360, | ||
referrer: serverResponse.request.clickurl, | ||
ad: serverResponse.request.htmlString | ||
}; | ||
bidResponses.push(bidResponse); | ||
} catch (e) { | ||
throw ['Invalid CPM value:', cpmHeader]; | ||
throw 'could not build bid response: ' + e; | ||
} | ||
|
||
// Validations passed - Got bid | ||
let bidResponse = bidfactory.createBid(CONSTANTS.STATUS.GOOD, bid); | ||
bidResponse.bidderCode = BIDDER_CODE; | ||
|
||
bidResponse.ad = htmlString; | ||
bidResponse.cpm = cpm; | ||
|
||
bidResponse.width = bid.sizes[0][0]; | ||
bidResponse.height = bid.sizes[0][1]; | ||
|
||
return bidResponse; | ||
return bidResponses; | ||
}, | ||
getUserSyncs: function (syncOptions) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
// prebid api | ||
function callBids(params) { | ||
let bids = params.bids || []; | ||
bids.forEach(sendBidRequest); | ||
}; | ||
|
||
function buildPayloadString(params) { | ||
for (let key in params) { | ||
if (params.hasOwnProperty(key)) { | ||
if (params[key] === undefined) { | ||
delete params[key]; | ||
} else { | ||
params[key] = encodeURIComponent(params[key]); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
callBids: callBids | ||
}; | ||
return utils._map(Object.keys(params), key => `${key}=${params[key]}`) | ||
.join('&') | ||
} | ||
|
||
adaptermanager.registerBidAdapter(new MobfoxAdapter(), 'mobfox'); | ||
module.exports = MobfoxAdapter; | ||
registerBidder(spec); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1742 got merged recently.
The first argument to interpretResponse now looks like this:
You'll have to pull master and update the spec so that it looks digs into that object as well now.