Skip to content

Commit

Permalink
Algorix: Add Rewarded Video detected, update mediatype resolving. (#1670
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Bugxyb authored Feb 21, 2022
1 parent 092d6b8 commit 2175275
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 68 deletions.
123 changes: 70 additions & 53 deletions src/main/java/org/prebid/server/bidder/algorix/AlgorixBidder.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.prebid.server.bidder.algorix;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Format;
import com.iab.openrtb.request.Imp;
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.MultiMap;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.collections4.CollectionUtils;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.algorix.model.AlgorixBidExt;
import org.prebid.server.bidder.algorix.model.AlgorixVideoExt;
import org.prebid.server.bidder.model.BidderBid;
import org.prebid.server.bidder.model.BidderError;
import org.prebid.server.bidder.model.HttpCall;
Expand All @@ -20,6 +25,7 @@
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.ExtImpPrebid;
import org.prebid.server.proto.openrtb.ext.request.algorix.ExtImpAlgorix;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.HttpUtil;
Expand All @@ -36,7 +42,7 @@
*/
public class AlgorixBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpAlgorix>> ALGORIX_EXT_TYPE_REFERENCE =
private static final TypeReference<ExtPrebid<ExtImpPrebid, ExtImpAlgorix>> ALGORIX_EXT_TYPE_REFERENCE =
new TypeReference<>() {
};

Expand All @@ -63,8 +69,9 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request

for (Imp imp : request.getImp()) {
try {
extImpAlgorix = extImpAlgorix == null ? parseImpExt(imp) : extImpAlgorix;
updatedImps.add(updateImp(imp));
final ExtPrebid<ExtImpPrebid, ExtImpAlgorix> impExt = parseImpExt(imp);
extImpAlgorix = extImpAlgorix == null ? impExt.getBidder() : extImpAlgorix;
updatedImps.add(updateImp(imp, impExt.getPrebid()));
} catch (PreBidException error) {
errors.add(BidderError.badInput(error.getMessage()));
}
Expand All @@ -86,39 +93,47 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
errors);
}

/**
* Parse Ext Imp
*
* @param imp BidRequest Imp
* @return Algorix Ext Imp
*/
private ExtImpAlgorix parseImpExt(Imp imp) {
private ExtPrebid<ExtImpPrebid, ExtImpAlgorix> parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), ALGORIX_EXT_TYPE_REFERENCE).getBidder();
return mapper.mapper().convertValue(imp.getExt(), ALGORIX_EXT_TYPE_REFERENCE);
} catch (IllegalArgumentException error) {
throw new PreBidException(String.format("Impression Id=%s, has invalid Ext", imp.getId()));
}
}

/**
* Update Imp for transform banner Size
*
* @param imp imp
* @return new imp
*/
private static Imp updateImp(Imp imp) {
private Imp updateImp(Imp imp, ExtImpPrebid extImpPrebid) {
if (imp.getBanner() != null) {
final Banner banner = imp.getBanner();
if (!(isValidSizeValue(banner.getW()) && isValidSizeValue(banner.getH()))
&& CollectionUtils.isNotEmpty(banner.getFormat())) {
final Format firstFormat = banner.getFormat().get(FIRST_INDEX);
imp = imp.toBuilder()
.banner(banner.toBuilder()
.w(firstFormat.getW())
.h(firstFormat.getH())
.build())
.build();
}
imp = updateBannerImp(imp);
}
if (imp.getVideo() != null) {
imp = updateVideoImp(imp, extImpPrebid);
}
return imp;
}

private Imp updateBannerImp(Imp imp) {
final Banner banner = imp.getBanner();
if (!(isValidSizeValue(banner.getW()) && isValidSizeValue(banner.getH()))
&& CollectionUtils.isNotEmpty(banner.getFormat())) {
final Format firstFormat = banner.getFormat().get(FIRST_INDEX);
return imp.toBuilder()
.banner(banner.toBuilder()
.w(firstFormat.getW())
.h(firstFormat.getH())
.build())
.build();
}
return imp;
}

private Imp updateVideoImp(Imp imp, ExtImpPrebid extImpPrebid) {
if (extImpPrebid != null && Objects.equals(extImpPrebid.getIsRewardedInventory(), 1)) {
final Video video = imp.getVideo();
return imp.toBuilder()
.video(video.toBuilder()
.ext(mapper.mapper().valueToTree(AlgorixVideoExt.of(1)))
.build())
.build();
}
return imp;
}
Expand All @@ -133,13 +148,6 @@ private static boolean isValidSizeValue(Integer value) {
return value != null && value > 0;
}

/**
* get Region Info From Algorix Ext Imp
* Default For Global EP, APAC for apse EP, USE for use EP
*
* @param extImp Algorix Ext Imp
* @return Region String
*/
private static String getRegionInfo(ExtImpAlgorix extImp) {
if (Objects.isNull(extImp.getRegion())) {
return "xyz";
Expand All @@ -154,25 +162,13 @@ private static String getRegionInfo(ExtImpAlgorix extImp) {
}
}

/**
* Replace url macro
*
* @param endpoint endpoint Url
* @param extImp Algorix Ext Imp
* @return target Url
*/
private static String resolveUrl(String endpoint, ExtImpAlgorix extImp) {
return endpoint
.replace(URL_REGION_MACRO, getRegionInfo(extImp))
.replace(URL_SID_MACRO, extImp.getSid())
.replace(URL_TOKEN_MACRO, extImp.getToken());
}

/**
* Add openrtb version header 2.5
*
* @return headers
*/
private static MultiMap resolveHeaders() {
final MultiMap headers = HttpUtil.headers();
headers.add(HttpUtil.X_OPENRTB_VERSION_HEADER, "2.5");
Expand All @@ -196,19 +192,40 @@ private List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidRespon
return bidsFromResponse(bidRequest, bidResponse);
}

private static List<BidderBid> bidsFromResponse(BidRequest bidRequest, BidResponse bidResponse) {
private List<BidderBid> bidsFromResponse(BidRequest bidRequest, BidResponse bidResponse) {
return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(bid, getBidType(bid.getImpid(), bidRequest.getImp()), bidResponse.getCur()))
.filter(Objects::nonNull)
.map(bid -> BidderBid.of(bid, getBidType(bid, bidRequest.getImp()), bidResponse.getCur()))
.collect(Collectors.toList());
}

private static BidType getBidType(String impId, List<Imp> imps) {
private AlgorixBidExt parseAlgorixBidExt(Bid bid) {
try {
return mapper.mapper().treeToValue(bid.getExt(), AlgorixBidExt.class);
} catch (IllegalArgumentException | JsonProcessingException error) {
return null;
}
}

private BidType getBidType(Bid bid, List<Imp> imps) {
final AlgorixBidExt bidExt = parseAlgorixBidExt(bid);
if (Objects.nonNull(bidExt)) {
switch (bidExt.getMediaType()) {
case "banner":
return BidType.banner;
case "native":
return BidType.xNative;
case "video":
return BidType.video;
default:
break;
}
}
for (Imp imp : imps) {
if (imp.getId().equals(impId)) {
if (imp.getId().equals(bid.getImpid())) {
if (imp.getBanner() != null) {
return BidType.banner;
} else if (imp.getVideo() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.prebid.server.bidder.algorix.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Value;

@Value(staticConstructor = "of")
public class AlgorixBidExt {

@JsonProperty("mediaType")
String mediaType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.prebid.server.bidder.algorix.model;

import lombok.Value;

@Value(staticConstructor = "of")
public class AlgorixVideoExt {

Integer rewarded;
}
Loading

0 comments on commit 2175275

Please sign in to comment.