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

OMS: add video support #3779

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 44 additions & 4 deletions src/main/java/org/prebid/server/bidder/oms/OmsBidder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.prebid.server.bidder.oms;

import com.fasterxml.jackson.core.type.TypeReference;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -10,8 +12,11 @@
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.exception.PreBidException;
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.omx.ExtImpOms;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;
Expand All @@ -23,6 +28,8 @@

public class OmsBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpOms>> EXT_TYPE_REFERENCE = new TypeReference<>() {
};
private final String endpointUrl;
private final JacksonMapper mapper;

Expand All @@ -32,16 +39,41 @@ public OmsBidder(String endpointUrl, JacksonMapper mapper) {
}

@Override
public final Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) {
return Result.withValue(BidderUtil.defaultRequest(bidRequest, endpointUrl, mapper));
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) {
String uri = endpointUrl;

if (!request.getImp().isEmpty()) {
try {
final ExtImpOms impExt = parseImpExt(request.getImp().get(0));
if (impExt != null) {
if (impExt.getPid() != null && !impExt.getPid().isEmpty()) {
uri = String.format("%s?publisherId=%s", endpointUrl, impExt.getPid());
} else if (impExt.getPublisherId() != null && impExt.getPublisherId() > 0) {
uri = String.format("%s?publisherId=%d", endpointUrl, impExt.getPublisherId());
}
}
} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}
}

return Result.withValue(BidderUtil.defaultRequest(request, uri, mapper));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it can work this way

            try {
                final ExtImpOms impExt = parseImpExt(request.getImp().getFirst());
                final String publisherId = impExt.getPid() == null && impExt.getPublisherId() != null && impExt.getPublisherId() > 0
                        ? String.valueOf(impExt.getPublisherId())
                        : impExt.getPid();
                final String url = "%s?publisherId=%s".formatted(endpointUrl, publisherId);
                return Result.withValue(BidderUtil.defaultRequest(request, url, mapper));
            } catch (PreBidException e) {
                return Result.withError(BidderError.badInput(e.getMessage()));
            }


private ExtImpOms parseImpExt(Imp imp) throws PreBidException {
try {
return mapper.mapper().convertValue(imp.getExt(), EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException("Invalid ext. Imp.Id: " + imp.getId());
}
}

@Override
public final Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.withValues(extractBids(bidResponse));
} catch (DecodeException e) {
} catch (DecodeException | PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}
Expand All @@ -59,7 +91,15 @@ private static List<BidderBid> bidsFromResponse(BidResponse bidResponse) {
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(bid, BidType.banner, bidResponse.getCur()))
.map(bid -> BidderBid.of(bid, getBidType(bid.getMtype()), bidResponse.getCur()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the following is not ported

func getBidVideo(bidType openrtb_ext.BidType, bid *openrtb2.Bid) *openrtb_ext.ExtBidPrebidVideo {
	if bidType != openrtb_ext.BidTypeVideo {
		return nil
	}

	var primaryCategory string
	if len(bid.Cat) > 0 {
		primaryCategory = bid.Cat[0]
	}

	return &openrtb_ext.ExtBidPrebidVideo{
		Duration:        int(bid.Dur),
		PrimaryCategory: primaryCategory,
	}
}

.toList();
}

private static BidType getBidType(Integer mType) {
return switch (mType) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
case null, default -> throw new PreBidException("Unsupported mType " + mType);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.prebid.server.proto.openrtb.ext.request.omx;

import lombok.Value;

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

String pid;

Integer publisherId;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add JsonProperty

also I would change the integration test payload to cover publisherId parameter

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have cheanged pid to publisherId but IMO its indifferent

}
2 changes: 2 additions & 0 deletions src/main/resources/bidder-config/oms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ adapters:
maintainer-email: [email protected]
app-media-types:
- banner
- video
site-media-types:
- banner
- video
supported-vendors:
vendor-id: 0
20 changes: 17 additions & 3 deletions src/main/resources/static/bidder-params/oms.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@
"properties": {
"pid": {
"type": "string",
"description": "An id used to identify OMS publisher.",
"description": "Deprecated: An id used to identify OMS publisher.",
"minLength": 5
},
"publisherId": {
"type": "integer",
"description": "An ID used to identify OMS publisher.",
"minimum": 10000
}
},
"required": [
"pid"
"oneOf": [
{
"required": [
"pid"
]
},
{
"required": [
"publisherId"
]
}
]
}
147 changes: 131 additions & 16 deletions src/test/java/org/prebid/server/bidder/oms/OmsBidderTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.prebid.server.bidder.oms;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
Expand All @@ -15,7 +16,12 @@
import org.prebid.server.bidder.model.HttpRequest;
import org.prebid.server.bidder.model.HttpResponse;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.omx.ExtImpOms;
import org.prebid.server.proto.openrtb.ext.response.BidType;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.UnaryOperator;
Expand All @@ -24,7 +30,7 @@
import static java.util.function.UnaryOperator.identity;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.prebid.server.proto.openrtb.ext.response.BidType.banner;
import static org.prebid.server.bidder.model.BidderError.badInput;

public class OmsBidderTest extends VertxTest {

Expand All @@ -37,10 +43,40 @@ public void creationShouldFailOnInvalidEndpointUrl() {
assertThatIllegalArgumentException().isThrownBy(() -> new OmsBidder("invalid_url", jacksonMapper));
}

@Test
public void makeHttpRequestsShouldReturnErrorWhenRequestHasInvalidImpression() {
// given
final ObjectNode invalidExt = mapper.valueToTree(ExtPrebid.of(null, mapper.createArrayNode()));
final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(invalidExt));

// when
final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).hasSize(1).first().isEqualTo(badInput("Invalid ext. Imp.Id: 123"));
}

