-
Notifications
You must be signed in to change notification settings - Fork 181
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
Add new Pangle Bidder #1190
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
207 changes: 207 additions & 0 deletions
207
src/main/java/org/prebid/server/bidder/pangle/PangleBidder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
@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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
15
src/main/java/org/prebid/server/bidder/pangle/model/BidExt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/prebid/server/bidder/pangle/model/PangleBidExt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/prebid/server/bidder/pangle/model/WrappedImpExtBidder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/prebid/server/proto/openrtb/ext/request/pangle/ExtImpPangle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/prebid/server/spring/config/bidder/PangleConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can token be null ?
There was a problem hiding this comment.
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