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

MinuteMedia: Add new bidder #3019

Merged
merged 6 commits into from
Mar 1, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package org.prebid.server.bidder.minutemedia;

import com.fasterxml.jackson.core.type.TypeReference;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.model.BidderBid;
import org.prebid.server.bidder.model.BidderCall;
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.minutemedia.ExtImpMinuteMedia;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class MinuteMediaBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpMinuteMedia>> MINUTE_MEDIA_EXT_TYPE_REFERENCE =
new TypeReference<>() {
};
public static final String PUBLISHER_ID_MACRO = "{{PublisherId}}";

private final String endpointUrl;
private final JacksonMapper mapper;

public MinuteMediaBidder(String endpointUrl, JacksonMapper mapper) {
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
this.mapper = Objects.requireNonNull(mapper);
}

@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) {
final String orgId;

try {
orgId = extractFirstImpOrdId(bidRequest.getImp());
Copy link
Collaborator

Choose a reason for hiding this comment

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

They are checking each imp for org, not just first

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only first. They iterate over request.imp object, but they return right away.

func extractOrg(openRTBRequest *openrtb2.BidRequest) (string, error) {
	var err error
	for _, imp := range openRTBRequest.Imp {
		var bidderExt adapters.ExtImpBidder
		if err = json.Unmarshal(imp.Ext, &bidderExt); err != nil {
			return "", fmt.Errorf("failed to unmarshal bidderExt: %w", err)
		}

		var impExt openrtb_ext.ImpExtMinuteMedia
		if err = json.Unmarshal(bidderExt.Bidder, &impExt); err != nil {
			return "", fmt.Errorf("failed to unmarshal ImpExtMinuteMedia: %w", err)
		}

		return strings.TrimSpace(impExt.Org), nil
	}

	return "", errors.New("no imps in bid request")
}

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 can't say that I like how it's done, but there was a discussion about it and they made an explicit decision to not do any kind of validation for this field...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree, org is the required field and always will be here

} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}

return Result.withValue(BidderUtil.defaultRequest(bidRequest, resolveEndpoint(endpointUrl, orgId), mapper));
}

private String extractFirstImpOrdId(List<Imp> imps) {
return imps.stream()
.findFirst()
.map(this::parseImpExt)
.map(ExtImpMinuteMedia::getOrg)
.map(String::strip)
.filter(StringUtils::isNotBlank)
.orElseThrow(() -> new PreBidException(
"Failed to extract bidrequest.imp[0].ext.prebid.bidder.minutemedia.org parameter"));
}

private ExtImpMinuteMedia parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), MINUTE_MEDIA_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException(e.getMessage());
}
}

private String resolveEndpoint(String endpointUrl, String orgId) {
return endpointUrl.replace(PUBLISHER_ID_MACRO, HttpUtil.encodeUrl(orgId));
}

@Override
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) {
final List<BidderError> errors = new ArrayList<>();
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.of(extractBids(bidResponse, errors), errors);
} catch (DecodeException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidResponse bidResponse, List<BidderError> errors) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
return bidsFromResponse(bidResponse, errors);
}

private static List<BidderBid> bidsFromResponse(BidResponse bidResponse, List<BidderError> errors) {
return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> makeBidderBid(bid, bidResponse.getCur(), errors))
.filter(Objects::nonNull)
.toList();
}

private static BidderBid makeBidderBid(Bid bid, String currency, List<BidderError> errors) {
try {
return BidderBid.of(bid, getBidType(bid), currency);
} catch (PreBidException e) {
errors.add(BidderError.badServerResponse(e.getMessage()));
return null;
}
}

private static BidType getBidType(Bid bid) {
final Integer markupType = bid.getMtype();
if (markupType == null) {
throw new PreBidException("Missing mediaType for bid: %s".formatted(bid.getId()));
}

return switch (markupType) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
default -> throw new PreBidException(
"Unsupported bid mediaType: %s for impression: %s".formatted(bid.getMtype(), bid.getImpid()));
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.prebid.server.proto.openrtb.ext.request.minutemedia;

import lombok.Value;

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

String org;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.prebid.server.spring.config.bidder;

import org.prebid.server.bidder.BidderDeps;
import org.prebid.server.bidder.minutemedia.MinuteMediaBidder;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.spring.config.bidder.model.BidderConfigurationProperties;
import org.prebid.server.spring.config.bidder.util.BidderDepsAssembler;
import org.prebid.server.spring.config.bidder.util.UsersyncerCreator;
import org.prebid.server.spring.env.YamlPropertySourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.validation.constraints.NotBlank;

@Configuration
@PropertySource(value = "classpath:/bidder-config/minutemedia.yaml", factory = YamlPropertySourceFactory.class)
public class MinuteMediaConfiguration {

private static final String BIDDER_NAME = "minutemedia";

@Bean("minutemediaConfigurationProperties")
@ConfigurationProperties("adapters.minutemedia")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps minutemediaBidderDeps(BidderConfigurationProperties minutemediaConfigurationProperties,
@NotBlank @Value("${external-url}") String externalUrl,
JacksonMapper mapper) {

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(minutemediaConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new MinuteMediaBidder(config.getEndpoint(), mapper))
.assemble();
}
}
19 changes: 19 additions & 0 deletions src/main/resources/bidder-config/minutemedia.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
adapters:
minutemedia:
endpoint: https://pbs.minutemedia-prebid.com/pbs-mm?publisher_id={{PublisherId}}
modifying-vast-xml-allowed: true
meta-info:
maintainer-email: [email protected]
app-media-types:
- banner
- video
site-media-types:
- banner
- video
vendor-id: 918
usersync:
cookie-family-name: minutemedia
iframe:
url: https://pbs-cs.minutemedia-prebid.com/pbs-iframe?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&gpp={{gpp}}&gpp_sid={{gpp_sid}}&redirect={{redirect_url}}
support-cors: false
uid-macro: '[PBS_UID]'
16 changes: 16 additions & 0 deletions src/main/resources/static/bidder-params/minutemedia.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "MinuteMedia Adapter Params",
"description": "A schema which validates params accepted by the MinuteMedia adapter",
"type": "object",
"properties": {
"org": {
"type": "string",
"description": "The organization ID.",
"minLength": 1
}
},
"required": [
"org"
]
}
Loading
Loading