Skip to content

Commit

Permalink
Add BidderUtil (#1465)
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhiiNahornyi authored Sep 16, 2021
1 parent 0ee3e9f commit 8f85e76
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.adtarget.ExtImpAdtarget;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -118,7 +119,7 @@ private Imp updateImp(Imp imp, ExtImpAdtarget extImpAdtarget) {
final AdtargetImpExt adtargetImpExt = AdtargetImpExt.of(extImpAdtarget);
final BigDecimal bidFloor = extImpAdtarget.getBidFloor();
return imp.toBuilder()
.bidfloor(bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) > 0 ? bidFloor : imp.getBidfloor())
.bidfloor(BidderUtil.isValidPrice(bidFloor) ? bidFloor : imp.getBidfloor())
.ext(mapper.mapper().valueToTree(adtargetImpExt))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.adtelligent.ExtImpAdtelligent;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -171,7 +172,7 @@ private Imp updateImp(Imp imp, ExtImpAdtelligent extImpAdtelligent) {
final AdtelligentImpExt adtelligentImpExt = AdtelligentImpExt.of(extImpAdtelligent);
final BigDecimal bidFloor = extImpAdtelligent.getBidFloor();
return imp.toBuilder()
.bidfloor(bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) > 0 ? bidFloor : imp.getBidfloor())
.bidfloor(BidderUtil.isValidPrice(bidFloor) ? bidFloor : imp.getBidfloor())
.ext(mapper.mapper().valueToTree(adtelligentImpExt))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.prebid.server.proto.openrtb.ext.request.ExtRequestTargeting;
import org.prebid.server.proto.openrtb.ext.request.appnexus.ExtImpAppnexus;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -362,7 +363,7 @@ private ImpWithExtProperties processImp(Imp imp, String defaultDisplayManagerVer
}

final BigDecimal reserve = appnexusExt.getReserve();
if (!bidFloorIsValid(imp.getBidfloor()) && bidFloorIsValid(reserve)) {
if (!BidderUtil.isValidPrice(imp.getBidfloor()) && BidderUtil.isValidPrice(reserve)) {
impBuilder.bidfloor(reserve); // This will be broken for non-USD currency.
}

Expand All @@ -375,10 +376,6 @@ private ImpWithExtProperties processImp(Imp imp, String defaultDisplayManagerVer
appnexusExt.getGenerateAdPodId());
}

private static boolean bidFloorIsValid(BigDecimal bidFloor) {
return bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) > 0;
}

