-
Notifications
You must be signed in to change notification settings - Fork 193
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
Kobler: New adapter ported from Go #3684
base: master
Are you sure you want to change the base?
Changes from 13 commits
8ea1419
3e4f05e
858b5df
c3e11ce
5ca450c
7293b44
ac1eb72
9287f80
857a308
c385084
3078b30
d64f134
68770b0
4982cf4
3521a00
5ee2283
a8b9c4b
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 |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package org.prebid.server.bidder.kobler; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
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 org.apache.commons.collections4.CollectionUtils; | ||
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.EncodeException; | ||
import org.prebid.server.json.JacksonMapper; | ||
import org.prebid.server.proto.openrtb.ext.ExtPrebid; | ||
import org.prebid.server.proto.openrtb.ext.request.kobler.ExtImpKobler; | ||
import org.prebid.server.proto.openrtb.ext.response.BidType; | ||
import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid; | ||
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; | ||
|
||
public class KoblerBidder implements Bidder<BidRequest> { | ||
|
||
private static final TypeReference<ExtPrebid<?, ExtImpKobler>> KOBLER_EXT_TYPE_REFERENCE = | ||
new TypeReference<>() { | ||
}; | ||
private static final String EXT_PREBID = "prebid"; | ||
private static final String DEFAULT_BID_CURRENCY = "USD"; | ||
private static final String DEV_ENDPOINT = "https://bid-service.dev.essrtb.com/bid/prebid_server_rtb_call"; | ||
|
||
private final String endpointUrl; | ||
private final CurrencyConversionService currencyConversionService; | ||
private final JacksonMapper mapper; | ||
|
||
public KoblerBidder(String endpointUrl, | ||
CurrencyConversionService currencyConversionService, | ||
JacksonMapper mapper) { | ||
|
||
this.endpointUrl = HttpUtil.validateUrl(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<>(); | ||
boolean testMode = false; | ||
final List<Imp> modifiedImps = new ArrayList<>(); | ||
|
||
final List<String> currencies = new ArrayList<>(bidRequest.getCur()); | ||
if (!currencies.contains(DEFAULT_BID_CURRENCY)) { | ||
currencies.add(DEFAULT_BID_CURRENCY); | ||
} | ||
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. Extract to separate method |
||
|
||
final List<Imp> imps = bidRequest.getImp(); | ||
if (!imps.isEmpty()) { | ||
try { | ||
testMode = parseImpExt(imps.get(0)).getTest(); | ||
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. imps.getFirst() |
||
} catch (PreBidException e) { | ||
errors.add(BidderError.badInput(e.getMessage())); | ||
} | ||
} | ||
|
||
for (Imp imp : imps) { | ||
try { | ||
final Imp processedImp = processImp(bidRequest, imp); | ||
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. let's rename the method to modifiedImps.add(modifyImp(bidRequest, imp)); |
||
modifiedImps.add(processedImp); | ||
} catch (PreBidException e) { | ||
errors.add(BidderError.badInput(e.getMessage())); | ||
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. as I can see, in case of any imp processing error it's immediately should return the Result 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. any fix on that? 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. ^^ 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. if processImp throws an exception, we immediately return Result.withErrors(errors), instead of continuing the loop. |
||
} | ||
} | ||
|
||
if (modifiedImps.isEmpty()) { | ||
errors.add(BidderError.badInput("No valid impressions")); | ||
return Result.withErrors(errors); | ||
} | ||
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. this check is redundant because the process should stop on the very first imp modification failure |
||
|
||
final BidRequest modifiedRequest = bidRequest.toBuilder() | ||
.cur(currencies) | ||
.imp(modifiedImps) | ||
.build(); | ||
|
||
final String endpoint = testMode ? DEV_ENDPOINT : endpointUrl; | ||
|
||
try { | ||
final HttpRequest<BidRequest> httpRequest = BidderUtil.defaultRequest(modifiedRequest, endpoint, mapper); | ||
return Result.of(Collections.singletonList(httpRequest), errors); | ||
} catch (EncodeException e) { | ||
errors.add(BidderError.badInput("Failed to encode request: " + e.getMessage())); | ||
return Result.withErrors(errors); | ||
} | ||
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. this try-catch is redundant |
||
} | ||
|
||
private Imp processImp(BidRequest bidRequest, Imp imp) { | ||
final Price resolvedBidFloor = resolveBidFloor(imp, bidRequest); | ||
|
||
return imp.toBuilder() | ||
.bidfloor(resolvedBidFloor.getValue()) | ||
.bidfloorcur(resolvedBidFloor.getCurrency()) | ||
.build(); | ||
} | ||
|
||
private Price resolveBidFloor(Imp imp, BidRequest bidRequest) { | ||
final Price initialBidFloorPrice = Price.of(imp.getBidfloorcur(), imp.getBidfloor()); | ||
return BidderUtil.shouldConvertBidFloor(initialBidFloorPrice, DEFAULT_BID_CURRENCY) | ||
? convertBidFloor(initialBidFloorPrice, bidRequest) | ||
: initialBidFloorPrice; | ||
} | ||
|
||
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 ExtImpKobler parseImpExt(Imp imp) { | ||
try { | ||
return mapper.mapper().convertValue(imp.getExt(), KOBLER_EXT_TYPE_REFERENCE).getBidder(); | ||
} catch (IllegalArgumentException e) { | ||
throw new PreBidException(e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public 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.of(extractBids(bidResponse, errors), errors); | ||
} catch (DecodeException e) { | ||
return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
} | ||
} | ||
|
||
private List<BidderBid> extractBids(BidResponse bidResponse, List<BidderError> errors) { | ||
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { | ||
return Collections.emptyList(); | ||
} | ||
return bidsFromResponse(bidResponse, errors); | ||
} | ||
|
||
private List<BidderBid> bidsFromResponse(BidResponse bidResponse, List<BidderError> errors) { | ||
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. the errors list is redundant 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. the errors list is still redundant |
||
return bidResponse.getSeatbid().stream() | ||
.filter(Objects::nonNull) | ||
.map(SeatBid::getBid) | ||
.filter(Objects::nonNull) | ||
.flatMap(Collection::stream) | ||
.map(bid -> BidderBid.of(bid, getBidType(bid), bidResponse.getCur())) | ||
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 |
||
.filter(Objects::nonNull) | ||
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. this filter is redundant |
||
.toList(); | ||
} | ||
|
||
private BidType getBidType(Bid bid) { | ||
if (bid.getExt() == null) { | ||
return BidType.banner; | ||
} | ||
|
||
final ObjectNode prebidNode = (ObjectNode) bid.getExt().get(EXT_PREBID); | ||
if (prebidNode == null) { | ||
return BidType.banner; | ||
} | ||
|
||
final ExtBidPrebid extBidPrebid = parseExtBidPrebid(prebidNode); | ||
if (extBidPrebid == null || extBidPrebid.getType() == null) { | ||
return BidType.banner; | ||
} | ||
|
||
return extBidPrebid.getType(); // jeśli udało się sparsować | ||
} | ||
|
||
private ExtBidPrebid parseExtBidPrebid(ObjectNode prebid) { | ||
try { | ||
return mapper.mapper().treeToValue(prebid, ExtBidPrebid.class); | ||
} catch (JsonProcessingException e) { | ||
return null; | ||
} | ||
} | ||
} | ||
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 BidType getBidType(Bid bid) {
return Optional.ofNullable(bid.getExt())
.map(ext -> ext.get(EXT_PREBID))
.map(ObjectNode.class::cast)
.map(this::parseExtBidPrebid)
.map(ExtBidPrebid::getType)
.orElse(BidType.banner);
}
private ExtBidPrebid parseExtBidPrebid(ObjectNode prebid) {
try {
return mapper.mapper().treeToValue(prebid, ExtBidPrebid.class);
} catch (JsonProcessingException e) {
return null;
}
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.prebid.server.proto.openrtb.ext.request.kobler; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Value; | ||
|
||
@Value(staticConstructor = "of") | ||
public class ExtImpKobler { | ||
|
||
@JsonProperty("test") | ||
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. No need for this annotation |
||
Boolean test; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.prebid.server.spring.config.bidder; | ||
|
||
import org.prebid.server.bidder.BidderDeps; | ||
import org.prebid.server.bidder.kobler.KoblerBidder; | ||
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.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/kobler.yaml", factory = YamlPropertySourceFactory.class) | ||
public class KoblerConfiguration { | ||
|
||
private static final String BIDDER_NAME = "kobler"; | ||
|
||
@Bean("koblerConfigurationProperties") | ||
@ConfigurationProperties("adapters.kobler") | ||
BidderConfigurationProperties configurationProperties() { | ||
return new BidderConfigurationProperties(); | ||
} | ||
|
||
@Bean | ||
BidderDeps koblerBidderDeps(BidderConfigurationProperties koblerConfigurationProperties, | ||
CurrencyConversionService currencyConversionService, | ||
@NotBlank @Value("${external-url}") String externalUrl, | ||
JacksonMapper mapper) { | ||
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. indentation is bad 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. I used a checkstyle and it didnt show mistakes. How it should looks like? 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. as least the method parameter should be aligned 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. it's still not aligned) |
||
|
||
return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
.withConfig(koblerConfigurationProperties) | ||
.usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
.bidderCreator(config -> new KoblerBidder(config.getEndpoint(), currencyConversionService, mapper)) | ||
.assemble(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
adapters: | ||
kobler: | ||
endpoint: "https://bid.essrtb.com/bid/prebid_server_rtb_call" | ||
przemkaczmarek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
endpoint-compression: gzip | ||
geoscope: | ||
- NOR | ||
- SWE | ||
- DNK | ||
meta-info: | ||
maintainer-email: [email protected] | ||
site-media-types: | ||
- banner | ||
vendor-id: 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "Kobler Adapter Params", | ||
"description": "A schema which validates params accepted by the Kobler adapter", | ||
"type": "object", | ||
|
||
"properties": { | ||
"test": { | ||
"type": "boolean", | ||
"description": "Whether the request is for testing only. When multiple ad units are submitted together, it is enough to set this parameter on the first one." | ||
} | ||
} | ||
} |
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.
Move to
kobler.yaml
, see examples on how to add custom configuration properties inRubiconConfiguration