88import org .prebid .server .hooks .execution .v1 .InvocationResultImpl ;
99import org .prebid .server .hooks .execution .v1 .auction .AuctionRequestPayloadImpl ;
1010import 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 ;
1212import org .prebid .server .hooks .v1 .InvocationAction ;
1313import org .prebid .server .hooks .v1 .InvocationResult ;
1414import org .prebid .server .hooks .v1 .InvocationStatus ;
2323import org .prebid .server .vertx .httpclient .HttpClient ;
2424import org .prebid .server .vertx .httpclient .model .HttpClientResponse ;
2525
26- import java .util .Collections ;
2726import java .util .List ;
2827import java .util .Objects ;
2928import java .util .Optional ;
3029import java .util .random .RandomGenerator ;
3130
3231public 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
0 commit comments