Skip to content

Commit

Permalink
Update NextMillennium adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
marki1an committed Jan 17, 2024
1 parent d2fbbdd commit 4751ca0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,14 @@ public class NextMillenniumBidder implements Bidder<BidRequest> {
};

private final String endpointUrl;
private final List<String> nmmFlags;
private final JacksonMapper mapper;

public NextMillenniumBidder(String endpointUrl, JacksonMapper mapper) {
public NextMillenniumBidder(String endpointUrl, JacksonMapper mapper, List<String> nmmFlags) {
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
this.mapper = Objects.requireNonNull(mapper);
}

private BidRequest updateBidRequest(BidRequest bidRequest, ExtImpNextMillennium ext) {

final ExtRequest extRequest = ExtRequest.of(ExtRequestPrebid.builder()
.storedrequest(ExtStoredRequest.of(resolveStoredRequestId(bidRequest, ext)))
.build());

final ObjectNode impExt = mapper.mapper().valueToTree(extRequest);

final List<Imp> imps = bidRequest.getImp().stream()
.map(imp -> imp.toBuilder().ext(impExt).build())
.toList();

return bidRequest.toBuilder().imp(imps).ext(extRequest).build();
}

private static String resolveStoredRequestId(BidRequest bidRequest, ExtImpNextMillennium extImpNextMillennium) {
final String groupId = extImpNextMillennium.getGroupId();
if (StringUtils.isEmpty(groupId)) {
return extImpNextMillennium.getPlacementId();
}
this.nmmFlags = Objects.requireNonNull(nmmFlags);

final String size = formattedSizeFromBanner(bidRequest.getImp().get(0).getBanner());
final String domain = ObjectUtils.firstNonNull(
ObjectUtil.getIfNotNull(bidRequest.getSite(), Site::getDomain),
ObjectUtil.getIfNotNull(bidRequest.getApp(), App::getDomain),
StringUtils.EMPTY);

return "g%s;%s;%s".formatted(groupId, size, domain);
}

@Override
Expand All @@ -98,6 +71,12 @@ private List<ExtImpNextMillennium> getImpExts(BidRequest bidRequest, List<Bidder
.toList();
}

private List<HttpRequest<BidRequest>> makeRequests(BidRequest bidRequest, List<ExtImpNextMillennium> extImps) {
return extImps.stream()
.map(extImp -> makeHttpRequest(updateBidRequest(bidRequest, extImp)))
.toList();
}

private ExtImpNextMillennium convertExt(Imp imp, List<BidderError> errors) {
try {
return mapper.mapper()
Expand All @@ -109,10 +88,33 @@ private ExtImpNextMillennium convertExt(Imp imp, List<BidderError> errors) {
return null;
}

private List<HttpRequest<BidRequest>> makeRequests(BidRequest bidRequest, List<ExtImpNextMillennium> extImps) {
return extImps.stream()
.map(extImp -> makeHttpRequest(updateBidRequest(bidRequest, extImp)))
private BidRequest updateBidRequest(BidRequest bidRequest, ExtImpNextMillennium ext) {

final ExtRequestPrebid prebid = ExtRequestPrebid.builder()
.storedrequest(ExtStoredRequest.of(resolveStoredRequestId(bidRequest, ext)))
.build();
final ExtRequest extRequest = ExtRequest.of(prebid);

final List<Imp> imps = bidRequest.getImp().stream()
.map(imp -> imp.toBuilder().ext(createImpExt(prebid)).build())
.toList();

return bidRequest.toBuilder().imp(imps).ext(extRequest).build();
}

private static String resolveStoredRequestId(BidRequest bidRequest, ExtImpNextMillennium extImpNextMillennium) {
final String groupId = extImpNextMillennium.getGroupId();
if (StringUtils.isEmpty(groupId)) {
return extImpNextMillennium.getPlacementId();
}

final String size = formattedSizeFromBanner(bidRequest.getImp().get(0).getBanner());
final String domain = ObjectUtils.firstNonNull(
ObjectUtil.getIfNotNull(bidRequest.getSite(), Site::getDomain),
ObjectUtil.getIfNotNull(bidRequest.getApp(), App::getDomain),
StringUtils.EMPTY);

return "g%s;%s;%s".formatted(groupId, size, domain);
}

private static String formattedSizeFromBanner(Banner banner) {
Expand All @@ -137,6 +139,13 @@ private static String formatSize(Integer w, Integer h) {
: null;
}

private ObjectNode createImpExt(ExtRequestPrebid prebid) {
final ObjectNode impExt = mapper.mapper().createObjectNode();
impExt.set("prebid", mapper.mapper().valueToTree(prebid));
impExt.set("nextMillennium", mapper.mapper().valueToTree(nmmFlags));
return impExt;
}

private HttpRequest<BidRequest> makeHttpRequest(BidRequest bidRequest) {
return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
Expand Down
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.nextmillennium.NextMillenniumBidder;
import org.prebid.server.json.JacksonMapper;
Expand All @@ -14,6 +17,7 @@
import org.springframework.context.annotation.PropertySource;

import javax.validation.constraints.NotBlank;
import java.util.List;

@Configuration
@PropertySource(value = "classpath:/bidder-config/nextmillennium.yaml", factory = YamlPropertySourceFactory.class)
Expand All @@ -23,19 +27,36 @@ public class NextMillenniumConfiguration {

@Bean("nextMillenniumConfigurationProperties")
@ConfigurationProperties("adapters.nextmillennium")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
NextMillenniumConfigurationProperties configurationProperties() {
return new NextMillenniumConfigurationProperties();
}

@Bean
BidderDeps nextMillenniumBidderDeps(BidderConfigurationProperties nextMillenniumConfigurationProperties,
BidderDeps nextMillenniumBidderDeps(NextMillenniumConfigurationProperties nextMillenniumConfigurationProperties,
@NotBlank @Value("${external-url}") String externalUrl,
JacksonMapper mapper) {

return BidderDepsAssembler.forBidder(BIDDER_NAME)
return BidderDepsAssembler.<NextMillenniumConfigurationProperties>forBidder(BIDDER_NAME)
.withConfig(nextMillenniumConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new NextMillenniumBidder(config.getEndpoint(), mapper))
.assemble();
.bidderCreator(config -> {
final ExtraInfo extraInfo = config.getExtraInfo();
return new NextMillenniumBidder(config.getEndpoint(), mapper, extraInfo.getNmmFlags());
}).assemble();
}

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

private ExtraInfo extraInfo = new ExtraInfo();
}

@Data
@NoArgsConstructor
private static class ExtraInfo {

List<String> nmmFlags;
}
}
4 changes: 4 additions & 0 deletions src/main/resources/bidder-config/nextmillennium.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ adapters:
url: https://cookies.nextmillmedia.com/sync?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}}
support-cors: false
uid-macro: '[NMUID]'
redirect:
url: https://cookies.nextmillmedia.com/sync?type=image&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}}
support-cors: false
userMacro: '[NMUID]'
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.prebid.server.bidder.nextmillennium;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iab.openrtb.request.App;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
Expand Down Expand Up @@ -41,11 +42,13 @@ public class NextMillenniumBidderTest extends VertxTest {

private static final String ENDPOINT_URL = "https://test-url.com/";

private final NextMillenniumBidder target = new NextMillenniumBidder(ENDPOINT_URL, jacksonMapper);
private final NextMillenniumBidder target =
new NextMillenniumBidder(ENDPOINT_URL, jacksonMapper, List.of("valueOne", "valueTwo"));

@Test
public void creationShouldFailOnInvalidEndpointUrl() {
assertThatIllegalArgumentException().isThrownBy(() -> new NextMillenniumBidder("invalid_url", jacksonMapper));
assertThatIllegalArgumentException().isThrownBy(() ->
new NextMillenniumBidder("invalid_url", jacksonMapper, List.of("valueOne", "valueTwo")));
}

@Test
Expand Down Expand Up @@ -253,6 +256,24 @@ public void makeHttpRequestsWithInvalidImpsShouldReturnError() {
assertThat(result.getValue()).isEmpty();
}

@Test
public void makeHttpRequestsShouldReturnImpExtNextMillenniumWhenNmmFlagsConfigured() {
// given
final BidRequest bidRequest = givenBidRequest(identity(),
givenImpWithExt(identity(), ExtImpNextMillennium.of("placement1", "group1")));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(HttpRequest::getPayload)
.flatExtracting(BidRequest::getImp)
.extracting(Imp::getExt)
.containsExactly(createImpExt(List.of("valueOne", "valueTwo")));
}

@Test
public void makeBidsShouldReturnBannerBidByDefault() throws JsonProcessingException {
// given
Expand Down Expand Up @@ -477,4 +498,11 @@ private static Imp givenImpWithExt(UnaryOperator<Imp.ImpBuilder> impCustomizer,
ExtPrebid.of(null, extImpNextMillennium))))::apply);
}

private static ObjectNode createImpExt(List<String> values) {
final ObjectNode objectNode = mapper.createObjectNode();
objectNode.set("prebid", mapper.valueToTree(ExtRequestPrebid.builder()
.storedrequest(ExtStoredRequest.of("ggroup1;;")).build()));
objectNode.set("nextMillennium", mapper.valueToTree(values));
return objectNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"storedrequest": {
"id": "placement_id"
}
}
},
"nextMillennium": [
"1"
]
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ adapters.motorik.enabled=true
adapters.motorik.endpoint=http://localhost:8090/motorik-exchange?k={{AccountID}}&name={{SourceId}}
adapters.nextmillennium.enabled=true
adapters.nextmillennium.endpoint=http://localhost:8090/nextmillennium-exchange
adapters.nextmillennium.extra-info.nmmFlags=1
adapters.nobid.enabled=true
adapters.nobid.endpoint=http://localhost:8090/nobid-exchange?pubid=
adapters.oms.enabled=true
Expand Down

0 comments on commit 4751ca0

Please sign in to comment.