Skip to content

Commit bf7d54a

Browse files
committed
LiveIntent Omni Channel Module Refactoring
1 parent e458a4c commit bf7d54a

6 files changed

Lines changed: 197 additions & 236 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.prebid.server.hooks.modules.liveintent.omni.channel.identity.config;
22

3-
import org.prebid.server.hooks.modules.liveintent.omni.channel.identity.model.config.ModuleConfig;
3+
import org.prebid.server.hooks.modules.liveintent.omni.channel.identity.model.config.LiveIntentOmniChannelProperties;
44
import org.prebid.server.hooks.modules.liveintent.omni.channel.identity.v1.LiveIntentOmniChannelIdentityModule;
55
import org.prebid.server.hooks.modules.liveintent.omni.channel.identity.v1.hooks.LiveIntentOmniChannelIdentityProcessedAuctionRequestHook;
6-
import org.prebid.server.hooks.v1.Hook;
7-
import org.prebid.server.hooks.v1.InvocationContext;
86
import org.prebid.server.hooks.v1.Module;
97
import org.prebid.server.json.JacksonMapper;
108
import org.prebid.server.vertx.httpclient.HttpClient;
@@ -13,7 +11,7 @@
1311
import org.springframework.context.annotation.Bean;
1412
import org.springframework.context.annotation.Configuration;
1513

16-
import java.util.Set;
14+
import java.util.Collections;
1715
import java.util.concurrent.ThreadLocalRandom;
1816

1917
@Configuration
@@ -25,15 +23,19 @@ public class LiveIntentOmniChannelIdentityConfiguration {
2523

2624
@Bean
2725
@ConfigurationProperties(prefix = "hooks.modules." + LiveIntentOmniChannelIdentityModule.CODE)
28-
ModuleConfig moduleConfig() {
29-
return new ModuleConfig();
26+
LiveIntentOmniChannelProperties properties() {
27+
return new LiveIntentOmniChannelProperties();
3028
}
3129

3230
@Bean
33-
Module liveIntentOmniChannelIdentityModule(ModuleConfig config, JacksonMapper mapper, HttpClient httpClient) {
34-
final Set<? extends Hook<?, ? extends InvocationContext>> hooks = Set.of(
31+
Module liveIntentOmniChannelIdentityModule(LiveIntentOmniChannelProperties properties,
32+
JacksonMapper mapper,
33+
HttpClient httpClient) {
34+
35+
final LiveIntentOmniChannelIdentityProcessedAuctionRequestHook hook =
3536
new LiveIntentOmniChannelIdentityProcessedAuctionRequestHook(
36-
config, mapper, httpClient, () -> ThreadLocalRandom.current().nextLong()));
37-
return new LiveIntentOmniChannelIdentityModule(hooks);
37+
properties, mapper, httpClient, () -> ThreadLocalRandom.current().nextLong());
38+
39+
return new LiveIntentOmniChannelIdentityModule(Collections.singleton(hook));
3840
}
3941
}

extra/modules/live-intent-omni-channel-identity/src/main/java/org/prebid/server/hooks/modules/liveintent/omni/channel/identity/model/config/ModuleConfig.java renamed to extra/modules/live-intent-omni-channel-identity/src/main/java/org/prebid/server/hooks/modules/liveintent/omni/channel/identity/model/config/LiveIntentOmniChannelProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lombok.Data;
44

55
@Data
6-
public final class ModuleConfig {
6+
public final class LiveIntentOmniChannelProperties {
77

88
long requestTimeoutMs;
99

extra/modules/live-intent-omni-channel-identity/src/main/java/org/prebid/server/hooks/modules/liveintent/omni/channel/identity/v1/hooks/LiveIntentOmniChannelIdentityProcessedAuctionRequestHook.java

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.prebid.server.hooks.execution.v1.InvocationResultImpl;
99
import org.prebid.server.hooks.execution.v1.auction.AuctionRequestPayloadImpl;
1010
import org.prebid.server.hooks.modules.liveintent.omni.channel.identity.model.IdResResponse;
11-
import org.prebid.server.hooks.modules.liveintent.omni.channel.identity.model.config.ModuleConfig;
11+
import org.prebid.server.hooks.modules.liveintent.omni.channel.identity.model.config.LiveIntentOmniChannelProperties;
1212
import org.prebid.server.hooks.v1.InvocationAction;
1313
import org.prebid.server.hooks.v1.InvocationResult;
1414
import org.prebid.server.hooks.v1.InvocationStatus;
@@ -23,63 +23,53 @@
2323
import org.prebid.server.vertx.httpclient.HttpClient;
2424
import org.prebid.server.vertx.httpclient.model.HttpClientResponse;
2525

26-
import java.util.Collections;
2726
import java.util.List;
2827
import java.util.Objects;
2928
import java.util.Optional;
3029
import java.util.random.RandomGenerator;
3130

3231
public class LiveIntentOmniChannelIdentityProcessedAuctionRequestHook implements ProcessedAuctionRequestHook {
3332

34-
private static final Logger logger =
35-
LoggerFactory.getLogger(LiveIntentOmniChannelIdentityProcessedAuctionRequestHook.class);
33+
private static final Logger logger = LoggerFactory.getLogger(
34+
LiveIntentOmniChannelIdentityProcessedAuctionRequestHook.class);
3635
private static final String CODE = "liveintent-omni-channel-identity-enrichment-hook";
3736

38-
private final ModuleConfig config;
37+
private final LiveIntentOmniChannelProperties config;
3938
private final JacksonMapper mapper;
4039
private final HttpClient httpClient;
4140
private final RandomGenerator random;
4241

43-
public LiveIntentOmniChannelIdentityProcessedAuctionRequestHook(
44-
ModuleConfig config,
45-
JacksonMapper mapper,
46-
HttpClient httpClient,
47-
RandomGenerator random) {
42+
public LiveIntentOmniChannelIdentityProcessedAuctionRequestHook(LiveIntentOmniChannelProperties config,
43+
JacksonMapper mapper,
44+
HttpClient httpClient,
45+
RandomGenerator random) {
4846

4947
this.config = Objects.requireNonNull(config);
48+
//todo: maybe it's redundant, what do you think?
49+
HttpUtil.validateUrlSyntax(config.getIdentityResolutionEndpoint());
5050
this.mapper = Objects.requireNonNull(mapper);
5151
this.httpClient = Objects.requireNonNull(httpClient);
5252
this.random = Objects.requireNonNull(random);
5353
}
5454

5555
@Override
56-
public Future<InvocationResult<AuctionRequestPayload>> call(
57-
AuctionRequestPayload auctionRequestPayload,
58-
AuctionInvocationContext invocationContext) {
59-
if (random.nextFloat() < config.getTreatmentRate()) {
60-
return requestEnrichment(auctionRequestPayload)
61-
.<InvocationResult<AuctionRequestPayload>>map(resolutionResult ->
62-
InvocationResultImpl.<AuctionRequestPayload>builder()
63-
.status(InvocationStatus.success)
64-
.action(InvocationAction.update)
65-
.payloadUpdate(requestPayload -> updatedPayload(requestPayload, resolutionResult))
66-
.build())
67-
.onFailure(throwable -> logger.error("Failed enrichment:", throwable));
68-
}
69-
return Future.succeededFuture(
70-
InvocationResultImpl.<AuctionRequestPayload>builder()
71-
.status(InvocationStatus.success)
72-
.action(InvocationAction.no_action)
73-
.build());
56+
public Future<InvocationResult<AuctionRequestPayload>> call(AuctionRequestPayload auctionRequestPayload,
57+
AuctionInvocationContext invocationContext) {
58+
59+
return config.getTreatmentRate() <= random.nextFloat()
60+
? noAction()
61+
: requestIdentities(auctionRequestPayload.bidRequest())
62+
.<InvocationResult<AuctionRequestPayload>>map(this::update)
63+
//todo: is it find to just fail instead of rejection or no_action?
64+
.onFailure(throwable -> logger.error("Failed enrichment:", throwable));
7465

7566
}
7667

77-
private Future<IdResResponse> requestEnrichment(AuctionRequestPayload auctionRequestPayload) {
78-
final String bidRequestJson = mapper.encodeToString(auctionRequestPayload.bidRequest());
68+
private Future<IdResResponse> requestIdentities(BidRequest bidRequest) {
7969
return httpClient.post(
8070
config.getIdentityResolutionEndpoint(),
8171
headers(),
82-
bidRequestJson,
72+
mapper.encodeToString(bidRequest),
8373
config.getRequestTimeoutMs())
8474
.map(this::processResponse);
8575
}
@@ -89,23 +79,37 @@ private MultiMap headers() {
8979
.add(HttpUtil.AUTHORIZATION_HEADER, "Bearer " + config.getAuthToken());
9080
}
9181

82+
//todo: no status check and proper error code handling
9283
private IdResResponse processResponse(HttpClientResponse response) {
9384
return mapper.decodeValue(response.getBody(), IdResResponse.class);
9485
}
9586

96-
private AuctionRequestPayload updatedPayload(AuctionRequestPayload requestPayload, IdResResponse idResResponse) {
97-
final User user = Optional.ofNullable(
98-
requestPayload.bidRequest())
99-
.map(BidRequest::getUser)
100-
.orElse(User.builder().build());
87+
private static Future<InvocationResult<AuctionRequestPayload>> noAction() {
88+
return Future.succeededFuture(InvocationResultImpl.<AuctionRequestPayload>builder()
89+
.status(InvocationStatus.success)
90+
.action(InvocationAction.no_action)
91+
.build());
92+
}
10193

102-
final List<Eid> allEids = ListUtil.union(
103-
Optional.ofNullable(user.getEids()).orElse(Collections.emptyList()), idResResponse.getEids());
94+
private InvocationResultImpl<AuctionRequestPayload> update(IdResResponse resolutionResult) {
95+
return InvocationResultImpl.<AuctionRequestPayload>builder()
96+
.status(InvocationStatus.success)
97+
.action(InvocationAction.update)
98+
//todo: might eids be null? NPE is possible
99+
.payloadUpdate(payload -> updatedPayload(payload, resolutionResult.getEids()))
100+
.build();
101+
}
104102

105-
final User updatedUser = user.toBuilder().eids(allEids).build();
106-
final BidRequest updatedBidRequest = requestPayload.bidRequest().toBuilder().user(updatedUser).build();
103+
private AuctionRequestPayload updatedPayload(AuctionRequestPayload requestPayload, List<Eid> resolvedEids) {
104+
final BidRequest bidRequest = requestPayload.bidRequest();
105+
final User updatedUser = Optional.ofNullable(bidRequest.getUser())
106+
.map(user -> user.toBuilder().eids(user.getEids() == null
107+
? resolvedEids
108+
: ListUtil.union(user.getEids(), resolvedEids)))
109+
.orElseGet(() -> User.builder().eids(resolvedEids))
110+
.build();
107111

108-
return AuctionRequestPayloadImpl.of(updatedBidRequest);
112+
return AuctionRequestPayloadImpl.of(bidRequest.toBuilder().user(updatedUser).build());
109113
}
110114

111115
@Override

extra/modules/live-intent-omni-channel-identity/src/test/java/org/prebid/server/hooks/modules/liveintent/omni/channel/identity/model/config/IdResResponseTest.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

extra/modules/live-intent-omni-channel-identity/src/test/java/org/prebid/server/hooks/modules/liveintent/omni/channel/identity/model/config/ModuleConfigTest.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)