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

Add new Pangle Bidder #1190

Merged
merged 4 commits into from
Apr 27, 2021
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
207 changes: 207 additions & 0 deletions src/main/java/org/prebid/server/bidder/pangle/PangleBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package org.prebid.server.bidder.pangle;

import com.fasterxml.jackson.databind.node.ObjectNode;
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.MultiMap;
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.BidderError;
import org.prebid.server.bidder.model.HttpCall;
import org.prebid.server.bidder.model.HttpRequest;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.bidder.pangle.model.BidExt;
import org.prebid.server.bidder.pangle.model.PangleBidExt;
import org.prebid.server.bidder.pangle.model.WrappedImpExtBidder;
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.request.ExtImpPrebid;
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;
import java.util.stream.Collectors;

/**
* Pangle {@link Bidder} implementation.
*/
public class PangleBidder implements Bidder<BidRequest> {

private final String endpointUrl;
private final JacksonMapper mapper;

public PangleBidder(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>> requests = new ArrayList<>();
final List<BidderError> errors = new ArrayList<>();

for (Imp imp : request.getImp()) {
try {
final WrappedImpExtBidder extBidder = parseImpExt(imp);
final Integer adType = resolveAdType(imp, extBidder);
final Imp modifiedImp = modifyImp(imp, adType, extBidder);

requests.add(createRequest(request, modifiedImp, extBidder.getBidder().getToken()));
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
}
}

return Result.of(requests, errors);
}

private WrappedImpExtBidder parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), WrappedImpExtBidder.class);
} catch (IllegalArgumentException e) {
throw new PreBidException(String.format("failed unmarshalling imp ext (err)%s", e.getMessage()));
}
}

private static int resolveAdType(Imp imp, WrappedImpExtBidder extBidder) {
if (imp.getVideo() != null) {
final ExtImpPrebid extPrebid = extBidder != null ? extBidder.getPrebid() : null;
final Integer isRewardedInventory = extPrebid != null ? extPrebid.getIsRewardedInventory() : null;
if (Objects.equals(isRewardedInventory, 1)) {
return 7;
}

if (Objects.equals(imp.getInstl(), 1)) {
return 8;
}
}

if (imp.getBanner() != null) {
if (Objects.equals(imp.getInstl(), 1)) {
return 2;
} else {
return 1;
}
}
if (imp.getXNative() != null && StringUtils.isNotBlank(imp.getXNative().getRequest())) {
return 5;
}

throw new PreBidException("not a supported adtype");
}

private Imp modifyImp(Imp imp, Integer adType, WrappedImpExtBidder extBidder) {
final WrappedImpExtBidder updatedImpExt = extBidder.toBuilder().adType(adType).build();
return imp.toBuilder()
.ext(mapper.mapper().convertValue(updatedImpExt, ObjectNode.class))
.build();
}

private HttpRequest<BidRequest> createRequest(BidRequest request, Imp imp, String token) {
final BidRequest outgoingRequest = request.toBuilder().imp(Collections.singletonList(imp)).build();

return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(makeHeaders(token))
.payload(outgoingRequest)
.body(mapper.encode(outgoingRequest))
.build();
}

private static MultiMap makeHeaders(String token) {
return HttpUtil.headers()
.add("TOKEN", token);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can token be null ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

From pangle.json
"required": [
"token"
]
Seems it can't be null

}

@Override
public final Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final List<BidderError> errors = new ArrayList<>();
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 List<BidderBid> extractBids(BidResponse bidResponse, List<BidderError> errors) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
return bidsFromResponse(bidResponse, errors);
}

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

