Skip to content
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

Biddo invamia bid adapters: import common code #12409

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions libraries/biddoInvamiaUtils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Helper function to build request payload for banner ads.
* @param {Object} bidRequest - The bid request object.
* @param {string} endpointUrl - The endpoint URL specific to the bidder.
* @returns {Array} An array of server requests.
*/
export function buildBannerRequests(bidRequest, endpointUrl) {
const serverRequests = [];
const sizes = bidRequest.mediaTypes.banner.sizes;

sizes.forEach(([width, height]) => {
bidRequest.params.requestedSizes = [width, height];

const payload = {
ctype: 'div',
pzoneid: bidRequest.params.zoneId,
width,
height,
};

const payloadString = Object.keys(payload)
.map((key) => `${key}=${encodeURIComponent(payload[key])}`)
.join('&');

serverRequests.push({
method: 'GET',
url: endpointUrl,
data: payloadString,
bidderRequest: bidRequest,
});
});

return serverRequests;
}

/**
* Helper function to interpret server response for banner ads.
* @param {Object} serverResponse - The server response object.
* @param {Object} bidderRequest - The matched bid request for this response.
* @returns {Array} An array of bid responses.
*/
export function interpretBannerResponse(serverResponse, bidderRequest) {
const response = serverResponse.body;
const bidResponses = [];

if (response && response.template && response.template.html) {
const { bidId } = bidderRequest;
const [width, height] = bidderRequest.params.requestedSizes;

const bidResponse = {
requestId: bidId,
cpm: response.hb.cpm,
creativeId: response.banner.hash,
currency: 'USD',
netRevenue: response.hb.netRevenue,
ttl: 600,
ad: response.template.html,
mediaType: 'banner',
meta: {
advertiserDomains: response.hb.adomains || [],
},
width,
height,
};

bidResponses.push(bidResponse);
}

return bidResponses;
}
84 changes: 7 additions & 77 deletions modules/biddoBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
import { buildBannerRequests, interpretBannerResponse } from '../libraries/biddoInvamiaUtils/index.js';

/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
Expand All @@ -12,86 +13,15 @@ const ENDPOINT_URL = 'https://ad.adopx.net/delivery/impress';
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bidRequest The bid request params to validate.
* @return boolean True if this is a valid bid request, and false otherwise.
*/
isBidRequestValid: function(bidRequest) {
isBidRequestValid: function (bidRequest) {
return !!bidRequest.params.zoneId;
},
/**
* Make a server request from the list of BidRequests.
*
* @param {Array<BidRequest>} validBidRequests an array of bid requests
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(validBidRequests) {
let serverRequests = [];

validBidRequests.forEach(bidRequest => {
const sizes = bidRequest.mediaTypes.banner.sizes;

sizes.forEach(([width, height]) => {
bidRequest.params.requestedSizes = [width, height];

const payload = {
ctype: 'div',
pzoneid: bidRequest.params.zoneId,
width,
height,
};

const payloadString = Object.keys(payload).map(k => k + '=' + encodeURIComponent(payload[k])).join('&');

serverRequests.push({
method: 'GET',
url: ENDPOINT_URL,
data: payloadString,
bidderRequest: bidRequest,
});
});
});

return serverRequests;
buildRequests: function (validBidRequests) {
return validBidRequests.flatMap((bidRequest) => buildBannerRequests(bidRequest, ENDPOINT_URL));
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @param {BidRequest} bidderRequest A matched bid request for this response.
* @return Array<BidResponse> An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse, {bidderRequest}) {
const response = serverResponse.body;
const bidResponses = [];

if (response && response.template && response.template.html) {
const {bidId} = bidderRequest;
const [width, height] = bidderRequest.params.requestedSizes;

const bidResponse = {
requestId: bidId,
cpm: response.hb.cpm,
creativeId: response.banner.hash,
currency: 'USD',
netRevenue: response.hb.netRevenue,
ttl: 600,
ad: response.template.html,
mediaType: 'banner',
meta: {
advertiserDomains: response.hb.adomains || [],
},
width,
height,
};

bidResponses.push(bidResponse);
}

return bidResponses;
interpretResponse: function (serverResponse, { bidderRequest }) {
return interpretBannerResponse(serverResponse, bidderRequest);
},
}
};

registerBidder(spec);
84 changes: 7 additions & 77 deletions modules/invamiaBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
import { buildBannerRequests, interpretBannerResponse } from '../libraries/biddoInvamiaUtils/index.js';

/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
Expand All @@ -12,86 +13,15 @@ const ENDPOINT_URL = 'https://ad.invamia.com/delivery/impress';
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bidRequest The bid request params to validate.
* @return boolean True if this is a valid bid request, and false otherwise.
*/
isBidRequestValid: function(bidRequest) {
isBidRequestValid: function (bidRequest) {
return !!bidRequest.params.zoneId;
},
/**
* Make a server request from the list of BidRequests.
*
* @param {Array<BidRequest>} validBidRequests an array of bid requests
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(validBidRequests) {
let serverRequests = [];

validBidRequests.forEach(bidRequest => {
const sizes = bidRequest.mediaTypes.banner.sizes;

sizes.forEach(([width, height]) => {
bidRequest.params.requestedSizes = [width, height];

const payload = {
ctype: 'div',
pzoneid: bidRequest.params.zoneId,
width,
height,
};

const payloadString = Object.keys(payload).map(k => k + '=' + encodeURIComponent(payload[k])).join('&');

serverRequests.push({
method: 'GET',
url: ENDPOINT_URL,
data: payloadString,
bidderRequest: bidRequest,
});
});
});

return serverRequests;
buildRequests: function (validBidRequests) {
return validBidRequests.flatMap((bidRequest) => buildBannerRequests(bidRequest, ENDPOINT_URL));
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @param {BidRequest} bidderRequest A matched bid request for this response.
* @return Array<BidResponse> An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse, {bidderRequest}) {
const response = serverResponse.body;
const bidResponses = [];

if (response && response.template && response.template.html) {
const {bidId} = bidderRequest;
const [width, height] = bidderRequest.params.requestedSizes;

const bidResponse = {
requestId: bidId,
cpm: response.hb.cpm,
creativeId: response.banner.hash,
currency: 'USD',
netRevenue: response.hb.netRevenue,
ttl: 600,
ad: response.template.html,
mediaType: 'banner',
meta: {
advertiserDomains: response.hb.adomains || [],
},
width,
height,
};

bidResponses.push(bidResponse);
}

return bidResponses;
interpretResponse: function (serverResponse, { bidderRequest }) {
return interpretBannerResponse(serverResponse, bidderRequest);
},
}
};

registerBidder(spec);