@Test
public void makeHttpRequestsShouldCreateExpectedUrl() {
// given
final BidRequest bidRequest = givenBidRequest(identity());
final ExtImpOms impExt = ExtImpOms.of("otherTagId", 12345);
final BidRequest bidRequest = givenBidRequest(impCustomizer -> impCustomizer.ext(givenImpExt(impExt)));

// when
final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1)
.extracting(HttpRequest::getUri)
.containsExactly("https://randomurl.com?publisherId=otherTagId");
}

@Test
public void makeHttpRequestsShouldCreateExpectedUrlWithPublisherId() {
// given
final ExtImpOms impExt = ExtImpOms.of(null, 12345);
final BidRequest bidRequest = givenBidRequest(impCustomizer -> impCustomizer.ext(givenImpExt(impExt)));

// when
final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest);
Expand All @@ -49,7 +85,43 @@ public void makeHttpRequestsShouldCreateExpectedUrl() {
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1)
.extracting(HttpRequest::getUri)
.containsExactly("https://randomurl.com");
.containsExactly("https://randomurl.com?publisherId=12345");
}

@Test
public void makeHttpRequestsShouldIncludePidInRequestWhenPresent() {
// given
final ObjectNode impExt = mapper.createObjectNode().put("pid", "examplePid");
final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(impExt));

// when
final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(HttpRequest::getPayload)
.flatExtracting(BidRequest::getImp)
.extracting(Imp::getExt)
.containsExactly(impExt);
}

@Test
public void makeHttpRequestsShouldIncludePublisherIdInRequestWhenPresent() {
// given
final ObjectNode impExt = mapper.createObjectNode().put("publisherId", 12345);
final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(impExt));

// when
final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(HttpRequest::getPayload)
.flatExtracting(BidRequest::getImp)
.extracting(Imp::getExt)
.containsExactly(impExt);
}

@Test
Expand Down Expand Up @@ -101,32 +173,75 @@ public void makeBidsShouldReturnBannerBid() throws JsonProcessingException {
// given
final BidderCall<BidRequest> httpCall = givenHttpCall(
givenBidRequest(impBuilder -> impBuilder.banner(Banner.builder().build())),
mapper.writeValueAsString(givenBidResponse(impBuilder -> impBuilder.impid("123"))));
mapper.writeValueAsString(givenBidResponse(impBuilder -> impBuilder.impid("123").mtype(1))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(givenBid(), banner, null));
assertThat(result.getValue()).extracting(BidderBid::getType).containsExactly(BidType.banner);
}

