|
| 1 | +package org.prebid.server.bidder.zeta_global_ssp; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 4 | +import com.iab.openrtb.request.BidRequest; |
| 5 | +import com.iab.openrtb.request.Imp; |
| 6 | +import com.iab.openrtb.response.Bid; |
| 7 | +import com.iab.openrtb.response.BidResponse; |
| 8 | +import com.iab.openrtb.response.SeatBid; |
| 9 | +import org.prebid.server.bidder.Bidder; |
| 10 | +import org.prebid.server.bidder.model.BidderBid; |
| 11 | +import org.prebid.server.bidder.model.BidderCall; |
| 12 | +import org.prebid.server.bidder.model.BidderError; |
| 13 | +import org.prebid.server.bidder.model.HttpRequest; |
| 14 | +import org.prebid.server.bidder.model.Result; |
| 15 | +import org.prebid.server.exception.PreBidException; |
| 16 | +import org.prebid.server.json.DecodeException; |
| 17 | +import org.prebid.server.json.JacksonMapper; |
| 18 | +import org.prebid.server.proto.openrtb.ext.ExtPrebid; |
| 19 | +import org.prebid.server.proto.openrtb.ext.request.zeta_global_ssp.ExtImpZetaGlobalSSP; |
| 20 | +import org.prebid.server.proto.openrtb.ext.response.BidType; |
| 21 | +import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid; |
| 22 | +import org.prebid.server.util.BidderUtil; |
| 23 | +import org.prebid.server.util.HttpUtil; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Objects; |
| 30 | +import java.util.Optional; |
| 31 | + |
| 32 | +public class ZetaGlobalSspBidder implements Bidder<BidRequest> { |
| 33 | + |
| 34 | + private static final TypeReference<ExtPrebid<?, ExtImpZetaGlobalSSP>> ZETA_GLOBAL_EXT_TYPE_REFERENCE = |
| 35 | + new TypeReference<>() { |
| 36 | + }; |
| 37 | + |
| 38 | + private static final TypeReference<ExtPrebid<ExtBidPrebid, ?>> EXT_BID_TYPE_REFERENCE = |
| 39 | + new TypeReference<>() { |
| 40 | + }; |
| 41 | + private static final String SID_MACRO = "{{AccountID}}"; |
| 42 | + |
| 43 | + private final String endpointUrl; |
| 44 | + private final JacksonMapper mapper; |
| 45 | + |
| 46 | + public ZetaGlobalSspBidder(String endpointUrl, JacksonMapper mapper) { |
| 47 | + this.endpointUrl = HttpUtil.validateUrl(endpointUrl); |
| 48 | + this.mapper = Objects.requireNonNull(mapper); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) { |
| 53 | + final Imp firstImp = request.getImp().getFirst(); |
| 54 | + final ExtImpZetaGlobalSSP extImp; |
| 55 | + |
| 56 | + try { |
| 57 | + extImp = parseImpExt(firstImp); |
| 58 | + } catch (PreBidException e) { |
| 59 | + return Result.withError(BidderError.badInput(e.getMessage())); |
| 60 | + } |
| 61 | + |
| 62 | + final HttpRequest<BidRequest> httpRequest = BidderUtil.defaultRequest( |
| 63 | + removeImpsExt(request), |
| 64 | + resolveEndpoint(extImp), |
| 65 | + mapper); |
| 66 | + |
| 67 | + return Result.withValues(Collections.singletonList(httpRequest)); |
| 68 | + } |
| 69 | + |
| 70 | + private ExtImpZetaGlobalSSP parseImpExt(Imp imp) { |
| 71 | + try { |
| 72 | + return mapper.mapper().convertValue(imp.getExt(), ZETA_GLOBAL_EXT_TYPE_REFERENCE).getBidder(); |
| 73 | + } catch (IllegalArgumentException e) { |
| 74 | + throw new PreBidException("Missing bidder ext in impression with id: " + imp.getId()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private String resolveEndpoint(ExtImpZetaGlobalSSP extImpZetaGlobalSSP) { |
| 79 | + return endpointUrl |
| 80 | + .replace(SID_MACRO, Objects.toString(extImpZetaGlobalSSP.getSid(), "0")); |
| 81 | + } |
| 82 | + |
| 83 | + private BidRequest removeImpsExt(BidRequest request) { |
| 84 | + final List<Imp> imps = new ArrayList<>(request.getImp()); |
| 85 | + final Imp firstImp = imps.getFirst().toBuilder().ext(null).build(); |
| 86 | + imps.set(0, firstImp); |
| 87 | + |
| 88 | + return request.toBuilder() |
| 89 | + .imp(imps) |
| 90 | + .build(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { |
| 95 | + try { |
| 96 | + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); |
| 97 | + final List<BidderError> errors = new ArrayList<>(); |
| 98 | + return Result.of(extractBids(bidResponse, errors), errors); |
| 99 | + } catch (DecodeException | PreBidException e) { |
| 100 | + return Result.withError(BidderError.badServerResponse(e.getMessage())); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private List<BidderBid> extractBids(BidResponse bidResponse, List<BidderError> errors) { |
| 105 | + if (bidResponse == null || bidResponse.getSeatbid() == null) { |
| 106 | + return Collections.emptyList(); |
| 107 | + } |
| 108 | + return bidsFromResponse(bidResponse, errors); |
| 109 | + } |
| 110 | + |
| 111 | + private List<BidderBid> bidsFromResponse(BidResponse bidResponse, List<BidderError> errors) { |
| 112 | + return bidResponse.getSeatbid().stream() |
| 113 | + .filter(Objects::nonNull) |
| 114 | + .map(SeatBid::getBid) |
| 115 | + .filter(Objects::nonNull) |
| 116 | + .flatMap(Collection::stream) |
| 117 | + .filter(Objects::nonNull) |
| 118 | + .map(bid -> makeBid(bid, bidResponse.getCur(), errors)) |
| 119 | + .filter(Objects::nonNull) |
| 120 | + .toList(); |
| 121 | + } |
| 122 | + |
| 123 | + private BidderBid makeBid(Bid bid, String currency, List<BidderError> errors) { |
| 124 | + final BidType mediaType = getMediaType(bid, errors); |
| 125 | + return mediaType == null ? null : BidderBid.of(bid, mediaType, currency); |
| 126 | + } |
| 127 | + |
| 128 | + private BidType getMediaType(Bid bid, List<BidderError> errors) { |
| 129 | + try { |
| 130 | + return Optional.ofNullable(bid.getExt()) |
| 131 | + .map(ext -> mapper.mapper().convertValue(ext, EXT_BID_TYPE_REFERENCE)) |
| 132 | + .map(ExtPrebid::getPrebid) |
| 133 | + .map(ExtBidPrebid::getType) |
| 134 | + .orElseThrow(IllegalArgumentException::new); |
| 135 | + } catch (IllegalArgumentException e) { |
| 136 | + errors.add(BidderError.badServerResponse( |
| 137 | + "Failed to parse impression \"%s\" mediatype".formatted(bid.getImpid()))); |
| 138 | + return null; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments