-
Notifications
You must be signed in to change notification settings - Fork 228
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
Open
przemkaczmarek
wants to merge
2
commits into
master
Choose a base branch
from
ResetDigital-Switch-to-PenRTB
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
206 changes: 97 additions & 109 deletions
206
src/main/java/org/prebid/server/bidder/resetdigital/ResetDigitalBidder.java
This file contains hidden or 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,182 +1,170 @@ | ||
| 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; | ||
| import com.iab.openrtb.response.BidResponse; | ||
| import com.iab.openrtb.response.SeatBid; | ||
| import io.vertx.core.MultiMap; | ||
| import org.apache.commons.collections4.CollectionUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.prebid.server.bidder.Bidder; | ||
| import org.prebid.server.bidder.model.BidderBid; | ||
| 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; | ||
| import java.util.Optional; | ||
|
|
||
| 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 (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); | ||
| } | ||
|
|
||
| private static Imp modifyImp(Imp imp, Price bidFloorPrice) { | ||
| return imp.toBuilder() | ||
| .bidfloorcur(bidFloorPrice.getCurrency()) | ||
| .bidfloor(bidFloorPrice.getValue()) | ||
| .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; | ||
| } | ||
|
|
||
| private Price convertBidFloor(Price bidFloorPrice, String impId, BidRequest bidRequest) { | ||
| final String bidFloorCur = bidFloorPrice.getCurrency(); | ||
| final Imp imp = request.getImp().getFirst(); | ||
| final ExtImpResetDigital extImp; | ||
| try { | ||
| final BigDecimal convertedPrice = currencyConversionService | ||
| .convertCurrency(bidFloorPrice.getValue(), bidRequest, bidFloorCur, DEFAULT_CURRENCY); | ||
|
|
||
| return Price.of(DEFAULT_CURRENCY, convertedPrice); | ||
| extImp = parseImpExt(imp); | ||
| } 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)); | ||
| return Result.withError(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| 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)); | ||
| } | ||
| final Imp modifiedImp = modifyImp(imp, extImp); | ||
| final BidRequest outgoingRequest = request.toBuilder() | ||
| .imp(Collections.singletonList(modifiedImp)) | ||
| .build(); | ||
|
|
||
| final String uri = endpointUrl + "?pid=" + HttpUtil.encodeUrl(extImp.getPlacementId()); | ||
| final MultiMap headers = HttpUtil.headers() | ||
| .add(HttpUtil.X_OPENRTB_VERSION_HEADER, "2.5"); | ||
|
|
||
| return Result.withValue(BidderUtil.defaultRequest(outgoingRequest, headers, uri, mapper)); | ||
| } | ||
|
|
||
| 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 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()); | ||
| } | ||
| } | ||
|
|
||
| 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)); | ||
| } | ||
| private static Imp modifyImp(Imp imp, ExtImpResetDigital extImp) { | ||
| return StringUtils.isBlank(imp.getTagid()) | ||
| ? imp.toBuilder().tagid(extImp.getPlacementId()).build() | ||
| : imp; | ||
| } | ||
|
|
||
| @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.withValues(extractBids(bidResponse, httpCall.getRequest().getPayload())); | ||
| 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) { | ||
| private static List<BidderBid> extractBids(BidResponse bidResponse, | ||
| BidRequest bidRequest, | ||
| List<BidderError> errors) { | ||
|
|
||
| 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"); | ||
|
|
||
| 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 bidsFromResponse(bidResponse, bidRequest); | ||
|
|
||
| return bidderBids; | ||
| } | ||
|
|
||
| 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(); | ||
| 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, 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 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; | ||
| } | ||
| throw new PreBidException("Failed to find banner/video/audio impression " + impId); | ||
| return BidType.banner; | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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,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' |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.