-
Notifications
You must be signed in to change notification settings - Fork 224
Showheroes bidder #4190
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
Merged
Merged
Showheroes bidder #4190
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
96792a6
add showheroes bid adapter
FilipStamenkovic a64deb1
fix tests
FilipStamenkovic aec416d
adjust code after review
FilipStamenkovic c9f3ee3
adjust tests after code review
FilipStamenkovic 2289079
add tests for showheroes alias
FilipStamenkovic 2a836f7
add tests for 2nd alias and resolve comments
FilipStamenkovic 7c3491d
refactor tests after review
FilipStamenkovic f71b879
refactor after review
FilipStamenkovic 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
218 changes: 218 additions & 0 deletions
218
src/main/java/org/prebid/server/bidder/showheroes/ShowheroesBidder.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,218 @@ | ||
| package org.prebid.server.bidder.showheroes; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import com.iab.openrtb.request.App; | ||
| import com.iab.openrtb.request.BidRequest; | ||
| import com.iab.openrtb.request.Imp; | ||
| import com.iab.openrtb.request.Site; | ||
| import com.iab.openrtb.request.Source; | ||
| 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.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.ExtRequest; | ||
| import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid; | ||
| import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidChannel; | ||
| import org.prebid.server.proto.openrtb.ext.request.ExtSource; | ||
| import org.prebid.server.proto.openrtb.ext.request.showheroes.ExtImpShowheroes; | ||
| import org.prebid.server.proto.openrtb.ext.response.BidType; | ||
| import org.prebid.server.util.BidderUtil; | ||
| import org.prebid.server.util.HttpUtil; | ||
| import org.prebid.server.version.PrebidVersionProvider; | ||
|
|
||
| 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.Optional; | ||
|
|
||
| public class ShowheroesBidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final String BID_CURRENCY = "EUR"; | ||
| private static final String PBSP_JAVA = "java"; | ||
| private static final TypeReference<ExtPrebid<?, ExtImpShowheroes>> SHOWHEROES_EXT_TYPE_REFERENCE = | ||
| new TypeReference<>() { | ||
| }; | ||
|
|
||
| private final String endpointUrl; | ||
| private final CurrencyConversionService currencyConversionService; | ||
| private final JacksonMapper mapper; | ||
| private final String pbsVersion; | ||
|
|
||
| public ShowheroesBidder(String endpointUrl, | ||
| CurrencyConversionService currencyConversionService, | ||
| PrebidVersionProvider prebidVersionProvider, | ||
| JacksonMapper mapper) { | ||
|
|
||
| this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); | ||
| this.currencyConversionService = Objects.requireNonNull(currencyConversionService); | ||
| this.mapper = Objects.requireNonNull(mapper); | ||
|
|
||
| this.pbsVersion = prebidVersionProvider.getNameVersionRecord(); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) { | ||
| final BidderError validationError = validate(request.getSite(), request.getApp()); | ||
| if (validationError != null) { | ||
| return Result.withError(validationError); | ||
| } | ||
|
|
||
| final List<BidderError> errors = new ArrayList<>(); | ||
|
|
||
| final ExtRequestPrebidChannel prebidChannel = getPrebidChannel(request); | ||
| final List<Imp> modifiedImps = new ArrayList<>(request.getImp().size()); | ||
|
|
||
| for (Imp impression : request.getImp()) { | ||
| try { | ||
| modifiedImps.add(modifyImp(request, impression, prebidChannel)); | ||
| } catch (Exception e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| if (modifiedImps.isEmpty()) { | ||
| return Result.withErrors(errors); | ||
| } | ||
|
|
||
| final Source source = modifySource(request); | ||
| final BidRequest modifiedRequest = request.toBuilder().imp(modifiedImps).source(source).build(); | ||
| final HttpRequest<BidRequest> httpRequest = BidderUtil.defaultRequest(modifiedRequest, endpointUrl, mapper); | ||
|
|
||
| return Result.of(Collections.singletonList(httpRequest), errors); | ||
| } | ||
|
|
||
| private static BidderError validate(Site site, App app) { | ||
| if (site == null && app == null) { | ||
| return BidderError.badInput("BidRequest must contain one of site or app"); | ||
| } | ||
| if (site != null && site.getPage() == null) { | ||
| return BidderError.badInput("BidRequest.site.page is required"); | ||
| } | ||
| if (app != null && app.getBundle() == null) { | ||
| return BidderError.badInput("BidRequest.app.bundle is required"); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static ExtRequestPrebidChannel getPrebidChannel(BidRequest bidRequest) { | ||
| return Optional.ofNullable(bidRequest.getExt()) | ||
| .map(ExtRequest::getPrebid) | ||
| .map(ExtRequestPrebid::getChannel) | ||
| .orElse(null); | ||
| } | ||
|
|
||
| private Imp modifyImp(BidRequest bidRequest, Imp imp, ExtRequestPrebidChannel prebidChannel) { | ||
| final ExtImpShowheroes extImpShowheroes = parseImpExt(imp); | ||
|
|
||
| final boolean shouldSetDisplayManager = prebidChannel != null && imp.getDisplaymanager() == null; | ||
| final boolean shouldConvertFloor = shouldConvertFloor(imp); | ||
|
|
||
| return imp.toBuilder() | ||
| .displaymanager(shouldSetDisplayManager ? prebidChannel.getName() : imp.getDisplaymanager()) | ||
| .displaymanagerver(shouldSetDisplayManager ? prebidChannel.getVersion() : imp.getDisplaymanagerver()) | ||
| .bidfloorcur(shouldConvertFloor ? BID_CURRENCY : imp.getBidfloorcur()) | ||
| .bidfloor(shouldConvertFloor ? resolveBidFloor(bidRequest, imp) : imp.getBidfloor()) | ||
| .ext(modifyImpExt(imp.getExt(), extImpShowheroes)) | ||
| .build(); | ||
| } | ||
|
|
||
| private ExtImpShowheroes parseImpExt(Imp imp) { | ||
| try { | ||
| return mapper.mapper().convertValue(imp.getExt(), SHOWHEROES_EXT_TYPE_REFERENCE).getBidder(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private ObjectNode modifyImpExt(ObjectNode impExt, ExtImpShowheroes shImpExt) { | ||
| impExt.set("params", mapper.mapper().createObjectNode().put("unitId", shImpExt.getUnitId())); | ||
| return impExt; | ||
| } | ||
|
|
||
| private static boolean shouldConvertFloor(Imp imp) { | ||
| return BidderUtil.isValidPrice(imp.getBidfloor()) | ||
| && !StringUtils.equalsIgnoreCase(imp.getBidfloorcur(), BID_CURRENCY); | ||
| } | ||
|
|
||
| private BigDecimal resolveBidFloor(BidRequest bidRequest, Imp imp) { | ||
| return currencyConversionService.convertCurrency( | ||
| imp.getBidfloor(), bidRequest, imp.getBidfloorcur(), BID_CURRENCY); | ||
| } | ||
|
|
||
| private Source modifySource(BidRequest bidRequest) { | ||
| if (pbsVersion == null) { | ||
| return bidRequest.getSource(); | ||
| } | ||
|
|
||
| final Source source = bidRequest.getSource(); | ||
|
|
||
| final ExtSource extSource = Optional.ofNullable(source) | ||
| .map(Source::getExt) | ||
| .orElse(ExtSource.of(null)); | ||
| final ObjectNode prebidExtSource = Optional.ofNullable(extSource.getProperty("pbs")) | ||
| .filter(JsonNode::isObject) | ||
| .map(ObjectNode.class::cast) | ||
| .orElseGet(mapper.mapper()::createObjectNode) | ||
| .put("pbsv", pbsVersion) | ||
| .put("pbsp", PBSP_JAVA); | ||
| extSource.addProperty("pbs", prebidExtSource); | ||
|
|
||
| return Optional.ofNullable(source) | ||
| .map(Source::toBuilder) | ||
| .orElseGet(Source::builder) | ||
| .ext(extSource) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
| try { | ||
| final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
| return Result.of(extractBids(bidResponse), Collections.emptyList()); | ||
| } catch (DecodeException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private List<BidderBid> extractBids(BidResponse bidResponse) { | ||
| if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| return bidResponse.getSeatbid().stream() | ||
| .filter(Objects::nonNull) | ||
| .map(SeatBid::getBid) | ||
| .filter(Objects::nonNull) | ||
| .flatMap(Collection::stream) | ||
| .filter(Objects::nonNull) | ||
| .map(bid -> BidderBid.of(bid, getBidType(bid), bidResponse.getCur())) | ||
| .filter(Objects::nonNull) | ||
| .toList(); | ||
| } | ||
|
|
||
| private static BidType getBidType(Bid bid) { | ||
| return switch (bid.getMtype()) { | ||
| case 1 -> BidType.banner; | ||
| case 2 -> BidType.video; | ||
| case null, default -> BidType.video; | ||
| }; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/prebid/server/proto/openrtb/ext/request/showheroes/ExtImpShowheroes.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,11 @@ | ||
| package org.prebid.server.proto.openrtb.ext.request.showheroes; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class ExtImpShowheroes { | ||
|
|
||
| @JsonProperty("unitId") | ||
| String unitId; | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/org/prebid/server/spring/config/bidder/ShowheroesConfiguration.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,49 @@ | ||
| package org.prebid.server.spring.config.bidder; | ||
|
|
||
| import org.prebid.server.bidder.BidderDeps; | ||
| import org.prebid.server.bidder.showheroes.ShowheroesBidder; | ||
| 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.config.bidder.util.UsersyncerCreator; | ||
| import org.prebid.server.spring.env.YamlPropertySourceFactory; | ||
| import org.prebid.server.version.PrebidVersionProvider; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| 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; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| @Configuration | ||
| @PropertySource(value = "classpath:/bidder-config/showheroes.yaml", factory = YamlPropertySourceFactory.class) | ||
| public class ShowheroesConfiguration { | ||
|
|
||
| private static final String BIDDER_NAME = "showheroes"; | ||
|
|
||
| @Bean("showheroesConfigurationProperties") | ||
| @ConfigurationProperties("adapters.showheroes") | ||
| BidderConfigurationProperties configurationProperties() { | ||
| return new BidderConfigurationProperties(); | ||
| } | ||
|
|
||
| @Bean | ||
| BidderDeps showheroesBidderDeps(BidderConfigurationProperties showheroesConfigurationProperties, | ||
| @NotBlank @Value("${external-url}") String externalUrl, | ||
| CurrencyConversionService currencyConversionService, | ||
| PrebidVersionProvider prebidVersionProvider, | ||
| JacksonMapper mapper) { | ||
|
|
||
| return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
| .withConfig(showheroesConfigurationProperties) | ||
| .usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
| .bidderCreator(config -> new ShowheroesBidder( | ||
| config.getEndpoint(), | ||
| currencyConversionService, | ||
| prebidVersionProvider, | ||
| 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,17 @@ | ||
| adapters: | ||
| showheroes: | ||
| endpoint: https://ads.viralize.tv/openrtb2/auction/ | ||
| aliases: | ||
| showheroes-bs: ~ | ||
| showheroesBs: ~ | ||
| ortb-version: '2.6' | ||
| meta-info: | ||
| maintainer-email: tech@showheroes.com | ||
| app-media-types: | ||
| - banner | ||
| - video | ||
| site-media-types: | ||
| - banner | ||
| - video | ||
| supported-vendors: | ||
| vendor-id: 111 | ||
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,14 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Showheroes Adapter Params", | ||
| "description": "A schema which validates params accepted by the Showheroes adapter", | ||
| "type": "object", | ||
| "properties": { | ||
| "unitId": { | ||
| "type": "string", | ||
| "description": "Unit ID", | ||
| "minLength": 8 | ||
| } | ||
| }, | ||
| "required": ["unitId"] | ||
| } |
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.
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.
separate integration tests should be added for all the hard aliases to make sure the configuration is correct