Skip to content

Comments

ResetDigital: Switch to OpenRTB#4385

Open
przemkaczmarek wants to merge 1 commit intomasterfrom
ResetDigital-Switch-to-PenRTB
Open

ResetDigital: Switch to OpenRTB#4385
przemkaczmarek wants to merge 1 commit intomasterfrom
ResetDigital-Switch-to-PenRTB

Conversation

@przemkaczmarek
Copy link
Collaborator

🔧 Type of changes

  • bid adapter update

✨ What's the context?

#4340

@osulzhenko osulzhenko linked an issue Feb 13, 2026 that may be closed by this pull request
Comment on lines +71 to 84
private ExtImpResetDigital parseImpExt(Imp imp) {
try {
final BigDecimal convertedPrice = currencyConversionService
.convertCurrency(bidFloorPrice.getValue(), bidRequest, bidFloorCur, DEFAULT_CURRENCY);
final ExtPrebid<?, ExtImpResetDigital> extPrebid = mapper.mapper()
.convertValue(imp.getExt(), EXT_TYPE_REFERENCE);

return Price.of(DEFAULT_CURRENCY, convertedPrice);
} catch (PreBidException e) {
throw new PreBidException(
"Unable to convert provided bid floor currency from %s to %s for imp `%s`"
.formatted(bidFloorCur, DEFAULT_CURRENCY, impId));
}
}
if (extPrebid == null || extPrebid.getBidder() == null) {
throw new PreBidException("imp.ext.bidder is required");
}

