Skip to content

Commit

Permalink
Yieldmo: Retrieve bid type from bid.ext.mediatype (#2759)
Browse files Browse the repository at this point in the history
  • Loading branch information
ym-prasanth authored Nov 27, 2023
1 parent 4d05ce8 commit aafdaea
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
23 changes: 14 additions & 9 deletions src/main/java/org/prebid/server/bidder/yieldmo/YieldmoBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.prebid.server.bidder.model.BidderError;
import org.prebid.server.bidder.model.HttpRequest;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.bidder.yieldmo.proto.YieldmoBidExt;
import org.prebid.server.bidder.yieldmo.proto.YieldmoImpExt;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.json.DecodeException;
Expand Down Expand Up @@ -97,13 +98,13 @@ private HttpRequest<BidRequest> makeRequest(BidRequest bidRequest) {
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.withValues(extractBids(httpCall.getRequest().getPayload(), bidResponse));
return Result.withValues(extractBids(bidResponse));
} catch (DecodeException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidResponse) {
private List<BidderBid> extractBids(BidResponse bidResponse) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
Expand All @@ -114,16 +115,20 @@ private static List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bi
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(Objects::nonNull)
.map(bid -> BidderBid.of(bid, resolveBidType(bid, bidRequest.getImp()), bidResponse.getCur()))
.map(bid -> {
final BidType bidType = resolveBidType(bid);
return bidType != null ? BidderBid.of(bid, bidType, bidResponse.getCur()) : null;
})
.filter(Objects::nonNull)
.toList();
}

private static BidType resolveBidType(Bid bid, List<Imp> imps) {
for (Imp imp : imps) {
if (Objects.equals(imp.getId(), bid.getImpid())) {
return imp.getBanner() != null ? BidType.banner : BidType.video;
}
private BidType resolveBidType(Bid bid) {
try {
final YieldmoBidExt bidExt = mapper.mapper().treeToValue(bid.getExt(), YieldmoBidExt.class);
return BidType.fromString(bidExt.getMediaType());
} catch (Exception e) {
return null;
}
return BidType.video;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.prebid.server.bidder.yieldmo.proto;

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

/**
* Defines the contract for bidresponse.seatbid[i].bid[i].ext
*/
@Value(staticConstructor = "of")
public class YieldmoBidExt {

@JsonProperty("mediatype")
String mediaType;
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.prebid.server.bidder.yieldmo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.iab.openrtb.request.Banner;
Expand Down Expand Up @@ -199,7 +201,7 @@ public void makeBidsShouldReturnEmptyListIfBidResponseSeatBidIsNull() throws Jso
}

@Test
public void makeBidsShouldReturnVideoBidByDefault() throws JsonProcessingException {
public void makeBidsShouldReturnEmptyListIfBidExtHasNoMediaType() throws JsonProcessingException {
// given
final BidderCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
Expand All @@ -213,8 +215,7 @@ public void makeBidsShouldReturnVideoBidByDefault() throws JsonProcessingExcepti

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), video, "USD"));
assertThat(result.getValue()).isEmpty();
}

@Test
Expand All @@ -225,15 +226,16 @@ public void makeBidsShouldReturnBannerBidIfBannerIsPresentInRequestImp() throws
.imp(singletonList(Imp.builder().banner(Banner.builder().build()).id("123").build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));
givenBidResponse(bidBuilder -> bidBuilder.impid("123").ext(toObjectNode("banner")))));

// when
final Result<List<BidderBid>> result = target.makeBids(httpCall, null);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), banner, "USD"));
.containsExactly(BidderBid.of(Bid.builder().impid("123").ext(toObjectNode("banner")).build(),
banner, "USD"));
}

@Test
Expand All @@ -244,15 +246,16 @@ public void makeBidsShouldReturnVideoBidIfVideoIsPresentInRequestImp() throws Js
.imp(singletonList(Imp.builder().video(Video.builder().build()).id("123").build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));
givenBidResponse(bidBuilder -> bidBuilder.impid("123").ext(toObjectNode("video")))));

// when
final Result<List<BidderBid>> result = target.makeBids(httpCall, null);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), video, "USD"));
.containsExactly(BidderBid.of(Bid.builder().impid("123").ext(toObjectNode("video")).build(),
video, "USD"));
}

private static BidRequest givenBidRequest(
Expand Down Expand Up @@ -291,4 +294,11 @@ private static BidderCall<BidRequest> givenHttpCall(BidRequest bidRequest, Strin
HttpResponse.of(200, null, body),
null);
}

private ObjectNode toObjectNode(String mediaType) {
final ObjectMapper mapper = new ObjectMapper();
final ObjectNode root = mapper.createObjectNode();
root.set("mediatype", mapper.convertValue(mediaType, JsonNode.class));
return root;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"prebid": {
"type": "banner"
},
"origbidcpm": 3.33
"origbidcpm": 3.33,
"mediatype": "banner"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
"cid": "cid",
"adm": "adm001",
"h": 250,
"w": 300
"w": 300,
"ext" : {
"mediatype" : "banner"
}
}
]
}
]
}
}

0 comments on commit aafdaea

Please sign in to comment.