-
Notifications
You must be signed in to change notification settings - Fork 248
Port Scalibur: New Adapter #4596
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
osulzhenko
wants to merge
3
commits into
master
Choose a base branch
from
port-scalibur
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
3 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
294 changes: 294 additions & 0 deletions
294
src/main/java/org/prebid/server/bidder/scalibur/ScaliburBidder.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 |
|---|---|---|
| @@ -0,0 +1,294 @@ | ||
| package org.prebid.server.bidder.scalibur; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.node.IntNode; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import com.fasterxml.jackson.databind.node.TextNode; | ||
| import com.iab.openrtb.request.BidRequest; | ||
| import com.iab.openrtb.request.Imp; | ||
| import com.iab.openrtb.request.Video; | ||
| import com.iab.openrtb.response.Bid; | ||
| import com.iab.openrtb.response.BidResponse; | ||
| import com.iab.openrtb.response.SeatBid; | ||
| 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.proto.openrtb.ext.request.scalibur.ExtImpScalibur; | ||
| 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.ExtRequest; | ||
| 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.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class ScaliburBidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final TypeReference<ExtPrebid<?, ExtImpScalibur>> TYPE_REFERENCE = new TypeReference<>() { | ||
| }; | ||
| private static final String DEFAULT_BID_CURRENCY = "USD"; | ||
| private static final String VAST_XML = """ | ||
| <VAST version=\"3.0\"><Ad><Wrapper><VASTAdTagURI><![CDATA[%s]]></VASTAdTagURI></Wrapper></Ad></VAST> | ||
| """; | ||
|
|
||
| private final String endpointUrl; | ||
| private final CurrencyConversionService currencyConversionService; | ||
| private final JacksonMapper mapper; | ||
|
|
||
| public ScaliburBidder(String endpointUrl, | ||
| CurrencyConversionService currencyConversionService, | ||
| 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 bidRequest) { | ||
| final List<BidderError> errors = new ArrayList<>(); | ||
| final List<Imp> validImps = new ArrayList<>(); | ||
|
|
||
| for (Imp imp : bidRequest.getImp()) { | ||
| try { | ||
| final ExtImpScalibur scaliburExt = parseImpExt(imp); | ||
| validImps.add(modifyImp(imp, scaliburExt, bidRequest)); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| if (validImps.isEmpty()) { | ||
| return Result.withErrors(errors); | ||
| } | ||
|
|
||
| final BidRequest modifiedBidRequest = bidRequest.toBuilder() | ||
| .imp(validImps) | ||
| .cur(null) | ||
| .ext(isDebugEnabled(bidRequest) ? createDebugExt() : null) | ||
| .build(); | ||
|
|
||
| final HttpRequest<BidRequest> httpRequest = BidderUtil.defaultRequest(modifiedBidRequest, endpointUrl, mapper); | ||
| return Result.of(Collections.singletonList(httpRequest), errors); | ||
| } | ||
|
|
||
| private ExtImpScalibur parseImpExt(Imp imp) { | ||
| try { | ||
| return mapper.mapper().convertValue(imp.getExt(), TYPE_REFERENCE).getBidder(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private Imp modifyImp(Imp imp, | ||
| ExtImpScalibur extImpScalibur, | ||
| BidRequest bidRequest) { | ||
|
|
||
| final Price resolvedBidFloor = resolveBidFloor(imp, extImpScalibur, bidRequest); | ||
| final JsonNode gpidNode = imp.getExt().get("gpid"); | ||
|
|
||
| return imp.toBuilder() | ||
| .bidfloor(resolvedBidFloor.getValue()) | ||
| .bidfloorcur(resolvedBidFloor.getCurrency()) | ||
| .video(resolveVideo(imp.getVideo())) | ||
| .ext(resolveImpExt(extImpScalibur, resolvedBidFloor, gpidNode)) | ||
| .build(); | ||
| } | ||
|
|
||
| private Price resolveBidFloor(Imp imp, | ||
| ExtImpScalibur extImpScalibur, | ||
| BidRequest bidRequest) { | ||
|
|
||
| final BigDecimal extPrice = extImpScalibur.getBidFloor(); | ||
| final Price price = BidderUtil.isValidPrice(extPrice) | ||
| ? Price.of(StringUtils.defaultIfBlank(extImpScalibur.getBidFloorCur(), imp.getBidfloorcur()), extPrice) | ||
| : Price.of(imp.getBidfloorcur(), imp.getBidfloor()); | ||
| if (BidderUtil.shouldConvertBidFloor(price, DEFAULT_BID_CURRENCY)) { | ||
| return convertBidFloor(price, bidRequest); | ||
| } | ||
| return Price.of( | ||
| StringUtils.defaultIfBlank(price.getCurrency(), DEFAULT_BID_CURRENCY), | ||
| price.getValue()); | ||
| } | ||
|
|
||
| private Price convertBidFloor(Price bidFloorPrice, BidRequest bidRequest) { | ||
| final BigDecimal convertedPrice = currencyConversionService.convertCurrency(bidFloorPrice.getValue(), | ||
| bidRequest, | ||
| bidFloorPrice.getCurrency(), | ||
| DEFAULT_BID_CURRENCY); | ||
|
|
||
| return Price.of(DEFAULT_BID_CURRENCY, convertedPrice); | ||
| } | ||
|
|
||
| private ObjectNode resolveImpExt(ExtImpScalibur extImpScalibur, | ||
| Price bidFloor, | ||
| JsonNode gpidNode) { | ||
|
|
||
| final ObjectNode ext = mapper.mapper().createObjectNode(); | ||
| ext.set("placementId", TextNode.valueOf(extImpScalibur.getPlacementId())); | ||
| if (BidderUtil.isValidPrice(bidFloor)) { | ||
| ext.set("bidfloor", mapper.mapper().valueToTree(bidFloor.getValue())); | ||
| } | ||
| ext.set("bidfloorcur", TextNode.valueOf(bidFloor.getCurrency())); | ||
| if (gpidNode != null && !gpidNode.isNull()) { | ||
| ext.set("gpid", gpidNode); | ||
| } | ||
| return ext; | ||
| } | ||
|
|
||
| private Video resolveVideo(Video video) { | ||
| if (video == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final Video.VideoBuilder builder = video.toBuilder(); | ||
|
|
||
| if (CollectionUtils.isEmpty(video.getMimes())) { | ||
| builder.mimes(Collections.singletonList("video/mp4")); | ||
| } | ||
| if (BidderUtil.isNullOrZero(video.getMinduration())) { | ||
| builder.minduration(1); | ||
| } | ||
| if (BidderUtil.isNullOrZero(video.getMaxduration())) { | ||
| builder.maxduration(180); | ||
| } | ||
| if (BidderUtil.isNullOrZero(video.getMaxbitrate())) { | ||
| builder.maxbitrate(30_000); | ||
| } | ||
| if (CollectionUtils.isEmpty(video.getProtocols())) { | ||
| builder.protocols(List.of(2, 3, 5, 6)); | ||
| } | ||
| if (BidderUtil.isNullOrZero(video.getW())) { | ||
| builder.w(640); | ||
| } | ||
| if (BidderUtil.isNullOrZero(video.getH())) { | ||
| builder.h(480); | ||
| } | ||
| if (BidderUtil.isNullOrZero(video.getPlacement())) { | ||
| builder.placement(1); | ||
| } | ||
| if (BidderUtil.isNullOrZero(video.getLinearity())) { | ||
| builder.linearity(1); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| private boolean isDebugEnabled(BidRequest bidRequest) { | ||
| if (Objects.equals(bidRequest.getTest(), 1)) { | ||
| return true; | ||
| } | ||
| final ExtRequest ext = bidRequest.getExt(); | ||
| if (ext == null || ext.getPrebid() == null) { | ||
| return false; | ||
| } | ||
| return Objects.equals(ext.getPrebid().getDebug(), 1); | ||
| } | ||
|
|
||
| private ExtRequest createDebugExt() { | ||
| final ExtRequest extRequest = ExtRequest.empty(); | ||
| extRequest.addProperty("isDebug", IntNode.valueOf(1)); | ||
| return extRequest; | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
| try { | ||
| final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
| return extractBids(httpCall.getRequest().getPayload(), bidResponse); | ||
| } catch (DecodeException | PreBidException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| private Result<List<BidderBid>> extractBids(BidRequest bidRequest, BidResponse bidResponse) { | ||
| if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { | ||
| return Result.empty(); | ||
| } | ||
| final List<BidderError> errors = new ArrayList<>(); | ||
| final List<BidderBid> bidderBids = new ArrayList<>(); | ||
| for (SeatBid seatBid : bidResponse.getSeatbid()) { | ||
| if (CollectionUtils.isEmpty(seatBid.getBid())) { | ||
| continue; | ||
| } | ||
|
|
||
| for (Bid bid : seatBid.getBid()) { | ||
| try { | ||
| bidderBids.add(createBidderBid(bid, bidRequest, bidResponse)); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return Result.of(bidderBids, errors); | ||
| } | ||
|
|
||
| private BidderBid createBidderBid(Bid bid, BidRequest bidRequest, BidResponse bidResponse) { | ||
| final Imp imp = bidRequest.getImp().stream() | ||
| .filter(candidate -> Objects.equals(candidate.getId(), bid.getImpid())) | ||
| .findFirst() | ||
| .orElseThrow(() -> new PreBidException("Invalid bid imp ID %s".formatted(bid.getImpid()))); | ||
|
|
||
| final BidType bidType = resolveBidType(bid, imp); | ||
| final Bid resolvedBid = bidType == BidType.video ? resolveVideoBid(bid) : bid; | ||
| final String resolvedCurrency = StringUtils.defaultIfEmpty(bidResponse.getCur(), DEFAULT_BID_CURRENCY); | ||
|
|
||
| return BidderBid.of(resolvedBid, bidType, resolvedCurrency); | ||
| } | ||
|
|
||
| private Bid resolveVideoBid(Bid bid) { | ||
| if (bid.getExt() == null) { | ||
| return bid; | ||
| } | ||
|
|
||
| final JsonNode vastXml = bid.getExt().get("vastXml"); | ||
| if (isNonEmptyText(vastXml)) { | ||
| return bid.toBuilder().adm(vastXml.asText()).build(); | ||
| } | ||
|
|
||
| final JsonNode vastUrl = bid.getExt().get("vastUrl"); | ||
| if (isNonEmptyText(vastUrl) && StringUtils.isEmpty(bid.getAdm())) { | ||
| return bid.toBuilder() | ||
| .adm(VAST_XML.formatted(vastUrl.asText()).trim()) | ||
| .build(); | ||
| } | ||
| return bid; | ||
| } | ||
|
|
||
| private boolean isNonEmptyText(JsonNode node) { | ||
| return node != null && node.isTextual() && StringUtils.isNotEmpty(node.asText()); | ||
| } | ||
|
|
||
| private BidType resolveBidType(Bid bid, Imp imp) { | ||
| if (Objects.equals(bid.getMtype(), 1)) { | ||
| return BidType.banner; | ||
| } | ||
| if (Objects.equals(bid.getMtype(), 2)) { | ||
| return BidType.video; | ||
| } | ||
| if (imp.getBanner() != null && imp.getVideo() == null) { | ||
| return BidType.banner; | ||
| } | ||
| if (imp.getVideo() != null && imp.getBanner() == null) { | ||
| return BidType.video; | ||
| } | ||
| throw new PreBidException( | ||
| "Unsupported or ambiguous media type for bid id=%s".formatted(bid.getId())); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/prebid/server/proto/openrtb/ext/request/scalibur/ExtImpScalibur.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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package org.prebid.server.proto.openrtb.ext.request.scalibur; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class ExtImpScalibur { | ||
|
|
||
| @JsonProperty("placementId") | ||
| String placementId; | ||
|
|
||
| @JsonProperty("bidfloor") | ||
| BigDecimal bidFloor; | ||
|
|
||
| @JsonProperty("bidfloorcur") | ||
| String bidFloorCur; | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/main/java/org/prebid/server/spring/config/bidder/ScaliburConfiguration.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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package org.prebid.server.spring.config.bidder; | ||
|
|
||
| import org.prebid.server.bidder.BidderDeps; | ||
| import org.prebid.server.bidder.scalibur.ScaliburBidder; | ||
| import org.prebid.server.currency.CurrencyConversionService; | ||
| import org.prebid.server.json.JacksonMapper; | ||
| import org.prebid.server.spring.config.bidder.model.BidderConfigurationProperties; | ||
| import org.prebid.server.spring.config.bidder.util.BidderDepsAssembler; | ||
| import org.prebid.server.spring.env.YamlPropertySourceFactory; | ||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.PropertySource; | ||
|
|
||
| @Configuration | ||
| @PropertySource(value = "classpath:/bidder-config/scalibur.yaml", factory = YamlPropertySourceFactory.class) | ||
| public class ScaliburConfiguration { | ||
|
|
||
| private static final String BIDDER_NAME = "scalibur"; | ||
|
|
||
| @Bean("scaliburConfigurationProperties") | ||
| @ConfigurationProperties("adapters.scalibur") | ||
| BidderConfigurationProperties configurationProperties() { | ||
| return new BidderConfigurationProperties(); | ||
| } | ||
|
|
||
| @Bean | ||
| BidderDeps scaliburBidderDeps(BidderConfigurationProperties scaliburConfigurationProperties, | ||
| CurrencyConversionService currencyConversionService, | ||
| JacksonMapper mapper) { | ||
|
|
||
| return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
| .withConfig(scaliburConfigurationProperties) | ||
| .bidderCreator(config -> new ScaliburBidder(config.getEndpoint(), currencyConversionService, mapper)) | ||
| .assemble(); | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| adapters: | ||
| scalibur: | ||
| endpoint: https://srv.scalibur.io/adserver/ortb?type=prebid-server | ||
| ortb-version: "2.6" | ||
| endpoint-compression: gzip | ||
| meta-info: | ||
| maintainer-email: support@scalibur.io | ||
| app-media-types: | ||
| - banner | ||
| - video | ||
| site-media-types: | ||
| - banner | ||
| - video | ||
| supported-vendors: | ||
| vendor-id: 1471 | ||
| usersync: | ||
| cookie-family-name: scalibur | ||
| iframe: | ||
| url: https://srv.scalibur.io/adserver/sync?type=iframe&gdpr={gdpr}&gdpr_consent={gdpr_consent}&us_privacy={us_privacy}&redirect={redirect_url} | ||
| uid-macro: '[PBS_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Scalibur Adapter Params", | ||
| "description": "A valid BidderParams object for the Scalibur adapter", | ||
| "type": "object", | ||
| "properties": { | ||
| "placementId": { | ||
| "type": "string", | ||
| "description": "Placement ID", | ||
| "minLength": 1 | ||
| }, | ||
| "bidfloor": { | ||
| "type": "number", | ||
| "description": "The minimum price acceptable for a bid", | ||
| "minimum": 0 | ||
| }, | ||
| "bidfloorcur": { | ||
| "type": "string", | ||
| "description": "currency floor price" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "placementId" | ||
| ] | ||
| } |
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.