@Test
public void makeBidsShouldReturnBannerBidIfBannerAndVideoAndAudioAndNativeIsAbsentInRequestImp()
throws JsonProcessingException {
public void makeBidsShouldReturnVideoBid() throws JsonProcessingException {
// given
final BidderCall<BidRequest> httpCall = givenHttpCall(
givenBidRequest(identity()),
mapper.writeValueAsString(givenBidResponse(impBuilder -> impBuilder.impid("123"))));
givenBidRequest(impBuilder -> impBuilder.banner(Banner.builder().build())),
mapper.writeValueAsString(givenBidResponse(impBuilder -> impBuilder.impid("123").mtype(2))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(givenBid(), banner, null));
assertThat(result.getValue()).extracting(BidderBid::getType).containsExactly(BidType.video);
}

@Test
public void makeBidsShouldReturnErrorWhenMTypeIsUnsupported() throws JsonProcessingException {
// given
final BidderCall<BidRequest> httpCall = givenHttpCall(
givenBidRequest(impBuilder -> impBuilder.banner(Banner.builder().build())),
mapper.writeValueAsString(givenBidResponse(impBuilder -> impBuilder.impid("123").mtype(99))));

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

// then
assertThat(result.getErrors()).hasSize(1);
assertThat(result.getErrors().get(0).getMessage()).contains("Unsupported mType 99");
assertThat(result.getValue()).isEmpty();
}

@Test
public void makeBidsShouldExtractAllBidsFromMultipleSeatBids() throws JsonProcessingException {
// given
final Bid bid1 = Bid.builder().impid("bid1").mtype(1).build();
final Bid bid2 = Bid.builder().impid("bid2").mtype(1).build();
final Bid bid3 = Bid.builder().impid("bid3").mtype(2).build();

final SeatBid seatBid1 = SeatBid.builder().bid(Arrays.asList(bid1, bid2)).build();
final SeatBid seatBid2 = SeatBid.builder().bid(Collections.singletonList(bid3)).build();

final BidResponse bidResponse = BidResponse.builder()
.seatbid(Arrays.asList(seatBid1, seatBid2))
.cur("USD")
.build();
final String bidResponseJson = mapper.writeValueAsString(bidResponse);

final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.banner(Banner.builder().build()));
final BidderCall<BidRequest> httpCall = givenHttpCall(bidRequest, bidResponseJson);

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(3)
.extracting(BidderBid::getType)
.containsExactly(BidType.banner, BidType.banner, BidType.video);
assertThat(result.getValue()).extracting(BidderBid::getBidCurrency).containsOnly("USD");
}

private static BidRequest givenBidRequest(UnaryOperator<Imp.ImpBuilder> impCustomizer) {
Expand All @@ -148,14 +263,14 @@ private static BidResponse givenBidResponse(Function<Bid.BidBuilder, Bid.BidBuil
.build();
}

private static Bid givenBid() {
return Bid.builder().impid("123").build();
}

private static BidderCall<BidRequest> givenHttpCall(BidRequest bidRequest, String body) {
return BidderCall.succeededHttp(
HttpRequest.<BidRequest>builder().payload(bidRequest).build(),
HttpResponse.of(200, null, body),
null);
}

private ObjectNode givenImpExt(ExtImpOms impExt) {
return mapper.valueToTree(ExtPrebid.of(null, impExt));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"exp": 300,
"price": 3.33,
"crid": "creativeId",
"mtype": 1,
"ext": {
"origbidcpm": 3.33,
"prebid": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"id": "bid_id",
"impid": "imp_id",
"price": 3.33,
"crid": "creativeId"
"crid": "creativeId",
"mtype": 1
}
]
}
Expand Down
Loading