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

Core: Add new bwx bidder #3095

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

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 io.vertx.core.http.HttpMethod;
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.bwx.ExtImpBwx;
import org.prebid.server.proto.openrtb.ext.response.BidType;
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 BwxBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpBwx>> BWX_EXT_TYPE_REFERENCE =
new TypeReference<>() {
};
private static final String URL_HOST_MACRO = "{{Host}}";
private static final String PUBLISHER_ID_MACRO = "{{SourceId}}";
private final String endpointUrl;
private final JacksonMapper mapper;

public BwxBidder(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<HttpRequest<BidRequest>> httpRequests = new ArrayList<>();
final List<BidderError> errors = new ArrayList<>();

for (Imp imp : request.getImp()) {
final ExtImpBwx extImpBwx;
try {
extImpBwx = parseImpExt(imp);
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
continue;
}
httpRequests.add(createHttpRequest(request, extImpBwx));
}

return Result.of(httpRequests, errors);
}

private ExtImpBwx parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), BWX_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException("Missing bidder ext in impression with id: " + imp.getId());
}
}

private HttpRequest<BidRequest> createHttpRequest(BidRequest request, ExtImpBwx extImpBwx) {
return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(resolveEndpoint(extImpBwx))
.body(mapper.encodeToBytes(request))
.payload(request)
.headers(HttpUtil.headers())
.build();
}

private String resolveEndpoint(ExtImpBwx extImpBwx) {
return endpointUrl
.replace(URL_HOST_MACRO, StringUtils.defaultString(extImpBwx.getEnv()))
.replace(PUBLISHER_ID_MACRO, StringUtils.defaultString(extImpBwx.getPid()));
}

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

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

return bidsFromResponse(bidResponse);
}

private List<BidderBid> bidsFromResponse(BidResponse bidResponse) {
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()))
.filter(Objects::nonNull)
.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 4 -> BidType.xNative;
default -> throw new PreBidException(
"Failed to parse bid mtype: %s for impression id %s".formatted(bid.getMtype(), bid.getImpid())
);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.prebid.server.proto.openrtb.ext.request.bwx;

import lombok.Value;

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

String env;

String pid;
}
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.bwx.BwxBidder;
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/bwx.yaml", factory = YamlPropertySourceFactory.class)
public class BwxConfiguration {

private static final String BIDDER_NAME = "bwx";

@Bean("bwxConfigurationProperties")
@ConfigurationProperties("adapters.bwx")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

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

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(bwxConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new BwxBidder(config.getEndpoint(), mapper))
.assemble();
}
}
15 changes: 15 additions & 0 deletions src/main/resources/bidder-config/bwx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
adapters:
bwx:
endpoint: http://rtb.boldwin.live?pid={{SourceId}}&host={{Host}}&pbs=1
meta-info:
maintainer-email: [email protected]
app-media-types:
- banner
- video
- native
site-media-types:
- banner
- video
- native
supported-vendors:
vendor-id: 0
21 changes: 21 additions & 0 deletions src/main/resources/static/bidder-params/bwx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "BoldwinX Adapter Params",
"description": "A schema which validates params accepted by the BoldwinX adapter",
"type": "object",
"properties": {
"pid": {
"type": "string",
"description": "Unique placement ID",
"minLength": 1
},
"env": {
"type": "string",
"description": "BoldwinX environment",
"minLength": 1
}
},
"required": [
"pid"
]
}
Loading
Loading