-
Notifications
You must be signed in to change notification settings - Fork 226
ResetDigital: Switch to OpenRTB #4385
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,5 +1,6 @@ | ||
| package org.prebid.server.bidder.resetdigital; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.iab.openrtb.request.BidRequest; | ||
| import com.iab.openrtb.request.Imp; | ||
| import com.iab.openrtb.response.Bid; | ||
|
|
@@ -12,171 +13,165 @@ | |
| import org.prebid.server.bidder.model.BidderCall; | ||
| import org.prebid.server.bidder.model.BidderError; | ||
| import org.prebid.server.bidder.model.HttpRequest; | ||
| import org.prebid.server.bidder.model.Price; | ||
| import org.prebid.server.bidder.model.Result; | ||
| import org.prebid.server.currency.CurrencyConversionService; | ||
| import org.prebid.server.exception.PreBidException; | ||
| import org.prebid.server.json.DecodeException; | ||
| import org.prebid.server.json.JacksonMapper; | ||
| import org.prebid.server.proto.openrtb.ext.ExtPrebid; | ||
| import org.prebid.server.proto.openrtb.ext.request.resetdigital.ExtImpResetDigital; | ||
| import org.prebid.server.proto.openrtb.ext.response.BidType; | ||
| import org.prebid.server.util.BidderUtil; | ||
| import org.prebid.server.util.HttpUtil; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class ResetDigitalBidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final String DEFAULT_CURRENCY = "USD"; | ||
| private static final TypeReference<ExtPrebid<?, ExtImpResetDigital>> EXT_TYPE_REFERENCE = | ||
| new TypeReference<>() { | ||
| }; | ||
|
|
||
| private final String endpointUrl; | ||
| private final CurrencyConversionService currencyConversionService; | ||
| private final JacksonMapper mapper; | ||
|
|
||
| public ResetDigitalBidder(String endpointUrl, | ||
| CurrencyConversionService currencyConversionService, | ||
| JacksonMapper mapper) { | ||
|
|
||
| public ResetDigitalBidder(String endpointUrl, JacksonMapper mapper) { | ||
| this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); | ||
| this.currencyConversionService = Objects.requireNonNull(currencyConversionService); | ||
| this.mapper = Objects.requireNonNull(mapper); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) { | ||
| final List<Imp> bannerImps = new ArrayList<>(); | ||
| final List<Imp> videoImps = new ArrayList<>(); | ||
| final List<Imp> audioImps = new ArrayList<>(); | ||
| Price bidFloorPrice; | ||
|
|
||
| for (Imp imp : request.getImp()) { | ||
| try { | ||
| bidFloorPrice = resolveBidFloor(imp, request); | ||
| } catch (PreBidException e) { | ||
| return Result.withError(BidderError.badInput(e.getMessage())); | ||
| } | ||
| populateBannerImps(bannerImps, bidFloorPrice, imp); | ||
| populateVideoImps(videoImps, bidFloorPrice, imp); | ||
| populateAudiImps(audioImps, bidFloorPrice, imp); | ||
| if (CollectionUtils.isEmpty(request.getImp()) || request.getImp().size() != 1) { | ||
| return Result.withError(BidderError.badInput( | ||
| "ResetDigital adapter supports only one impression per request")); | ||
| } | ||
|
|
||
| return Result.withValues(getHttpRequests(request, bannerImps, videoImps, audioImps)); | ||
| } | ||
|
|
||
| private List<HttpRequest<BidRequest>> getHttpRequests(BidRequest request, | ||
| List<Imp> bannerImps, | ||
| List<Imp> videoImps, | ||
| List<Imp> audioImps) { | ||
|
|
||
| return Stream.of(bannerImps, videoImps, audioImps) | ||
| .filter(CollectionUtils::isNotEmpty) | ||
| .map(imp -> makeHttpRequest(request, imp)) | ||
| .toList(); | ||
| } | ||
|
|
||
| private HttpRequest<BidRequest> makeHttpRequest(BidRequest bidRequest, List<Imp> imp) { | ||
| final BidRequest outgoingRequest = bidRequest.toBuilder().imp(imp).build(); | ||
|
|
||
| return BidderUtil.defaultRequest(outgoingRequest, endpointUrl, mapper); | ||
| } | ||
| final Imp imp = request.getImp().getFirst(); | ||
| final ExtImpResetDigital extImp; | ||
| try { | ||
| extImp = parseImpExt(imp); | ||
| } catch (PreBidException e) { | ||
| return Result.withError(BidderError.badInput(e.getMessage())); | ||
| } | ||
|
|
||
| private static Imp modifyImp(Imp imp, Price bidFloorPrice) { | ||
| return imp.toBuilder() | ||
| .bidfloorcur(bidFloorPrice.getCurrency()) | ||
| .bidfloor(bidFloorPrice.getValue()) | ||
| final Imp modifiedImp = modifyImp(imp, extImp); | ||
| final BidRequest outgoingRequest = request.toBuilder() | ||
| .imp(Collections.singletonList(modifiedImp)) | ||
| .build(); | ||
| } | ||
|
|
||
| private Price resolveBidFloor(Imp imp, BidRequest bidRequest) { | ||
| final Price initialBidFloorPrice = Price.of(imp.getBidfloorcur(), imp.getBidfloor()); | ||
| return BidderUtil.isValidPrice(initialBidFloorPrice) | ||
| ? convertBidFloor(initialBidFloorPrice, imp.getId(), bidRequest) | ||
| : initialBidFloorPrice; | ||
| final String uri = endpointUrl + "?pid=" + HttpUtil.encodeUrl(extImp.getPlacementId()); | ||
|
|
||
| return Result.withValue(BidderUtil.defaultRequest(outgoingRequest, uri, mapper)); | ||
|
Collaborator
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. Add |
||
| } | ||
|
|
||
| private Price convertBidFloor(Price bidFloorPrice, String impId, BidRequest bidRequest) { | ||
| final String bidFloorCur = bidFloorPrice.getCurrency(); | ||
| 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()); | ||
| } | ||
| } | ||
|
Comment on lines
+71
to
84
Collaborator
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. |
||
|
|
||
| private static void populateVideoImps(List<Imp> videoImps, Price bidFloorPrice, Imp imp) { | ||
| if (imp.getVideo() != null) { | ||
| final Imp videoImp = imp.toBuilder().banner(null).xNative(null).audio(null).build(); | ||
| videoImps.add(modifyImp(videoImp, bidFloorPrice)); | ||
| } | ||
| } | ||
| 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(); | ||
|
Comment on lines
+86
to
+93
Collaborator
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. |
||
| } | ||
|
|
||
| @Override | ||
| public final Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
| try { | ||
| final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
| return Result.withValues(extractBids(bidResponse, httpCall.getRequest().getPayload())); | ||
| return extractBids(bidResponse, httpCall.getRequest().getPayload()); | ||
| } catch (DecodeException | PreBidException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| private static List<BidderBid> extractBids(BidResponse bidResponse, BidRequest bidRequest) { | ||
| 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; | ||
|
Comment on lines
+106
to
+175
Collaborator
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. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,24 @@ | ||
| adapters: | ||
| resetdigital: | ||
| endpoint: http://b-us-east14.resetdigital.co:9001 | ||
| endpoint: https://prebid.resetdigital.co | ||
| endpoint-compression: gzip | ||
| meta-info: | ||
| maintainer-email: biddersupport@resetdigital.co | ||
| app-media-types: | ||
| - banner | ||
| - video | ||
| - native | ||
| - audio | ||
| site-media-types: | ||
| - banner | ||
| - video | ||
| - native | ||
| - audio | ||
| supported-vendors: | ||
| vendor-id: 1162 | ||
| usersync: | ||
| cookie-family-name: resetdigital | ||
| redirect: | ||
| url: https://sync.resetdigital.co/csync?pid=rubicon&redir={{redirect_url}} | ||
| support-cors: false | ||
| uid-macro: '$USER_ID' | ||
| url: https://sync.resetdigital.co/usersync?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}} | ||
| support-cors: true | ||
| uid-macro: '$UID' |
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.
getImp()can't be empty