Skip to content

Commit

Permalink
Review conversant adapter (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhiiNahornyi authored Dec 7, 2020
1 parent 0e805f2 commit 271b201
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.Site;
import com.iab.openrtb.request.Video;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import io.vertx.core.http.HttpMethod;
Expand All @@ -26,7 +27,6 @@
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -40,8 +40,8 @@
*/
public class ConversantBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpConversant>> CONVERSANT_EXT_TYPE_REFERENCE = new
TypeReference<ExtPrebid<?, ExtImpConversant>>() {
private static final TypeReference<ExtPrebid<?, ExtImpConversant>> CONVERSANT_EXT_TYPE_REFERENCE =
new TypeReference<ExtPrebid<?, ExtImpConversant>>() {
};

// List of API frameworks supported by the publisher
Expand Down Expand Up @@ -72,14 +72,13 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}
final String body = mapper.encode(outgoingRequest);

return Result.of(Collections.singletonList(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.body(body)
.headers(HttpUtil.headers())
.body(mapper.encode(outgoingRequest))
.payload(outgoingRequest)
.build()),
Collections.emptyList());
Expand Down Expand Up @@ -115,7 +114,7 @@ private ExtImpConversant parseImpExt(Imp imp, int impIndex) {
throw new PreBidException(String.format("Impression[%d] missing ext.bidder object", impIndex));
}

if (StringUtils.isBlank(extImp.getSiteId())) {
if (StringUtils.isEmpty(extImp.getSiteId())) {
throw new PreBidException(String.format("Impression[%d] requires ext.bidder.site_id", impIndex));
}
return extImp;
Expand All @@ -130,97 +129,105 @@ private static App updateApp(App app, String siteId) {
}

private static Imp modifyImp(Imp imp, ExtImpConversant impExt) {
final BigDecimal extBidfloor = impExt.getBidfloor();
final String extTagId = impExt.getTagId();
final Integer extSecure = impExt.getSecure();
final boolean shouldChangeSecure = extSecure != null && (imp.getSecure() == null || imp.getSecure() == 0);
final Banner impBanner = imp.getBanner();
final Integer extPosition = impExt.getPosition();
final Video impVideo = imp.getVideo();
final Banner banner = imp.getBanner();
final Video video = imp.getVideo();

return imp.toBuilder()
.displaymanager(DISPLAY_MANAGER)
.displaymanagerver(DISPLAY_MANAGER_VER)
.banner(modifyBanner(impBanner, extPosition))
.bidfloor(extBidfloor != null ? extBidfloor : imp.getBidfloor())
.tagid(extTagId != null ? extTagId : imp.getTagid())
.secure(shouldChangeSecure ? extSecure : imp.getSecure())
.video(impVideo != null && impBanner == null ? modifyVideo(impVideo, impExt) : impVideo)
.bidfloor(impExt.getBidfloor())
.tagid(impExt.getTagId())
.secure(getSecure(imp, impExt))
.banner(modifyBanner(banner, impExt.getPosition()))
.video(video != null && banner == null ? modifyVideo(video, impExt) : video)
.build();
}

private static Integer getSecure(Imp imp, ExtImpConversant impExt) {
final Integer extSecure = impExt.getSecure();
final Integer impSecure = imp.getSecure();

return extSecure != null && (impSecure == null || impSecure == 0) ? extSecure : impSecure;
}

private static Banner modifyBanner(Banner impBanner, Integer extPosition) {
return impBanner == null || extPosition == null
? impBanner
return impBanner == null
? null
: impBanner.toBuilder()
.pos(AD_POSITIONS.contains(extPosition) ? extPosition : null)
.pos(isValidPosition(extPosition) ? extPosition : null)
.build();
}

private static Video modifyVideo(Video video, ExtImpConversant impExt) {
final List<String> extMimes = impExt.getMimes();
final Integer extMaxduration = impExt.getMaxduration();
final Integer extMaxDuration = impExt.getMaxduration();
final Integer extPosition = impExt.getPosition();
final List<Integer> extProtocols = impExt.getProtocols();
final List<Integer> extApi = impExt.getApi();
return video.toBuilder()
.mimes(extMimes != null ? extMimes : video.getMimes())
.maxduration(extMaxduration != null ? extMaxduration : video.getMaxduration())
.pos(makePosition(extPosition, video.getPos()))
.api(makeApi(extApi, video.getApi()))
.protocols(makeProtocols(extProtocols, video.getProtocols()))
.mimes(CollectionUtils.isNotEmpty(extMimes) ? extMimes : video.getMimes())
.maxduration(extMaxDuration != null ? extMaxDuration : video.getMaxduration())
.pos(isValidPosition(extPosition) ? extPosition : null)
.api(makeApi(impExt.getApi(), video.getApi()))
.protocols(makeProtocols(impExt.getProtocols(), video.getProtocols()))
.build();
}

private static Integer makePosition(Integer position, Integer videoPos) {
return isValidPosition(position) ? position : isValidPosition(videoPos) ? videoPos : null;
}

private static boolean isValidPosition(Integer position) {
return position != null && AD_POSITIONS.contains(position);
}

private static List<Integer> makeApi(List<Integer> extApi, List<Integer> videoApi) {
final List<Integer> protocols = CollectionUtils.isNotEmpty(extApi) ? extApi : videoApi;
return CollectionUtils.isNotEmpty(protocols)
? protocols.stream().filter(APIS::contains).collect(Collectors.toList()) : videoApi;
final List<Integer> api = CollectionUtils.isNotEmpty(extApi) ? extApi : videoApi;
return CollectionUtils.isNotEmpty(api)
? api.stream().filter(APIS::contains).collect(Collectors.toList())
: videoApi;
}

private static List<Integer> makeProtocols(List<Integer> extProtocols, List<Integer> videoProtocols) {
final List<Integer> protocols = CollectionUtils.isNotEmpty(extProtocols) ? extProtocols : videoProtocols;
return CollectionUtils.isNotEmpty(protocols)
? protocols.stream().filter(PROTOCOLS::contains).collect(Collectors.toList()) : videoProtocols;
? protocols.stream().filter(PROTOCOLS::contains).collect(Collectors.toList())
: videoProtocols;
}

@Override
public Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.withValues(extractBids(httpCall.getRequest().getPayload(), bidResponse));
} catch (DecodeException | PreBidException e) {
return Result.withValues(extractBids(httpCall));
} catch (PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidResponse) {
private List<BidderBid> extractBids(HttpCall<BidRequest> httpCall) {
final BidResponse bidResponse;
try {
bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
} catch (DecodeException e) {
throw new PreBidException(e.getMessage());
}
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
throw new PreBidException("Empty bid request");
}
return bidsFromResponse(bidRequest, bidResponse);
return bidsFromResponse(httpCall.getRequest().getPayload(), bidResponse);
}

private static List<BidderBid> bidsFromResponse(BidRequest bidRequest, BidResponse bidResponse) {
final SeatBid firstSeatBid = bidResponse.getSeatbid().get(0);
return firstSeatBid.getBid().stream()
final List<Bid> bids = firstSeatBid.getBid();

if (CollectionUtils.isEmpty(bids)) {
throw new PreBidException("Empty bids array");
}
return bids.stream()
.filter(Objects::nonNull)
.map(bid -> BidderBid.of(bid, getType(bid.getImpid(), bidRequest.getImp()), bidResponse.getCur()))
.collect(Collectors.toList());
}

private static BidType getType(String impId, List<Imp> imps) {
for (Imp imp : imps) {
if (imp.getId().equals(impId) && imp.getVideo() != null) {
return BidType.video;
if (imp.getId().equals(impId)) {
return imp.getVideo() != null ? BidType.video : BidType.banner;
}
}
return BidType.banner;
Expand Down
Loading

0 comments on commit 271b201

Please sign in to comment.