|
| 1 | +package org.prebid.server.hooks.modules.liveintent.omni.channel.identity.v1.hooks; |
| 2 | + |
| 3 | +import com.iab.openrtb.request.BidRequest; |
| 4 | +import com.iab.openrtb.request.Eid; |
| 5 | +import com.iab.openrtb.request.User; |
| 6 | +import io.vertx.core.Future; |
| 7 | +import io.vertx.core.MultiMap; |
| 8 | +import org.apache.commons.collections4.ListUtils; |
| 9 | +import org.prebid.server.hooks.execution.v1.InvocationResultImpl; |
| 10 | +import org.prebid.server.hooks.execution.v1.auction.AuctionRequestPayloadImpl; |
| 11 | +import org.prebid.server.hooks.modules.liveintent.omni.channel.identity.model.IdResResponse; |
| 12 | +import org.prebid.server.hooks.modules.liveintent.omni.channel.identity.model.config.LiveIntentOmniChannelProperties; |
| 13 | +import org.prebid.server.hooks.v1.InvocationAction; |
| 14 | +import org.prebid.server.hooks.v1.InvocationResult; |
| 15 | +import org.prebid.server.hooks.v1.InvocationStatus; |
| 16 | +import org.prebid.server.hooks.v1.auction.AuctionInvocationContext; |
| 17 | +import org.prebid.server.hooks.v1.auction.AuctionRequestPayload; |
| 18 | +import org.prebid.server.hooks.v1.auction.ProcessedAuctionRequestHook; |
| 19 | +import org.prebid.server.json.JacksonMapper; |
| 20 | +import org.prebid.server.log.ConditionalLogger; |
| 21 | +import org.prebid.server.log.LoggerFactory; |
| 22 | +import org.prebid.server.util.HttpUtil; |
| 23 | +import org.prebid.server.util.ListUtil; |
| 24 | +import org.prebid.server.vertx.httpclient.HttpClient; |
| 25 | +import org.prebid.server.vertx.httpclient.model.HttpClientResponse; |
| 26 | + |
| 27 | +import java.util.List; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.concurrent.ThreadLocalRandom; |
| 31 | + |
| 32 | +public class LiveIntentOmniChannelIdentityProcessedAuctionRequestHook implements ProcessedAuctionRequestHook { |
| 33 | + |
| 34 | + private static final ConditionalLogger conditionalLogger = new ConditionalLogger(LoggerFactory.getLogger( |
| 35 | + LiveIntentOmniChannelIdentityProcessedAuctionRequestHook.class)); |
| 36 | + |
| 37 | + private static final String CODE = "liveintent-omni-channel-identity-enrichment-hook"; |
| 38 | + |
| 39 | + private final LiveIntentOmniChannelProperties config; |
| 40 | + private final JacksonMapper mapper; |
| 41 | + private final HttpClient httpClient; |
| 42 | + private final double logSamplingRate; |
| 43 | + |
| 44 | + public LiveIntentOmniChannelIdentityProcessedAuctionRequestHook(LiveIntentOmniChannelProperties config, |
| 45 | + JacksonMapper mapper, |
| 46 | + HttpClient httpClient, |
| 47 | + double logSamplingRate) { |
| 48 | + |
| 49 | + this.config = Objects.requireNonNull(config); |
| 50 | + HttpUtil.validateUrlSyntax(config.getIdentityResolutionEndpoint()); |
| 51 | + this.mapper = Objects.requireNonNull(mapper); |
| 52 | + this.httpClient = Objects.requireNonNull(httpClient); |
| 53 | + this.logSamplingRate = logSamplingRate; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Future<InvocationResult<AuctionRequestPayload>> call(AuctionRequestPayload auctionRequestPayload, |
| 58 | + AuctionInvocationContext invocationContext) { |
| 59 | + |
| 60 | + return config.getTreatmentRate() > ThreadLocalRandom.current().nextFloat() |
| 61 | + ? requestIdentities(auctionRequestPayload.bidRequest()) |
| 62 | + .<InvocationResult<AuctionRequestPayload>>map(this::update) |
| 63 | + .onFailure(throwable -> conditionalLogger.error( |
| 64 | + "Failed enrichment: %s".formatted(throwable.getMessage()), logSamplingRate)) |
| 65 | + : noAction(); |
| 66 | + } |
| 67 | + |
| 68 | + private Future<IdResResponse> requestIdentities(BidRequest bidRequest) { |
| 69 | + return httpClient.post( |
| 70 | + config.getIdentityResolutionEndpoint(), |
| 71 | + headers(), |
| 72 | + mapper.encodeToString(bidRequest), |
| 73 | + config.getRequestTimeoutMs()) |
| 74 | + .map(this::processResponse); |
| 75 | + } |
| 76 | + |
| 77 | + private MultiMap headers() { |
| 78 | + return MultiMap.caseInsensitiveMultiMap() |
| 79 | + .add(HttpUtil.AUTHORIZATION_HEADER, "Bearer " + config.getAuthToken()); |
| 80 | + } |
| 81 | + |
| 82 | + private IdResResponse processResponse(HttpClientResponse response) { |
| 83 | + return mapper.decodeValue(response.getBody(), IdResResponse.class); |
| 84 | + } |
| 85 | + |
| 86 | + private static Future<InvocationResult<AuctionRequestPayload>> noAction() { |
| 87 | + return Future.succeededFuture(InvocationResultImpl.<AuctionRequestPayload>builder() |
| 88 | + .status(InvocationStatus.success) |
| 89 | + .action(InvocationAction.no_action) |
| 90 | + .build()); |
| 91 | + } |
| 92 | + |
| 93 | + private InvocationResultImpl<AuctionRequestPayload> update(IdResResponse resolutionResult) { |
| 94 | + return InvocationResultImpl.<AuctionRequestPayload>builder() |
| 95 | + .status(InvocationStatus.success) |
| 96 | + .action(InvocationAction.update) |
| 97 | + .payloadUpdate(payload -> updatedPayload(payload, resolutionResult.getEids())) |
| 98 | + .build(); |
| 99 | + } |
| 100 | + |
| 101 | + private AuctionRequestPayload updatedPayload(AuctionRequestPayload requestPayload, List<Eid> resolvedEids) { |
| 102 | + final List<Eid> eids = ListUtils.emptyIfNull(resolvedEids); |
| 103 | + final BidRequest bidRequest = requestPayload.bidRequest(); |
| 104 | + final User updatedUser = Optional.ofNullable(bidRequest.getUser()) |
| 105 | + .map(user -> user.toBuilder().eids(ListUtil.union(ListUtils.emptyIfNull(user.getEids()), eids))) |
| 106 | + .orElseGet(() -> User.builder().eids(eids)) |
| 107 | + .build(); |
| 108 | + |
| 109 | + return AuctionRequestPayloadImpl.of(bidRequest.toBuilder().user(updatedUser).build()); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String code() { |
| 114 | + return CODE; |
| 115 | + } |
| 116 | +} |
0 commit comments