-
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e4a6df4
Upgrade MobfoxAdapter to be compliant with Prebid 1.0 - missing cpm
hadarpeer 777ce2e
Merge branch 'master' of https://github.com/prebid/Prebid.js
hadarpeer b7829fe
MfBidAdapter changes for preBid1.0
hadarpeer 35e8afd
Upgrade MobfoxAdapter to be compliant with Prebid 1.0 - final version
hadarpeer b1d0929
Merge branch 'master' of https://github.com/prebid/Prebid.js
hadarpeer 9772f66
removed accidentally added files
hadarpeer 3971ab5
Merge branch 'master' of https://github.com/prebid/Prebid.js
hadarpeer 1d1830f
Merge branch 'master' of https://github.com/prebid/Prebid.js
hadarpeer 12edab1
Update MobfoxBidAdapter after review
hadarpeer 6b780a2
Update MobfoxBidAdapter after review - Final
hadarpeer b1b14db
Add markdown file containing key information about Mobfox adapter
hadarpeer fe2fe07
Merge branch 'master' of https://github.com/prebid/Prebid.js
hadarpeer d702efd
Merge branch 'master' of https://github.com/prebid/Prebid.js
hadarpeer 805eda6
Fix error field - under request in serverResponse
hadarpeer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,25 @@ | ||
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'; | ||
const CPM_HEADER = 'X-Pricing-CPM'; | ||
|
||
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 +71,63 @@ 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 serverResponseBody = serverResponse.body; | ||
let serverResponseHeaders = serverResponse.headers; | ||
let bidRequestData = bidRequest.data.split('&'); | ||
const bidResponse = { | ||
requestId: bidRequest.requestId, | ||
cpm: serverResponseHeaders.get(CPM_HEADER), | ||
width: bidRequestData[5].split('=')[1], | ||
height: bidRequestData[6].split('=')[1], | ||
creativeId: bidRequestData[3].split('=')[1], | ||
currency: 'USD', | ||
netRevenue: true, | ||
ttl: 360, | ||
referrer: serverResponseBody.request.clickurl, | ||
ad: serverResponseBody.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; | ||
} | ||
|
||
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Overview | ||
|
||
``` | ||
Module Name: Mobfox Bidder Adapter | ||
Module Type: Bidder Adapter | ||
Maintainer: solutions-team@matomy.com | ||
``` | ||
|
||
# Description | ||
|
||
Module that connects to Mobfox's demand sources | ||
|
||
# Test Parameters | ||
``` | ||
var adUnits = [{ | ||
code: 'div-gpt-ad-1460505748561-0', | ||
sizes: [[320, 480], [300, 250], [300,600]], | ||
|
||
// Replace this object to test a new Adapter! | ||
bids: [{ | ||
bidder: 'mobfox', | ||
params: { | ||
s: "267d72ac3f77a3f447b32cf7ebf20673", // required - The hash of your inventory to identify which app is making the request, | ||
imp_instl: 1 // optional - set to 1 if using interstitial otherwise delete or set to 0 | ||
} | ||
}] | ||
|
||
}]; | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.