private static AppnexusImpExt makeAppnexusImpExt(ExtImpAppnexus appnexusExt) {
return AppnexusImpExt.of(
AppnexusImpExtAppnexus.of(appnexusExt.getPlacementId(), makeKeywords(appnexusExt.getKeywords()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.beintoo.ExtImpBeintoo;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -123,7 +124,7 @@ private static Imp modifyImp(Imp imp, boolean isSecure, ExtImpBeintoo extImpBein

final String stringBidfloor = extImpBeintoo.getBidFloor();
final BigDecimal bidfloor = StringUtils.isBlank(stringBidfloor) ? null : new BigDecimal(stringBidfloor);
return (bidfloor != null ? bidfloor.compareTo(BigDecimal.ZERO) : 0) > 0
return BidderUtil.isValidPrice(bidfloor)
? impBuilder.bidfloor(bidfloor).build()
: impBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.connectad.ExtImpConnectAd;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -77,13 +78,13 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
final String body = mapper.encode(outgoingRequest);

return Result.of(Collections.singletonList(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(resolveHeaders(request.getDevice()))
.payload(outgoingRequest)
.body(body)
.build()),
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(resolveHeaders(request.getDevice()))
.payload(outgoingRequest)
.body(body)
.build()),
errors);
}

Expand All @@ -109,7 +110,7 @@ private ExtImpConnectAd parseImpExt(Imp imp) {
private Imp updateImp(Imp imp, Integer secure, Integer siteId, BigDecimal bidFloor) {
final Imp.ImpBuilder updatedImp = imp.toBuilder().tagid(siteId.toString()).secure(secure);

if (bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) != 0) {
if (BidderUtil.isValidPrice(bidFloor)) {
updatedImp.bidfloor(bidFloor).bidfloorcur("USD");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.conversant.ExtImpConversant;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -153,15 +154,11 @@ private static String getTagId(String tagId, String impExtTagId) {

private static BigDecimal getBidFloor(BigDecimal impBidFloor, BigDecimal impExtBidFloor) {

return isValidBidFloor(impExtBidFloor) && !isValidBidFloor(impBidFloor)
return BidderUtil.isValidPrice(impExtBidFloor) && !BidderUtil.isValidPrice(impBidFloor)
? impExtBidFloor
: impBidFloor;
}

private static boolean isValidBidFloor(BigDecimal bidFloor) {
return bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) > 0;
}

private static Integer getSecure(Imp imp, ExtImpConversant impExt) {
final Integer extSecure = impExt.getSecure();
final Integer impSecure = imp.getSecure();
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/org/prebid/server/bidder/dmx/DmxBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.prebid.server.proto.openrtb.ext.request.ExtUser;
import org.prebid.server.proto.openrtb.ext.request.dmx.ExtImpDmx;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -116,13 +117,13 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
final String uri = String.format("%s%s", endpointUrl, urlParameter);

return Result.of(Collections.singletonList(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(uri)
.headers(HttpUtil.headers())
.body(mapper.encode(outgoingRequest))
.payload(outgoingRequest)
.build()),
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(uri)
.headers(HttpUtil.headers())
.body(mapper.encode(outgoingRequest))
.payload(outgoingRequest)
.build()),
errors);
}

Expand Down Expand Up @@ -204,9 +205,7 @@ private static Imp fetchParams(Imp imp, ExtImpDmx extImp) {

private static BigDecimal resolveBidFloor(ExtImpDmx extImp, BigDecimal bidFloor) {
final BigDecimal extBidFloor = extImp.getBidFloor();
return extBidFloor != null && extBidFloor.compareTo(BigDecimal.ZERO) > 0
? extBidFloor
: bidFloor;
return BidderUtil.isValidPrice(extBidFloor) ? extBidFloor : bidFloor;
}

private static Banner resolveBanner(Banner banner) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/prebid/server/bidder/mgid/MgidBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.request.mgid.ExtImpMgid;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -119,11 +120,11 @@ private static String currencyValueOrNull(String value) {

private static BigDecimal getBidFloor(ExtImpMgid impMgid) {
return ObjectUtils.defaultIfNull(
bidFloorValueOrNull(impMgid.getBidfloor()), bidFloorValueOrNull(impMgid.getBidFloorSecond()));
validBidFloorOrNull(impMgid.getBidfloor()), validBidFloorOrNull(impMgid.getBidFloorSecond()));
}

private static BigDecimal bidFloorValueOrNull(BigDecimal value) {
return value != null && value.compareTo(BigDecimal.ZERO) > 0 ? value : null;
private static BigDecimal validBidFloorOrNull(BigDecimal bidFloor) {
return BidderUtil.isValidPrice(bidFloor) ? bidFloor : null;
}

private static String getTagid(Imp imp, ExtImpMgid impMgid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.nanointeractive.ExtImpNanointeractive;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -75,13 +75,13 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
final String body = mapper.encode(outgoingRequest);

return Result.of(Collections.singletonList(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(headers(request))
.payload(outgoingRequest)
.body(body)
.build()),
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(headers(request))
.payload(outgoingRequest)
.body(body)
.build()),
errors);
}

Expand Down Expand Up @@ -149,7 +149,7 @@ private static List<BidderBid> bidsFromResponse(BidResponse bidResponse) {
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(bid -> bid.getPrice().compareTo(BigDecimal.ZERO) > 0)
.filter(bid -> BidderUtil.isValidPrice(bid.getPrice()))
.map(bid -> BidderBid.of(bid, BidType.banner, bidResponse.getCur()))
.collect(Collectors.toList());
}
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/prebid/server/bidder/openx/OpenxBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.openx.ExtImpOpenx;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -174,15 +175,11 @@ private Imp makeImp(Imp imp) {
}

private static BigDecimal resolveBidFloor(BigDecimal impBidFloor, BigDecimal customFloor) {
return !bidFloorIsValid(impBidFloor) && bidFloorIsValid(customFloor)
return !BidderUtil.isValidPrice(impBidFloor) && BidderUtil.isValidPrice(customFloor)
? customFloor
: impBidFloor;
}

private static boolean bidFloorIsValid(BigDecimal bidFloor) {
return bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) > 0;
}

private ExtRequest makeReqExt(Imp imp) {
final ExtImpOpenx openxImpExt = parseOpenxExt(imp).getBidder();
return mapper.fillExtension(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.pubnative.ExtImpPubnative;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -135,7 +136,7 @@ private static Banner resolveBanner(Banner banner) {
private BigDecimal resolveBidFloor(BidRequest bidRequest, Imp imp) {
final BigDecimal bidFloor = imp.getBidfloor();
final String bidFloorCur = resolveBidFloorCurrency(bidRequest, imp.getBidfloorcur());
if (bidFloor == null || bidFloor.compareTo(BigDecimal.ZERO) <= 0
if (!BidderUtil.isValidPrice(bidFloor)
|| StringUtils.equals(bidFloorCur, PUBNATIVE_CURRENCY)
|| StringUtils.isEmpty(bidFloorCur)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import org.prebid.server.proto.openrtb.ext.request.rubicon.RubiconVideoParams;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -462,7 +463,7 @@ private BigDecimal resolveBidFloor(Imp imp, BidRequest bidRequest, List<BidderEr

private static BigDecimal resolveBidFloorPrice(Imp imp) {
final BigDecimal bidFloor = imp.getBidfloor();
return bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) > 0 ? bidFloor : null;
return BidderUtil.isValidPrice(bidFloor) ? bidFloor : null;
}

private static String resolveBidFloorCurrency(Imp imp, BidRequest bidRequest, List<BidderError> errors) {
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/prebid/server/bidder/sovrn/SovrnBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.sovrn.ExtImpSovrn;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.math.BigDecimal;
Expand Down Expand Up @@ -122,11 +123,9 @@ private ExtImpSovrn parseExtImpSovrn(Imp imp) {
}

private static BigDecimal resolveBidFloor(BigDecimal impBidFloor, BigDecimal extBidFloor) {
return !isValidBidFloor(impBidFloor) && isValidBidFloor(extBidFloor) ? extBidFloor : impBidFloor;
}

private static boolean isValidBidFloor(BigDecimal bidFloor) {
return bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) > 0;
return !BidderUtil.isValidPrice(impBidFloor) && BidderUtil.isValidPrice(extBidFloor)
? extBidFloor
: impBidFloor;
}

private static MultiMap headers(BidRequest bidRequest) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/prebid/server/util/BidderUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.prebid.server.util;

import java.math.BigDecimal;

public class BidderUtil {

private BidderUtil() {
}

public static boolean isValidPrice(BigDecimal price) {
return price != null && price.compareTo(BigDecimal.ZERO) > 0;
}
}

0 comments on commit 8f85e76

Please sign in to comment.