private static void populateBannerImps(List<Imp> bannerImps, Price bidFloorPrice, Imp imp) {
if (imp.getBanner() != null) {
final Imp bannerImp = imp.toBuilder().video(null).xNative(null).audio(null).build();
bannerImps.add(modifyImp(bannerImp, bidFloorPrice));
return extPrebid.getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException("Error parsing resetDigitalExt from imp.ext: " + e.getMessage());
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    private ExtImpResetDigital parseImpExt(Imp imp) {
        try {
            return mapper.mapper().convertValue(imp.getExt(), EXT_TYPE_REFERENCE).getBidder();
        } catch (IllegalArgumentException e) {
            throw new PreBidException("Error parsing resetDigitalExt from imp.ext: " + e.getMessage());
        }
    }

populateBannerImps(bannerImps, bidFloorPrice, imp);
populateVideoImps(videoImps, bidFloorPrice, imp);
populateAudiImps(audioImps, bidFloorPrice, imp);
if (CollectionUtils.isEmpty(request.getImp()) || request.getImp().size() != 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getImp() can't be empty

Comment on lines +86 to +93
private static Imp modifyImp(Imp imp, ExtImpResetDigital extImp) {
final Imp.ImpBuilder impBuilder = imp.toBuilder();

private static void populateAudiImps(List<Imp> audioImps, Price bidFloorPrice, Imp imp) {
if (imp.getAudio() != null) {
final Imp audioImp = imp.toBuilder().banner(null).xNative(null).video(null).build();
audioImps.add(modifyImp(audioImp, bidFloorPrice));
if (StringUtils.isBlank(imp.getTagid())) {
impBuilder.tagid(extImp.getPlacementId());
}

return impBuilder.build();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    private static Imp modifyImp(Imp imp, ExtImpResetDigital extImp) {
        return StringUtils.isBlank(imp.getTagid())
                ? imp.toBuilder().tagid(extImp.getPlacementId()).build()
                : imp;
    }

: initialBidFloorPrice;
final String uri = endpointUrl + "?pid=" + HttpUtil.encodeUrl(extImp.getPlacementId());

return Result.withValue(BidderUtil.defaultRequest(outgoingRequest, uri, mapper));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "X-OpenRTB-Version" header

Comment on lines +106 to +175
private static Result<List<BidderBid>> extractBids(BidResponse bidResponse, BidRequest bidRequest) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
if (bidResponse.getCur() != null && !StringUtils.equalsIgnoreCase(DEFAULT_CURRENCY, bidResponse.getCur())) {
throw new PreBidException("Bidder support only USD currency");
return Result.withValues(Collections.emptyList());
}
return bidsFromResponse(bidResponse, bidRequest);
}

private static List<BidderBid> bidsFromResponse(BidResponse bidResponse, BidRequest bidRequest) {
return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(bid, getBidType(bid, bidRequest.getImp()), DEFAULT_CURRENCY))
.toList();
final String currency = StringUtils.isNotBlank(bidResponse.getCur())
? bidResponse.getCur()
: DEFAULT_CURRENCY;

return bidsFromResponse(bidResponse, bidRequest, currency);
}

private static BidType getBidType(Bid bid, List<Imp> imps) {
final String impId = bid.getImpid();
for (Imp imp : imps) {
if (imp.getId().equals(impId)) {
if (imp.getBanner() != null) {
return BidType.banner;
} else if (imp.getVideo() != null) {
return BidType.video;
} else if (imp.getAudio() != null) {
return BidType.audio;
private static Result<List<BidderBid>> bidsFromResponse(BidResponse bidResponse,
BidRequest bidRequest,
String currency) {
final List<BidderBid> bids = new ArrayList<>();
final List<BidderError> errors = new ArrayList<>();

for (SeatBid seatBid : bidResponse.getSeatbid()) {
if (seatBid == null || seatBid.getBid() == null) {
continue;
}
for (Bid bid : seatBid.getBid()) {
if (!BidderUtil.isValidPrice(bid.getPrice())) {
errors.add(BidderError.badServerResponse(
"price %s <= 0 filtered out".formatted(bid.getPrice())));
continue;
}

try {
final BidType bidType = getBidType(bid, bidRequest);
bids.add(BidderBid.of(bid, bidType, currency));
} catch (PreBidException e) {
errors.add(BidderError.badServerResponse(e.getMessage()));
}
}
}
throw new PreBidException("Failed to find banner/video/audio impression " + impId);

return Result.of(bids, errors);
}

private static BidType getBidType(Bid bid, BidRequest bidRequest) {
final Integer mtype = bid.getMtype();
if (mtype != null) {
return switch (mtype) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
case 3 -> BidType.audio;
case 4 -> BidType.xNative;
default -> throw new PreBidException("Unsupported MType: " + mtype);
};
}

final Imp imp = bidRequest.getImp().getFirst();
if (!imp.getId().equals(bid.getImpid())) {
throw new PreBidException("No matching impression found for ImpID: " + bid.getImpid());
}

return getMediaType(imp);
}

private static BidType getMediaType(Imp imp) {
if (imp.getVideo() != null) {
return BidType.video;
} else if (imp.getAudio() != null) {
return BidType.audio;
} else if (imp.getXNative() != null) {
return BidType.xNative;
}
return BidType.banner;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    @Override
    public final Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) {
        try {
            final List<BidderError> errors = new ArrayList<>();
            final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
            return Result.of(extractBids(bidResponse, httpCall.getRequest().getPayload(), errors), errors);
        } catch (DecodeException | PreBidException e) {
            return Result.withError(BidderError.badServerResponse(e.getMessage()));
        }
    }

    private static List<BidderBid> extractBids(BidResponse bidResponse,
                                               BidRequest bidRequest,
                                               List<BidderError> errors) {

        if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
            return Collections.emptyList();
        }

        final Imp imp = bidRequest.getImp().getFirst();
        final String currency = StringUtils.isNotBlank(bidResponse.getCur())
                ? bidResponse.getCur()
                : bidRequest.getCur().stream().findFirst().orElse(DEFAULT_CURRENCY);

        final List<BidderBid> bidderBids = new ArrayList<>();
        for (SeatBid seatBid : bidResponse.getSeatbid()) {
            if (seatBid == null || CollectionUtils.isEmpty(seatBid.getBid())) {
                continue;
            }

            for (Bid bid : seatBid.getBid()) {
                try {
                    bidderBids.add(makeBidderBid(bid, seatBid.getSeat(), currency, imp));
                } catch (PreBidException e) {
                    errors.add(BidderError.badServerResponse(e.getMessage()));
                }
            }
        }

        return bidderBids;
    }

    private static BidderBid makeBidderBid(Bid bid, String seat, String currency, Imp imp) {
        if (!BidderUtil.isValidPrice(bid.getPrice())) {
            throw new PreBidException("price %s <= 0 filtered out".formatted(bid.getPrice()));
        }

        final BidType bidType = Optional.ofNullable(getBidType(bid))
                .orElseGet(() -> getBidType(bid, imp));

        return StringUtils.isNotBlank(seat)
                ? BidderBid.of(bid, bidType, seat, currency)
                : BidderBid.of(bid, bidType, currency);
    }

    private static BidType getBidType(Bid bid) {
        final Integer mtype = bid.getMtype();
        return switch (mtype) {
            case 1 -> BidType.banner;
            case 2 -> BidType.video;
            case 3 -> BidType.audio;
            case 4 -> BidType.xNative;
            case null -> null;
            default -> throw new PreBidException("Unsupported MType: " + mtype);
        };
    }

    private static BidType getBidType(Bid bid, Imp imp) {
        if (!imp.getId().equals(bid.getImpid())) {
            throw new PreBidException("No matching impression found for ImpID: " + bid.getImpid());
        }

        if (imp.getVideo() != null) {
            return BidType.video;
        } else if (imp.getAudio() != null) {
            return BidType.audio;
        } else if (imp.getXNative() != null) {
            return BidType.xNative;
        }
        return BidType.banner;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Port PR from PBS-Go: ResetDigital: Switch to OpenRTB

3 participants