|
| 1 | +package org.prebid.server.bidder.nexx360; |
| 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.apache.commons.collections4.CollectionUtils; |
| 10 | +import org.apache.commons.lang3.StringUtils; |
| 11 | +import org.apache.http.client.utils.URIBuilder; |
| 12 | +import org.prebid.server.bidder.Bidder; |
| 13 | +import org.prebid.server.bidder.model.BidderBid; |
| 14 | +import org.prebid.server.bidder.model.BidderCall; |
| 15 | +import org.prebid.server.bidder.model.BidderError; |
| 16 | +import org.prebid.server.bidder.model.HttpRequest; |
| 17 | +import org.prebid.server.bidder.model.Result; |
| 18 | +import org.prebid.server.exception.PreBidException; |
| 19 | +import org.prebid.server.json.DecodeException; |
| 20 | +import org.prebid.server.json.JacksonMapper; |
| 21 | +import org.prebid.server.proto.openrtb.ext.ExtPrebid; |
| 22 | +import org.prebid.server.proto.openrtb.ext.request.ExtRequest; |
| 23 | +import org.prebid.server.proto.openrtb.ext.request.nexx360.ExtImpNexx360; |
| 24 | +import org.prebid.server.proto.openrtb.ext.response.BidType; |
| 25 | +import org.prebid.server.util.BidderUtil; |
| 26 | +import org.prebid.server.util.HttpUtil; |
| 27 | +import org.prebid.server.version.PrebidVersionProvider; |
| 28 | + |
| 29 | +import java.net.URISyntaxException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Collection; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Objects; |
| 35 | +import java.util.stream.Collectors; |
| 36 | + |
| 37 | +public class Nexx360Bidder implements Bidder<BidRequest> { |
| 38 | + |
| 39 | + private static final TypeReference<ExtPrebid<?, ExtImpNexx360>> TYPE_REFERENCE = new TypeReference<>() { |
| 40 | + }; |
| 41 | + private static final String BIDDER_NAME = "nexx360"; |
| 42 | + |
| 43 | + private final String endpointUrl; |
| 44 | + private final JacksonMapper mapper; |
| 45 | + private final PrebidVersionProvider prebidVersionProvider; |
| 46 | + |
| 47 | + public Nexx360Bidder(String endpointUrl, JacksonMapper mapper, PrebidVersionProvider prebidVersionProvider) { |
| 48 | + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); |
| 49 | + this.mapper = Objects.requireNonNull(mapper); |
| 50 | + this.prebidVersionProvider = Objects.requireNonNull(prebidVersionProvider); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) { |
| 55 | + final List<Imp> imps = request.getImp(); |
| 56 | + final List<Imp> modifiedImps = new ArrayList<>(); |
| 57 | + |
| 58 | + final ExtImpNexx360 firstExtImp; |
| 59 | + try { |
| 60 | + firstExtImp = parseImpExt(imps.getFirst()); |
| 61 | + } catch (PreBidException e) { |
| 62 | + return Result.withError(BidderError.badInput(e.getMessage())); |
| 63 | + } |
| 64 | + |
| 65 | + for (final Imp imp : imps) { |
| 66 | + modifiedImps.add(modifyImp(imp)); |
| 67 | + } |
| 68 | + |
| 69 | + final BidRequest modifiedRequest = makeRequest(request, modifiedImps); |
| 70 | + final String url = makeUrl(firstExtImp.getTagId(), firstExtImp.getPlacement()); |
| 71 | + return Result.withValue(BidderUtil.defaultRequest(modifiedRequest, url, mapper)); |
| 72 | + } |
| 73 | + |
| 74 | + private ExtImpNexx360 parseImpExt(Imp imp) { |
| 75 | + try { |
| 76 | + return mapper.mapper().convertValue(imp.getExt(), TYPE_REFERENCE).getBidder(); |
| 77 | + } catch (IllegalArgumentException e) { |
| 78 | + throw new PreBidException(e.getMessage()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private Imp modifyImp(Imp imp) { |
| 83 | + return imp.toBuilder() |
| 84 | + .ext(mapper.mapper().createObjectNode().set(BIDDER_NAME, imp.getExt().get("bidder"))) |
| 85 | + .build(); |
| 86 | + } |
| 87 | + |
| 88 | + private BidRequest makeRequest(BidRequest request, List<Imp> imps) { |
| 89 | + final ExtRequest extRequest = ExtRequest.empty(); |
| 90 | + extRequest.addProperty(BIDDER_NAME, mapper.mapper().valueToTree( |
| 91 | + Nexx360ExtRequest.of(Nexx360ExtRequestCaller.of(prebidVersionProvider.getNameVersionRecord())))); |
| 92 | + |
| 93 | + return request.toBuilder() |
| 94 | + .imp(imps) |
| 95 | + .ext(extRequest) |
| 96 | + .build(); |
| 97 | + } |
| 98 | + |
| 99 | + private String makeUrl(String tagId, String placement) { |
| 100 | + final URIBuilder uriBuilder; |
| 101 | + try { |
| 102 | + uriBuilder = new URIBuilder(endpointUrl); |
| 103 | + } catch (URISyntaxException e) { |
| 104 | + throw new PreBidException("Invalid url: %s, error: %s".formatted(endpointUrl, e.getMessage())); |
| 105 | + } |
| 106 | + |
| 107 | + if (StringUtils.isNotBlank(placement)) { |
| 108 | + uriBuilder.addParameter("placement", placement); |
| 109 | + } |
| 110 | + if (StringUtils.isNotBlank(tagId)) { |
| 111 | + uriBuilder.addParameter("tag_id", tagId); |
| 112 | + } |
| 113 | + |
| 114 | + return uriBuilder.toString(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { |
| 119 | + try { |
| 120 | + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); |
| 121 | + final List<BidderError> errors = new ArrayList<>(); |
| 122 | + return Result.of(extractBids(bidResponse, errors), errors); |
| 123 | + } catch (DecodeException e) { |
| 124 | + return Result.withError(BidderError.badServerResponse(e.getMessage())); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private List<BidderBid> extractBids(BidResponse bidResponse, List<BidderError> errors) { |
| 129 | + if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { |
| 130 | + return Collections.emptyList(); |
| 131 | + } |
| 132 | + return bidsFromResponse(bidResponse, errors); |
| 133 | + } |
| 134 | + |
| 135 | + private List<BidderBid> bidsFromResponse(BidResponse bidResponse, List<BidderError> errors) { |
| 136 | + return bidResponse.getSeatbid().stream() |
| 137 | + .filter(Objects::nonNull) |
| 138 | + .map(SeatBid::getBid) |
| 139 | + .filter(Objects::nonNull) |
| 140 | + .flatMap(Collection::stream) |
| 141 | + .filter(Objects::nonNull) |
| 142 | + .map(bid -> makeBid(bid, bidResponse.getCur(), errors)) |
| 143 | + .filter(Objects::nonNull) |
| 144 | + .collect(Collectors.toList()); |
| 145 | + } |
| 146 | + |
| 147 | + private BidderBid makeBid(Bid bid, String currency, List<BidderError> errors) { |
| 148 | + try { |
| 149 | + return BidderBid.of(bid, getBidType(bid), currency); |
| 150 | + } catch (PreBidException e) { |
| 151 | + errors.add(BidderError.badServerResponse(e.getMessage())); |
| 152 | + return null; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private BidType getBidType(Bid bid) { |
| 157 | + final String bidType; |
| 158 | + try { |
| 159 | + bidType = mapper.mapper() |
| 160 | + .convertValue(bid.getExt(), Nexx360ExtBid.class) |
| 161 | + .getBidType(); |
| 162 | + } catch (IllegalArgumentException e) { |
| 163 | + throw new PreBidException( |
| 164 | + "unable to fetch mediaType in multi-format: " + bid.getImpid()); |
| 165 | + } |
| 166 | + |
| 167 | + return switch (bidType) { |
| 168 | + case "banner" -> BidType.banner; |
| 169 | + case "video" -> BidType.video; |
| 170 | + case "audio" -> BidType.audio; |
| 171 | + case "native" -> BidType.xNative; |
| 172 | + default -> throw new PreBidException( |
| 173 | + "unable to fetch mediaType in multi-format: " + bid.getImpid()); |
| 174 | + }; |
| 175 | + } |
| 176 | +} |
0 commit comments