private BidderBid createBid(Bid bid, String currency, List<BidderError> errors) {
final Integer adType;
try {
adType = getAdTypeFromBidExt(bid);
} catch (PreBidException e) {
errors.add(BidderError.badServerResponse(e.getMessage()));
return null;
}
final BidType bidType;
switch (adType) {
case 1:
bidType = BidType.banner;
break;
case 2:
bidType = BidType.banner;
break;
case 5:
bidType = BidType.xNative;
break;
case 7:
bidType = BidType.video;
break;
case 8:
bidType = BidType.video;
break;
default:
errors.add(BidderError.badServerResponse("unrecognized adtype in response"));
return null;
}

return BidderBid.of(bid, bidType, currency);
}

private Integer getAdTypeFromBidExt(Bid bid) {
if (bid == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add .filter(Objects::nonNull) while looping through the bids?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

From Go implementation, they want to add errors about not present bids

throw new PreBidException("the bid request object is not present");
}
final PangleBidExt bidExt;
try {
bidExt = mapper.mapper().convertValue(bid.getExt(), PangleBidExt.class);
} catch (IllegalArgumentException e) {
throw new PreBidException("invalid bid ext");
}

final BidExt pangleBidExt = bidExt != null ? bidExt.getPangle() : null;
final Integer adType = pangleBidExt != null ? pangleBidExt.getAdType() : null;
if (adType == null) {
throw new PreBidException("missing pangleExt/adtype in bid ext");
}
return adType;
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/prebid/server/bidder/pangle/model/BidExt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.prebid.server.bidder.pangle.model;

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

@AllArgsConstructor(staticName = "of")
@Builder(toBuilder = true)
@Value
public class BidExt {

@JsonProperty("adtype")
Integer adType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.prebid.server.bidder.pangle.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Value;

@AllArgsConstructor(staticName = "of")
@Builder(toBuilder = true)
@Value
public class PangleBidExt {

BidExt pangle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.prebid.server.bidder.pangle.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Value;
import org.prebid.server.proto.openrtb.ext.request.ExtImpPrebid;
import org.prebid.server.proto.openrtb.ext.request.pangle.ExtImpPangle;

@AllArgsConstructor(staticName = "of")
@Builder(toBuilder = true)
@Value
public class WrappedImpExtBidder {

ExtImpPrebid prebid;

ExtImpPangle bidder;

@JsonProperty("adtype")
Integer adType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.prebid.server.proto.openrtb.ext.request.pangle;

import lombok.AllArgsConstructor;
import lombok.Value;

@AllArgsConstructor(staticName = "of")
@Value
public class ExtImpPangle {

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

import org.prebid.server.bidder.BidderDeps;
import org.prebid.server.bidder.pangle.PangleBidder;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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/pangle.yaml", factory = YamlPropertySourceFactory.class)
public class PangleConfiguration {

private static final String BIDDER_NAME = "pangle";

@Value("${external-url}")
@NotBlank
private String externalUrl;

@Autowired
private JacksonMapper mapper;

@Autowired
@Qualifier("pangleConfigurationProperties")
private BidderConfigurationProperties configProperties;

@Bean("pangleConfigurationProperties")
@ConfigurationProperties("adapters.pangle")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps pangleBidderDeps() {
return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(configProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new PangleBidder(config.getEndpoint(), mapper))
.assemble();
}
}
24 changes: 24 additions & 0 deletions src/main/resources/bidder-config/pangle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
adapters:
pangle:
enabled: false
endpoint: https://
pbs-enforces-gdpr: true
pbs-enforces-ccpa: true
modifying-vast-xml-allowed: true
deprecated-names:
aliases: {}
meta-info:
maintainer-email: pangle_dsp@bytedance.com
app-media-types:
- banner
- video
- native
site-media-types:
supported-vendors:
vendor-id: 0
usersync:
url:
redirect-url:
cookie-family-name: pangle
type: redirect
support-cors: false
16 changes: 16 additions & 0 deletions src/main/resources/static/bidder-params/pangle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Pangle Adapter Params",
"description": "A schema which validates params accepted by the Pangle adapter",
"type": "object",
"properties": {
"token": {
"type": "string",
"description": "Access Token",
"pattern": ".+"
}
},
"required": [
"token"
]
}
Loading