-
Notifications
You must be signed in to change notification settings - Fork 220
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
Open
FilipStamenkovic
wants to merge
7
commits into
prebid:master
Choose a base branch
from
showheroes:showheroes-bidder
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.
+1,244
β0
Open
Showheroes bidder #4190
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e1b6a5
add showheroes bid adapter
FilipStamenkovic 7cf2464
fix tests
FilipStamenkovic f5186ee
adjust code after review
FilipStamenkovic d21324c
adjust tests after code review
FilipStamenkovic 4ee05b4
add tests for showheroes alias
FilipStamenkovic ae6260f
add tests for 2nd alias and resolve comments
FilipStamenkovic fb04e9f
refactor tests 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
212 changes: 212 additions & 0 deletions
212
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,212 @@ | ||
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.ObjectUtils; | ||
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, 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(Imp imp, ExtImpShowheroes shImpExt) { | ||
final ObjectNode impExt = ObjectUtils.defaultIfNull(imp.getExt(), mapper.mapper().createObjectNode()); | ||
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 = ObjectUtils.defaultIfNull(bidRequest.getSource(), Source.builder().build()); | ||
final ExtSource extSource = ObjectUtils.defaultIfNull(source.getExt(), 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 source.toBuilder().ext(extSource).build(); | ||
} | ||
Comment on lines
+161
to
+176
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 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: ~ | ||
Comment on lines
+4
to
+6
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. separate integration tests should be added for all the hard aliases to make sure the configuration is correct |
||
ortb-version: '2.6' | ||
meta-info: | ||
maintainer-email: [email protected] | ||
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.
imp.ext
can't benull
here