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

MetaX: New bidder #3386

Merged
merged 3 commits into from
Aug 21, 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
158 changes: 158 additions & 0 deletions src/main/java/org/prebid/server/bidder/metax/MetaxBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.prebid.server.bidder.metax;

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.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.metax.ExtImpMetax;
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;
import java.util.Optional;

public class MetaxBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpMetax>> METAX_EXT_TYPE_REFERENCE =
new TypeReference<>() {
};
private static final String PUBLISHER_ID_MACRO = "{{publisherId}}";
private static final String AD_UNIT_MACRO = "{{adUnit}}";

private final String endpointUrl;
private final JacksonMapper mapper;

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

@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) {
final List<BidderError> errors = new ArrayList<>();
final List<HttpRequest<BidRequest>> httpRequests = new ArrayList<>();

for (Imp imp : request.getImp()) {
try {
final ExtImpMetax extImpMetax = parseImpExt(imp);
httpRequests.add(BidderUtil.defaultRequest(prepareBidRequest(request, imp),
resolveEndpoint(extImpMetax),
mapper));
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
}
}

return Result.of(httpRequests, errors);
}

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

private static BidRequest prepareBidRequest(BidRequest bidRequest, Imp imp) {
return bidRequest.toBuilder()
.imp(Collections.singletonList(modifyImp(imp)))
.build();
}

private static Imp modifyImp(Imp imp) {
final Banner banner = imp.getBanner();
final Integer width = banner != null ? banner.getW() : null;
final Integer height = banner != null ? banner.getH() : null;
if (width != null && height != null) {
return imp;
}

final List<Format> formats = banner != null ? banner.getFormat() : null;
if (CollectionUtils.isEmpty(formats)) {
return imp;
}

final Format firstFormat = formats.getFirst();
return imp.toBuilder()
.banner(banner.toBuilder()
.w(Optional.ofNullable(firstFormat).map(Format::getW).orElse(0))
.h(Optional.ofNullable(firstFormat).map(Format::getH).orElse(0))
.build())
.build();
}

private String resolveEndpoint(ExtImpMetax extImpMetax) {
final String publisherIdAsString = Optional.ofNullable(extImpMetax.getPublisherId())
.map(Object::toString)
.orElse(StringUtils.EMPTY);
final String adUnitAsString = Optional.ofNullable(extImpMetax.getAdUnit())
.map(Object::toString)
.orElse(StringUtils.EMPTY);

return endpointUrl
.replace(PUBLISHER_ID_MACRO, publisherIdAsString)
.replace(AD_UNIT_MACRO, adUnitAsString);
}

@Override
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(bidResponse));
} catch (DecodeException | PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

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

return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid).filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(Objects::nonNull)
.map(bid -> BidderBid.of(bid, getBidType(bid), bidResponse.getCur()))
.toList();
}

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

return switch (markupType) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
case 3 -> BidType.audio;
case 4 -> BidType.xNative;
default -> throw new PreBidException("Unsupported MType: %s"
.formatted(bid.getImpid()));
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.prebid.server.proto.openrtb.ext.request.metax;

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

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

@JsonProperty("publisherId")
Integer publisherId;

@JsonProperty("adunit")
Integer adUnit;
}
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.metax.MetaxBidder;
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 jakarta.validation.constraints.NotBlank;

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

private static final String BIDDER_NAME = "metax";

@Bean("metaxConfigurationProperties")
@ConfigurationProperties("adapters.metax")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

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

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(metaxConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new MetaxBidder(config.getEndpoint(), mapper))
.assemble();
}
}
18 changes: 18 additions & 0 deletions src/main/resources/bidder-config/metax.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# The MetaX Bidding adapter requires setup before beginning. Please contact us at <[email protected]>
adapters:
metax:
endpoint: https://hb.metaxads.com/prebid?sid={{publisherId}}&adunit={{adUnit}}&source=prebid-server
meta-info:
maintainer-email: [email protected]
app-media-types:
- banner
- video
- native
- audio
site-media-types:
- banner
- video
- native
- audio
supported-vendors:
vendor-id: 1301
22 changes: 22 additions & 0 deletions src/main/resources/static/bidder-params/metax.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "MetaX Adapter Params",
"description": "A schema which validates params accepted by the MetaX adapter",
"type": "object",
"properties": {
"publisherId": {
"type": "integer",
"description": "An ID which identifies the publisher",
"minimum": 1
},
"adunit": {
"type": "integer",
"description": "An ID which identifies the adunit",
"minimum": 1
}
},
"required": [
"publisherId",
"adunit"
]
}
Loading
Loading