|
| 1 | +package org.prebid.server.bidder.sparteo; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 5 | +import com.fasterxml.jackson.databind.JsonNode; |
| 6 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 7 | +import com.iab.openrtb.request.BidRequest; |
| 8 | +import com.iab.openrtb.request.Imp; |
| 9 | +import com.iab.openrtb.request.Publisher; |
| 10 | +import com.iab.openrtb.request.Site; |
| 11 | +import com.iab.openrtb.response.Bid; |
| 12 | +import com.iab.openrtb.response.BidResponse; |
| 13 | +import com.iab.openrtb.response.SeatBid; |
| 14 | +import org.apache.commons.collections4.CollectionUtils; |
| 15 | +import org.prebid.server.bidder.Bidder; |
| 16 | +import org.prebid.server.bidder.model.BidderBid; |
| 17 | +import org.prebid.server.bidder.model.BidderCall; |
| 18 | +import org.prebid.server.bidder.model.BidderError; |
| 19 | +import org.prebid.server.bidder.model.HttpRequest; |
| 20 | +import org.prebid.server.bidder.model.Result; |
| 21 | +import org.prebid.server.exception.PreBidException; |
| 22 | +import org.prebid.server.json.DecodeException; |
| 23 | +import org.prebid.server.json.JacksonMapper; |
| 24 | +import org.prebid.server.proto.openrtb.ext.ExtPrebid; |
| 25 | +import org.prebid.server.proto.openrtb.ext.request.ExtPublisher; |
| 26 | +import org.prebid.server.proto.openrtb.ext.request.sparteo.ExtImpSparteo; |
| 27 | +import org.prebid.server.proto.openrtb.ext.response.BidType; |
| 28 | +import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid; |
| 29 | +import org.prebid.server.util.BidderUtil; |
| 30 | +import org.prebid.server.util.HttpUtil; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Collection; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Objects; |
| 37 | +import java.util.Optional; |
| 38 | + |
| 39 | +public class SparteoBidder implements Bidder<BidRequest> { |
| 40 | + |
| 41 | + private static final TypeReference<ExtPrebid<?, ExtImpSparteo>> TYPE_REFERENCE = |
| 42 | + new TypeReference<>() { }; |
| 43 | + |
| 44 | + private final String endpointUrl; |
| 45 | + private final JacksonMapper mapper; |
| 46 | + |
| 47 | + public SparteoBidder(String endpointUrl, JacksonMapper mapper) { |
| 48 | + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); |
| 49 | + this.mapper = Objects.requireNonNull(mapper); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) { |
| 54 | + final List<BidderError> errors = new ArrayList<>(); |
| 55 | + final List<Imp> modifiedImps = new ArrayList<>(); |
| 56 | + String siteNetworkId = null; |
| 57 | + |
| 58 | + for (Imp imp : request.getImp()) { |
| 59 | + if (siteNetworkId == null) { |
| 60 | + try { |
| 61 | + siteNetworkId = parseExtImp(imp).getNetworkId(); |
| 62 | + } catch (PreBidException e) { |
| 63 | + errors.add(BidderError.badInput( |
| 64 | + "ignoring imp id=%s, error processing ext: %s".formatted( |
| 65 | + imp.getId(), e.getMessage()))); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + final ObjectNode modifiedExt = modifyImpExt(imp); |
| 70 | + modifiedImps.add(imp.toBuilder().ext(modifiedExt).build()); |
| 71 | + } |
| 72 | + |
| 73 | + if (modifiedImps.isEmpty()) { |
| 74 | + return Result.withErrors(errors); |
| 75 | + } |
| 76 | + |
| 77 | + final BidRequest outgoingRequest = request.toBuilder() |
| 78 | + .imp(modifiedImps) |
| 79 | + .site(modifySite(request.getSite(), siteNetworkId, mapper)) |
| 80 | + .build(); |
| 81 | + |
| 82 | + final HttpRequest<BidRequest> call = BidderUtil.defaultRequest(outgoingRequest, endpointUrl, mapper); |
| 83 | + |
| 84 | + return Result.of(Collections.singletonList(call), errors); |
| 85 | + } |
| 86 | + |
| 87 | + private ExtImpSparteo parseExtImp(Imp imp) { |
| 88 | + try { |
| 89 | + return mapper.mapper().convertValue(imp.getExt(), TYPE_REFERENCE).getBidder(); |
| 90 | + } catch (IllegalArgumentException e) { |
| 91 | + throw new PreBidException("invalid imp.ext"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static ObjectNode modifyImpExt(Imp imp) { |
| 96 | + final ObjectNode modifiedImpExt = imp.getExt().deepCopy(); |
| 97 | + final ObjectNode sparteoNode = modifiedImpExt.putObject("sparteo"); |
| 98 | + final JsonNode bidderJsonNode = modifiedImpExt.remove("bidder"); |
| 99 | + sparteoNode.set("params", bidderJsonNode); |
| 100 | + |
| 101 | + return modifiedImpExt; |
| 102 | + } |
| 103 | + |
| 104 | + private Site modifySite(Site site, String siteNetworkId, JacksonMapper mapper) { |
| 105 | + if (site == null || site.getPublisher() == null || siteNetworkId == null) { |
| 106 | + return site; |
| 107 | + } |
| 108 | + |
| 109 | + final Publisher originalPublisher = site.getPublisher(); |
| 110 | + final ExtPublisher originalExt = originalPublisher.getExt(); |
| 111 | + |
| 112 | + final ExtPublisher modifiedExt = originalExt != null |
| 113 | + ? ExtPublisher.of(originalExt.getPrebid()) |
| 114 | + : ExtPublisher.empty(); |
| 115 | + |
| 116 | + if (originalExt != null) { |
| 117 | + mapper.fillExtension(modifiedExt, originalExt); |
| 118 | + } |
| 119 | + |
| 120 | + final JsonNode paramsProperty = modifiedExt.getProperty("params"); |
| 121 | + final ObjectNode paramsNode; |
| 122 | + |
| 123 | + if (paramsProperty != null && paramsProperty.isObject()) { |
| 124 | + paramsNode = (ObjectNode) paramsProperty; |
| 125 | + } else { |
| 126 | + paramsNode = mapper.mapper().createObjectNode(); |
| 127 | + modifiedExt.addProperty("params", paramsNode); |
| 128 | + } |
| 129 | + |
| 130 | + paramsNode.put("networkId", siteNetworkId); |
| 131 | + |
| 132 | + final Publisher modifiedPublisher = originalPublisher.toBuilder() |
| 133 | + .ext(modifiedExt) |
| 134 | + .build(); |
| 135 | + |
| 136 | + return site.toBuilder() |
| 137 | + .publisher(modifiedPublisher) |
| 138 | + .build(); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { |
| 143 | + try { |
| 144 | + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); |
| 145 | + final List<BidderError> errors = new ArrayList<>(); |
| 146 | + return Result.of(extractBids(bidResponse, errors), errors); |
| 147 | + } catch (DecodeException e) { |
| 148 | + return Result.withError(BidderError.badServerResponse(e.getMessage())); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private List<BidderBid> extractBids(BidResponse bidResponse, List<BidderError> errors) { |
| 153 | + if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { |
| 154 | + return Collections.emptyList(); |
| 155 | + } |
| 156 | + |
| 157 | + return bidResponse.getSeatbid().stream() |
| 158 | + .filter(Objects::nonNull) |
| 159 | + .map(SeatBid::getBid) |
| 160 | + .filter(Objects::nonNull) |
| 161 | + .flatMap(Collection::stream) |
| 162 | + .filter(Objects::nonNull) |
| 163 | + .map(bid -> toBidderBid(bid, bidResponse.getCur(), errors)) |
| 164 | + .filter(Objects::nonNull) |
| 165 | + .toList(); |
| 166 | + } |
| 167 | + |
| 168 | + private BidderBid toBidderBid(Bid bid, String currency, List<BidderError> errors) { |
| 169 | + try { |
| 170 | + final BidType bidType = getBidType(bid); |
| 171 | + |
| 172 | + final Integer mtype = switch (bidType) { |
| 173 | + case banner -> 1; |
| 174 | + case video -> 2; |
| 175 | + case xNative -> 4; |
| 176 | + default -> null; |
| 177 | + }; |
| 178 | + |
| 179 | + final Bid bidWithMtype = mtype != null ? bid.toBuilder().mtype(mtype).build() : bid; |
| 180 | + |
| 181 | + return BidderBid.of(bidWithMtype, bidType, currency); |
| 182 | + } catch (PreBidException e) { |
| 183 | + errors.add(BidderError.badServerResponse(e.getMessage())); |
| 184 | + return null; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private BidType getBidType(Bid bid) throws PreBidException { |
| 189 | + final BidType bidType = Optional.ofNullable(bid.getExt()) |
| 190 | + .map(ext -> ext.get("prebid")) |
| 191 | + .filter(JsonNode::isObject) |
| 192 | + .map(this::parseExtBidPrebid) |
| 193 | + .map(ExtBidPrebid::getType) |
| 194 | + .orElseThrow(() -> new PreBidException( |
| 195 | + "Failed to parse bid mediatype for impression \"%s\"".formatted(bid.getImpid()))); |
| 196 | + |
| 197 | + if (bidType == BidType.audio) { |
| 198 | + throw new PreBidException( |
| 199 | + "Audio bid type not supported by this adapter for impression id: %s".formatted(bid.getImpid())); |
| 200 | + } |
| 201 | + |
| 202 | + return bidType; |
| 203 | + } |
| 204 | + |
| 205 | + private ExtBidPrebid parseExtBidPrebid(JsonNode prebidNode) { |
| 206 | + try { |
| 207 | + return mapper.mapper().treeToValue(prebidNode, ExtBidPrebid.class); |
| 208 | + } catch (JsonProcessingException e) { |
| 209 | + return null; |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments