Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sovrn: Add video support #1874

Merged
merged 3 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 84 additions & 52 deletions src/main/java/org/prebid/server/bidder/sovrn/SovrnBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.iab.openrtb.request.Device;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.User;
import com.iab.openrtb.request.Video;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
Expand Down Expand Up @@ -55,14 +56,12 @@ public SovrnBidder(String endpointUrl, JacksonMapper mapper) {

@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) {
if (CollectionUtils.isEmpty(bidRequest.getImp())) {
return Result.empty();
}

final List<BidderError> errors = new ArrayList<>();
final List<Imp> processedImps = new ArrayList<>();

for (final Imp imp : bidRequest.getImp()) {
try {
validateImpVideo(imp.getVideo());
processedImps.add(makeImp(imp));
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
Expand All @@ -71,59 +70,30 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ

final BidRequest outgoingRequest = bidRequest.toBuilder().imp(processedImps).build();

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

@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(bidResponse));
} catch (DecodeException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
private static void validateImpVideo(Video video) {
if (video != null) {
if (video.getMimes() == null
|| video.getMinduration() == 0
|| video.getMaxduration() == 0
|| video.getProtocols() == null) {
throw new PreBidException("Missing required video parameter");
}
}
}

private Imp makeImp(Imp imp) {
// Just supporting banner until new video support comes along - already merged in Go prebid server
if (imp.getBanner() == null) {
throw new PreBidException(
String.format("No banner object found in imp. Ignoring Imp ID=%s", imp.getId()));
}

// Remove unsupported media types
if (imp.getAudio() != null) {
imp = imp.toBuilder().audio(null).build();
}

if (imp.getVideo() != null) {
imp = imp.toBuilder().video(null).build();
}

if (imp.getXNative() != null) {
imp = imp.toBuilder().xNative(null).build();
}

final ExtImpSovrn sovrnExt = parseExtImpSovrn(imp);

return imp.toBuilder()
.bidfloor(resolveBidFloor(imp.getBidfloor(), sovrnExt.getBidfloor()))
.tagid(ObjectUtils.defaultIfNull(sovrnExt.getTagid(), sovrnExt.getLegacyTagId()))
.tagid(resolveTagId(sovrnExt))
.build();
}

private ExtImpSovrn parseExtImpSovrn(Imp imp) {
if (imp.getExt() == null) {
throw new PreBidException("Sovrn parameters section is missing");
}

try {
return mapper.mapper().convertValue(imp.getExt(), SOVRN_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
Expand All @@ -137,6 +107,28 @@ private static BigDecimal resolveBidFloor(BigDecimal impBidFloor, BigDecimal ext
: impBidFloor;
}

private String resolveTagId(ExtImpSovrn sovrnExt) {
final String tagId = ObjectUtils.defaultIfNull(sovrnExt.getTagid(), sovrnExt.getLegacyTagId());
if (StringUtils.isEmpty(tagId)) {
throw new PreBidException("Missing required parameter 'tagid'");
}
return tagId;
}

private Result<List<HttpRequest<BidRequest>>> makeHttpRequest(BidRequest bidRequest,
List<BidderError> errors) {

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

private static MultiMap headers(BidRequest bidRequest) {
final MultiMap headers = HttpUtil.headers();

Expand All @@ -157,25 +149,65 @@ private static MultiMap headers(BidRequest bidRequest) {
return headers;
}

private static List<BidderBid> extractBids(BidResponse bidResponse) {
@Override
public Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final List<BidderError> bidderErrors = new ArrayList<>();
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
final BidRequest request = httpCall.getRequest().getPayload();

return Result.of(extractBids(bidResponse, request, bidderErrors), bidderErrors);
} catch (DecodeException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidResponse bidResponse,
BidRequest bidRequest,
List<BidderError> bidderErrors) {
return bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())
? Collections.emptyList()
: bidsFromResponse(bidResponse);
: bidsFromResponse(bidResponse, bidRequest, bidderErrors);
}

private static List<BidderBid> bidsFromResponse(BidResponse bidResponse) {
private static List<BidderBid> bidsFromResponse(BidResponse bidResponse,
BidRequest bidRequest,
List<BidderError> bidderErrors) {

return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(updateBid(bid), BidType.banner, bidResponse.getCur()))
.map(bid -> makeBidderBid(
bid,
resolveBidType(bid.getImpid(), bidRequest.getImp(), bidderErrors),
bidResponse.getCur()))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

private static Bid updateBid(Bid bid) {
return bid.toBuilder()
.adm(HttpUtil.decodeUrl(bid.getAdm()))
.build();
private static BidderBid makeBidderBid(Bid bid, BidType bidType, String cur) {
if (bidType == null) {
return null;
}

final Bid updatedBid = bid.toBuilder().adm(HttpUtil.decodeUrl(bid.getAdm())).build();
return BidderBid.of(updatedBid, bidType, cur);
}

private static BidType resolveBidType(String impId, List<Imp> imps, List<BidderError> bidderErrors) {
for (Imp imp : imps) {
final boolean matchedImpId = impId.equals(imp.getId());
if (matchedImpId && imp.getVideo() != null) {
return BidType.video;
} else if (matchedImpId) {
return BidType.banner;
}
}

bidderErrors.add(
BidderError.badInput("Imp ID " + impId + " in bid didn't match with any imp in the original request"));
return null;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/bidder-config/sovrn.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
adapters:
sovrn:
endpoint: http://ap.lijit.com/rtb/bid?src=prebid_server
modifyingVastXmlAllowed: true
meta-info:
maintainer-email: sovrnoss@sovrn.com
app-media-types:
- banner
- video
site-media-types:
- banner
- video
supported-vendors:
vendor-id: 13
usersync:
Expand Down
Loading