Skip to content

Commit

Permalink
Refactor craftBidAdapter (prebid#12517)
Browse files Browse the repository at this point in the history
  • Loading branch information
crumbjp authored Dec 2, 2024
1 parent cafb7f7 commit 0b8d774
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
22 changes: 22 additions & 0 deletions libraries/interpretResponseUtils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {logError} from '../../src/utils.js';

export function interpretResponseUtil(serverResponse, {bidderRequest}, eachBidCallback) {
const bids = [];
if (!serverResponse.body || serverResponse.body.error) {
let errorMessage = `in response for ${bidderRequest.bidderCode} adapter`;
if (serverResponse.body && serverResponse.body.error) { errorMessage += `: ${serverResponse.body.error}`; }
logError(errorMessage);
return bids;
}
(serverResponse.body.tags || []).forEach(serverBid => {
try {
const bid = eachBidCallback(serverBid);
if (bid) {
bids.push(bid);
}
} catch (e) {
// Do nothing
}
});
return bids;
}
34 changes: 9 additions & 25 deletions modules/craftBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getBidRequest, logError} from '../src/utils.js';
import {getBidRequest} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import {find, includes} from '../src/polyfill.js';
Expand All @@ -7,6 +7,7 @@ import {ajax} from '../src/ajax.js';
import {hasPurpose1Consent} from '../src/utils/gdpr.js';
import {convertOrtbRequestToProprietaryNative} from '../src/native.js';
import {getANKeywordParam} from '../libraries/appnexusUtils/anKeywords.js';
import {interpretResponseUtil} from '../libraries/interpretResponseUtils/index.js';

const BIDDER_CODE = 'craft';
const URL_BASE = 'https://gacraft.jp/prebid-v3';
Expand Down Expand Up @@ -68,31 +69,14 @@ export const spec = {

interpretResponse: function(serverResponse, {bidderRequest}) {
try {
serverResponse = serverResponse.body;
const bids = [];
if (!serverResponse) {
return [];
}
if (serverResponse.error) {
let errorMessage = `in response for ${bidderRequest.bidderCode} adapter`;
if (serverResponse.error) {
errorMessage += `: ${serverResponse.error}`;
const bids = interpretResponseUtil(serverResponse, {bidderRequest}, serverBid => {
const rtbBid = getRtbBid(serverBid);
if (rtbBid && rtbBid.cpm !== 0 && includes(this.supportedMediaTypes, rtbBid.ad_type)) {
const bid = newBid(serverBid, rtbBid, bidderRequest);
bid.mediaType = parseMediaType(rtbBid);
return bid;
}
logError(errorMessage);
return bids;
}
if (serverResponse.tags) {
serverResponse.tags.forEach(serverBid => {
const rtbBid = getRtbBid(serverBid);
if (rtbBid) {
if (rtbBid.cpm !== 0 && includes(this.supportedMediaTypes, rtbBid.ad_type)) {
const bid = newBid(serverBid, rtbBid, bidderRequest);
bid.mediaType = parseMediaType(rtbBid);
bids.push(bid);
}
}
});
}
});
return bids;
} catch (e) {
return [];
Expand Down

0 comments on commit 0b8d774

Please sign in to comment.