|
| 1 | +package org.prebid.server.bidder.showheroes; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 4 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | +import com.iab.openrtb.request.Banner; |
| 7 | +import com.iab.openrtb.request.BidRequest; |
| 8 | +import com.iab.openrtb.request.Imp; |
| 9 | +import com.iab.openrtb.request.Video; |
| 10 | +import com.iab.openrtb.response.Bid; |
| 11 | +import com.iab.openrtb.response.BidResponse; |
| 12 | +import com.iab.openrtb.response.SeatBid; |
| 13 | +import org.apache.commons.collections4.CollectionUtils; |
| 14 | +import org.prebid.server.bidder.Bidder; |
| 15 | +import org.prebid.server.bidder.model.BidderBid; |
| 16 | +import org.prebid.server.bidder.model.BidderCall; |
| 17 | +import org.prebid.server.bidder.model.BidderError; |
| 18 | +import org.prebid.server.bidder.model.HttpRequest; |
| 19 | +import org.prebid.server.bidder.model.Result; |
| 20 | +import org.prebid.server.currency.CurrencyConversionService; |
| 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.ExtRequest; |
| 26 | +import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid; |
| 27 | +import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidChannel; |
| 28 | +import org.prebid.server.proto.openrtb.ext.request.showheroes.ExtImpShowheroes; |
| 29 | +import org.prebid.server.proto.openrtb.ext.response.BidType; |
| 30 | +import org.prebid.server.util.BidderUtil; |
| 31 | +import org.prebid.server.util.HttpUtil; |
| 32 | +import org.prebid.server.version.PrebidVersionProvider; |
| 33 | + |
| 34 | +import java.math.BigDecimal; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.Collection; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Objects; |
| 40 | +import java.util.Optional; |
| 41 | + |
| 42 | +public class ShowheroesBidder implements Bidder<BidRequest> { |
| 43 | + |
| 44 | + private static final String BID_CURRENCY = "EUR"; |
| 45 | + private static final String DEFAULT_ORTB_CURRENCY = "USD"; |
| 46 | + private static final TypeReference<ExtPrebid<?, ExtImpShowheroes>> SHOWHEROES_EXT_TYPE_REFERENCE = |
| 47 | + new TypeReference<>() { |
| 48 | + }; |
| 49 | + |
| 50 | + private final String endpointUrl; |
| 51 | + private final CurrencyConversionService currencyConversionService; |
| 52 | + private final JacksonMapper mapper; |
| 53 | + |
| 54 | + public ShowheroesBidder(String endpointUrl, |
| 55 | + CurrencyConversionService currencyConversionService, |
| 56 | + PrebidVersionProvider prebidVersionProvider, |
| 57 | + JacksonMapper mapper) { |
| 58 | + |
| 59 | + this.endpointUrl = HttpUtil.validateUrlSyntax(Objects.requireNonNull(endpointUrl)); |
| 60 | + this.currencyConversionService = Objects.requireNonNull(currencyConversionService); |
| 61 | + this.mapper = Objects.requireNonNull(mapper); |
| 62 | + } |
| 63 | + |
| 64 | + private BidderError validate(BidRequest bidRequest) { |
| 65 | + // request must contain site object with page or app object with bundle |
| 66 | + if (bidRequest.getSite() == null && bidRequest.getApp() == null) { |
| 67 | + return BidderError.badInput("BidRequest must contain one of site or app"); |
| 68 | + } |
| 69 | + if (bidRequest.getSite() != null && bidRequest.getSite().getPage() == null) { |
| 70 | + return BidderError.badInput("BidRequest.site.page is required"); |
| 71 | + } |
| 72 | + if (bidRequest.getApp() != null && bidRequest.getApp().getBundle() == null) { |
| 73 | + return BidderError.badInput("BidRequest.app.bundle is required"); |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + private ExtRequestPrebidChannel getPrebidChannel(BidRequest bidRequest) { |
| 79 | + return Optional.ofNullable(bidRequest.getExt()) |
| 80 | + .map(ExtRequest::getPrebid) |
| 81 | + .map(ExtRequestPrebid::getChannel) |
| 82 | + .orElse(null); |
| 83 | + } |
| 84 | + |
| 85 | + private Imp processImpression(BidRequest bidRequest, Imp imp, ExtRequestPrebidChannel prebidChannel) { |
| 86 | + final Banner banner = imp.getBanner(); |
| 87 | + final Video video = imp.getVideo(); |
| 88 | + if (banner == null && video == null) { |
| 89 | + throw new PreBidException("Impression must contain one of banner, video or native"); |
| 90 | + } |
| 91 | + |
| 92 | + final ExtImpShowheroes extImpShowheroes = mapper.mapper() |
| 93 | + .convertValue(imp.getExt(), SHOWHEROES_EXT_TYPE_REFERENCE).getBidder(); |
| 94 | + if (extImpShowheroes == null || extImpShowheroes.getUnitId() == null |
| 95 | + || extImpShowheroes.getUnitId().isBlank()) { |
| 96 | + throw new PreBidException("Ext.imp.bidder.unitId is required"); |
| 97 | + } |
| 98 | + |
| 99 | + String channelName = null; |
| 100 | + String channelVersion = null; |
| 101 | + if (prebidChannel != null) { |
| 102 | + channelName = prebidChannel.getName(); |
| 103 | + channelVersion = prebidChannel.getVersion(); |
| 104 | + } |
| 105 | + |
| 106 | + final ObjectNode impExt = imp.getExt(); |
| 107 | + |
| 108 | + final Imp.ImpBuilder impBuilder = imp.toBuilder(); |
| 109 | + |
| 110 | + // copy unitId from ext.bidder to ext.params |
| 111 | + impExt.set("params", JsonNodeFactory.instance.objectNode() |
| 112 | + .put("unitId", extImpShowheroes.getUnitId())); |
| 113 | + |
| 114 | + impBuilder.ext(impExt); |
| 115 | + if (imp.getDisplaymanager() == null && channelName != null) { |
| 116 | + impBuilder.displaymanager(channelName); |
| 117 | + impBuilder.displaymanagerver(channelVersion); |
| 118 | + } |
| 119 | + |
| 120 | + String currency = imp.getBidfloorcur(); |
| 121 | + // if floor price is 0, or currency is EUR - no need to convert |
| 122 | + if (imp.getBidfloor() == null || imp.getBidfloor().compareTo(BigDecimal.ZERO) == 0 |
| 123 | + || currency == BID_CURRENCY) { |
| 124 | + return impBuilder.build(); |
| 125 | + } |
| 126 | + if (currency != null && !currency.isBlank()) { |
| 127 | + // if not provided default currency is USD |
| 128 | + currency = DEFAULT_ORTB_CURRENCY; |
| 129 | + } |
| 130 | + |
| 131 | + final BigDecimal eurFloor = currencyConversionService.convertCurrency( |
| 132 | + imp.getBidfloor(), bidRequest, currency, BID_CURRENCY); |
| 133 | + return impBuilder |
| 134 | + .bidfloorcur(BID_CURRENCY) |
| 135 | + .bidfloor(eurFloor) |
| 136 | + .build(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) { |
| 141 | + final BidderError validationError = validate(request); |
| 142 | + if (validationError != null) { |
| 143 | + return Result.of(Collections.emptyList(), List.of(validationError)); |
| 144 | + } |
| 145 | + |
| 146 | + final List<BidderError> errors = new ArrayList<>(); |
| 147 | + final List<HttpRequest<BidRequest>> httpRequests = new ArrayList<>(); |
| 148 | + |
| 149 | + final ExtRequestPrebidChannel prebidChannel = getPrebidChannel(request); |
| 150 | + final List<Imp> modifiedImps = new ArrayList<>(request.getImp().size()); |
| 151 | + |
| 152 | + for (Imp impression : request.getImp()) { |
| 153 | + try { |
| 154 | + modifiedImps.add(processImpression(request, impression, prebidChannel)); |
| 155 | + } catch (Exception e) { |
| 156 | + errors.add(BidderError.badInput(e.getMessage())); |
| 157 | + continue; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + httpRequests.add(makeHttpRequest(request.toBuilder().imp(modifiedImps).build())); |
| 162 | + return Result.of(httpRequests, errors); |
| 163 | + } |
| 164 | + |
| 165 | + private HttpRequest<BidRequest> makeHttpRequest(BidRequest request) { |
| 166 | + return BidderUtil.defaultRequest(request, endpointUrl, mapper); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { |
| 171 | + final BidResponse bidResponse; |
| 172 | + final int statusCode = httpCall.getResponse().getStatusCode(); |
| 173 | + if (statusCode == 204) { |
| 174 | + return Result.of(Collections.emptyList(), Collections.emptyList()); |
| 175 | + } |
| 176 | + if (statusCode != 200) { |
| 177 | + return Result.withError(BidderError.badServerResponse( |
| 178 | + "Unexpected status code: " + statusCode)); |
| 179 | + } |
| 180 | + |
| 181 | + try { |
| 182 | + bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); |
| 183 | + } catch (DecodeException | PreBidException e) { |
| 184 | + return Result.withError(BidderError.badServerResponse(e.getMessage())); |
| 185 | + } |
| 186 | + |
| 187 | + return Result.of(extractBids(bidResponse), Collections.emptyList()); |
| 188 | + } |
| 189 | + |
| 190 | + private List<BidderBid> extractBids(BidResponse bidResponse) { |
| 191 | + if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { |
| 192 | + return Collections.emptyList(); |
| 193 | + } |
| 194 | + |
| 195 | + return bidResponse.getSeatbid().stream() |
| 196 | + .filter(Objects::nonNull) |
| 197 | + .map(SeatBid::getBid) |
| 198 | + .filter(Objects::nonNull) |
| 199 | + .flatMap(Collection::stream) |
| 200 | + .filter(Objects::nonNull) |
| 201 | + .map(bid -> BidderBid.of(bid, getBidType(bid), bidResponse.getCur())) |
| 202 | + .filter(Objects::nonNull) |
| 203 | + .toList(); |
| 204 | + } |
| 205 | + |
| 206 | + private BidType getBidType(Bid bid) { |
| 207 | + return switch (bid.getMtype()) { |
| 208 | + case 1 -> BidType.banner; |
| 209 | + case 2 -> BidType.video; |
| 210 | + default -> BidType.video; // if not provided video is assumed |
| 211 | + }; |
| 212 | + } |
| 213 | +} |
0 commit comments