Skip to content
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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
59 changes: 38 additions & 21 deletions src/main/java/org/prebid/server/bidder/kobler/KoblerBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
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.BidRequest;
import com.iab.openrtb.request.Imp;
Expand Down Expand Up @@ -40,19 +41,23 @@ 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 String defaultBidCurrency;
private final String devEndpoint;
private final String extPrebid;
private final CurrencyConversionService currencyConversionService;
private final JacksonMapper mapper;

public KoblerBidder(String endpointUrl,
String defaultBidCurrency,
String devEndpoint,
String extPrebid,
CurrencyConversionService currencyConversionService,
JacksonMapper mapper) {

this.endpointUrl = HttpUtil.validateUrl(endpointUrl);
this.defaultBidCurrency = Objects.requireNonNull(defaultBidCurrency);
this.devEndpoint = Objects.requireNonNull(devEndpoint);
this.extPrebid = Objects.requireNonNull(extPrebid);
this.currencyConversionService = Objects.requireNonNull(currencyConversionService);
this.mapper = Objects.requireNonNull(mapper);
}
Expand All @@ -63,19 +68,13 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
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);
}
final List<String> currencies = normalizeCurrencies(bidRequest);

final List<Imp> imps = bidRequest.getImp();
if (!imps.isEmpty()) {
try {
testMode = parseImpExt(imps.getFirst()).getTest();
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
return Result.withErrors(errors);
}
try {
testMode = isTest(imps.getFirst());
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
}

for (Imp imp : imps) {
Expand All @@ -92,12 +91,28 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
.imp(modifiedImps)
.build();

final String endpoint = testMode ? DEV_ENDPOINT : endpointUrl;
final String endpoint = testMode ? devEndpoint : endpointUrl;

final HttpRequest<BidRequest> httpRequest = BidderUtil.defaultRequest(modifiedRequest, endpoint, mapper);
return Result.of(Collections.singletonList(httpRequest), errors);
}

private boolean isTest(Imp imp) {
try {
return parseImpExt(imp).getTest();
} catch (PreBidException e) {
return false;
}
}

private List<String> normalizeCurrencies(BidRequest bidRequest) {
final List<String> currencies = new ArrayList<>(bidRequest.getCur());
if (!currencies.contains(defaultBidCurrency)) {
currencies.add(defaultBidCurrency);
}
return currencies;
}

private Imp modifyImp(BidRequest bidRequest, Imp imp) {
final Price resolvedBidFloor = resolveBidFloor(imp, bidRequest);

Expand All @@ -109,7 +124,7 @@ private Imp modifyImp(BidRequest bidRequest, Imp imp) {

private Price resolveBidFloor(Imp imp, BidRequest bidRequest) {
final Price initialBidFloorPrice = Price.of(imp.getBidfloorcur(), imp.getBidfloor());
return BidderUtil.shouldConvertBidFloor(initialBidFloorPrice, DEFAULT_BID_CURRENCY)
return BidderUtil.shouldConvertBidFloor(initialBidFloorPrice, defaultBidCurrency)
? convertBidFloor(initialBidFloorPrice, bidRequest)
: initialBidFloorPrice;
}
Expand All @@ -119,9 +134,9 @@ private Price convertBidFloor(Price bidFloorPrice, BidRequest bidRequest) {
bidFloorPrice.getValue(),
bidRequest,
bidFloorPrice.getCurrency(),
DEFAULT_BID_CURRENCY);
defaultBidCurrency);

return Price.of(DEFAULT_BID_CURRENCY, convertedPrice);
return Price.of(defaultBidCurrency, convertedPrice);
}

private ExtImpKobler parseImpExt(Imp imp) {
Expand Down Expand Up @@ -155,13 +170,15 @@ private List<BidderBid> bidsFromResponse(BidResponse bidResponse) {
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(Objects::nonNull)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this filter is redundant

.map(bid -> BidderBid.of(bid, getBidType(bid), bidResponse.getCur()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add .filter(Objects::nonNull) before .map(bid -> ...)

.toList();
}

private BidType getBidType(Bid bid) {
return Optional.ofNullable(bid.getExt())
.map(ext -> ext.get(EXT_PREBID))
.map(ext -> ext.get(extPrebid))
.filter(JsonNode::isObject)
.map(ObjectNode.class::cast)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add .filter(JsonNode::isObject)

.map(this::parseExtBidPrebid)
.map(ExtBidPrebid::getType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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")
Boolean test;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.prebid.server.spring.config.bidder;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.prebid.server.bidder.BidderDeps;
import org.prebid.server.bidder.kobler.KoblerBidder;
import org.prebid.server.currency.CurrencyConversionService;
Expand All @@ -13,6 +16,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.constraints.NotBlank;

Expand All @@ -24,20 +28,43 @@ public class KoblerConfiguration {

@Bean("koblerConfigurationProperties")
@ConfigurationProperties("adapters.kobler")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
KoblerConfigurationProperties configurationProperties() {
return new KoblerConfigurationProperties();
}

@Bean
BidderDeps koblerBidderDeps(BidderConfigurationProperties koblerConfigurationProperties,
BidderDeps koblerBidderDeps(KoblerConfigurationProperties config,
CurrencyConversionService currencyConversionService,
@NotBlank @Value("${external-url}") String externalUrl,
JacksonMapper mapper) {

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(koblerConfigurationProperties)
return BidderDepsAssembler.<KoblerConfigurationProperties>forBidder(BIDDER_NAME)
.withConfig(config)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new KoblerBidder(config.getEndpoint(), currencyConversionService, mapper))
.bidderCreator(cfg -> new KoblerBidder(
cfg.getEndpoint(),
cfg.getDefaultBidCurrency(),
cfg.getDevEndpoint(),
cfg.getExtPrebid(),
currencyConversionService,
mapper))
.assemble();

}

@Validated
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
private static class KoblerConfigurationProperties extends BidderConfigurationProperties {

@NotBlank
private String defaultBidCurrency;

@NotBlank
private String devEndpoint;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe only the devEndpoint was asked to be extracted to the properties, other stuff seems static and it doesn't have much sense being extracted

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@CTMBNara marked these fields so I moved them all


@NotBlank
private String extPrebid;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/bidder-config/kobler.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
adapters:
kobler:
endpoint: "https://bid.essrtb.com/bid/prebid_server_rtb_call"
default-bid-currency: "USD"
dev-endpoint: "https://bid-service.dev.essrtb.com/bid/prebid_server_rtb_call"
ext-prebid: "prebid"
endpoint-compression: gzip
geoscope:
- NOR
Expand Down
68 changes: 42 additions & 26 deletions src/test/java/org/prebid/server/bidder/kobler/KoblerBidderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class KoblerBidderTest extends VertxTest {

private static final String ENDPOINT_URL = "https://test.com";
private static final String DEV_ENDPOINT = "https://bid-service.dev.essrtb.com/bid/prebid_server_rtb_call";
private static final String DEFAULT_BID_CURRENCY = "USD";
private static final String EXT_PREBID = "prebid";

@Mock
private CurrencyConversionService currencyConversionService;
Expand All @@ -51,13 +53,25 @@ public class KoblerBidderTest extends VertxTest {

@BeforeEach
public void setUp() {
target = new KoblerBidder(ENDPOINT_URL, currencyConversionService, jacksonMapper);
target = new KoblerBidder(
ENDPOINT_URL,
DEFAULT_BID_CURRENCY,
DEV_ENDPOINT,
EXT_PREBID,
currencyConversionService,
jacksonMapper);
}

@Test
public void creationShouldFailOnInvalidEndpointUrl() {
assertThatIllegalArgumentException().isThrownBy(() ->
new KoblerBidder("invalid_url", currencyConversionService, jacksonMapper));
new KoblerBidder(
"invalid_url",
DEFAULT_BID_CURRENCY,
DEV_ENDPOINT,
EXT_PREBID,
currencyConversionService,
jacksonMapper));
}

@Test
Expand Down Expand Up @@ -98,13 +112,15 @@ public void makeHttpRequestsShouldConvertBidFloorCurrency() {

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue().get(0).getPayload().getImp())
assertThat(result.getValue())
.extracting(HttpRequest::getPayload)
.flatExtracting(BidRequest::getImp)
.extracting(Imp::getBidfloor, Imp::getBidfloorcur)
.containsExactly(tuple(BigDecimal.TEN, "USD"));
}

@Test
public void makeHttpRequestsShouldUseDevEndpointwhenTestModeEnabled() {
public void makeHttpRequestsShouldUseDevEndpointWhenTestModeEnabled() {
// given
final BidRequest bidRequest = givenBidRequest(bidRequestBuilder -> bidRequestBuilder
.cur(singletonList("EUR")),
Expand Down Expand Up @@ -132,6 +148,28 @@ public void makeHttpRequestsShouldAddUsdToCurrenciesIfMissing() {
assertThat(result.getValue().get(0).getPayload().getCur()).containsExactlyInAnyOrder("EUR", "USD");
}

@Test
public void makeHttpRequestsShouldUseDevEndpointwhenImpExtTestIsTrue() {
// given
final ObjectNode extNode = jacksonMapper.mapper().createObjectNode();
extNode.putObject("bidder").put("test", true);

final Imp imp = Imp.builder().id("test-imp").ext(extNode).build();

final BidRequest bidRequest = BidRequest.builder()
.imp(Collections.singletonList(imp)).cur(Collections.singletonList("USD")).build();

// when
final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1);

final HttpRequest<BidRequest> httpRequest = result.getValue().get(0);
assertThat(httpRequest.getUri()).isEqualTo(DEV_ENDPOINT);
}

@Test
public void makeBidsShouldReturnErrorIfResponseBodyIsInvalid() {
// given
Expand Down Expand Up @@ -196,28 +234,6 @@ public void makeBidsShouldReturnEmptyListwhenSeatbidIsEmpty() throws JsonProcess
assertThat(result.getErrors()).isEmpty();
}

@Test
public void makeHttpRequestsShouldUseDevEndpointwhenImpExtTestIsTrue() {
// given
final ObjectNode extNode = jacksonMapper.mapper().createObjectNode();
extNode.putObject("bidder").put("test", true);

final Imp imp = Imp.builder().id("test-imp").ext(extNode).build();

final BidRequest bidRequest = BidRequest.builder()
.imp(Collections.singletonList(imp)).cur(Collections.singletonList("USD")).build();

// when
final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1);

final HttpRequest<BidRequest> httpRequest = result.getValue().get(0);
assertThat(httpRequest.getUri()).isEqualTo(DEV_ENDPOINT);
}

private static BidRequest givenBidRequest(UnaryOperator<Imp.ImpBuilder> impCustomizer) {
return givenBidRequest(identity(), impCustomizer);
}
Expand Down
Loading