-
Notifications
You must be signed in to change notification settings - Fork 175
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
Tradplus #3508
Open
tradplus
wants to merge
5
commits into
prebid:master
Choose a base branch
from
tradplus:tradplus
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+646
−0
Open
Tradplus #3508
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4f2b2dd
New Adapter: TradPlus
tradplus 6ee24b5
TradPlus: Update bidder
tradplus eed13bc
Merge branch 'master' into tradplus
tradplus cef48a1
feat: change some code by reviewer commment
tradplus 77816f0
Merge branch 'prebid:master' into tradplus
tradplus 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
143 changes: 143 additions & 0 deletions
143
src/main/java/org/prebid/server/bidder/tradplus/TradPlusBidder.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,143 @@ | ||
package org.prebid.server.bidder.tradplus; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.iab.openrtb.request.BidRequest; | ||
import com.iab.openrtb.request.Imp; | ||
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.tradplus.ExtImpTradPlus; | ||
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.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class TradPlusBidder implements Bidder<BidRequest> { | ||
|
||
private static final TypeReference<ExtPrebid<?, ExtImpTradPlus>> EXT_TYPE_REFERENCE = | ||
new TypeReference<>() { | ||
}; | ||
|
||
private static final String ZONE_ID = "{{ZoneID}}"; | ||
private static final String ACCOUNT_ID = "{{AccountID}}"; | ||
|
||
private final String endpointUrl; | ||
private final JacksonMapper mapper; | ||
|
||
public TradPlusBidder(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 Map<ExtImpTradPlus, List<Imp>> extToImps = new HashMap<>(); | ||
try { | ||
final ExtImpTradPlus extImpTradPlus = parseImpExt(bidRequest.getImp().getFirst().getExt()); | ||
validateImpExt(extImpTradPlus); | ||
extToImps.computeIfAbsent(extImpTradPlus, ext -> new ArrayList<>()).add(bidRequest.getImp().getFirst()); | ||
} catch (PreBidException e) { | ||
return Result.withError(BidderError.badInput(e.getMessage())); | ||
} | ||
|
||
final List<HttpRequest<BidRequest>> httpRequests = extToImps.entrySet().stream() | ||
.map(entry -> makeHttpRequest(entry.getKey(), entry.getValue(), bidRequest)) | ||
.toList(); | ||
|
||
return Result.withValues(httpRequests); | ||
} | ||
|
||
private ExtImpTradPlus parseImpExt(ObjectNode extNode) { | ||
final ExtImpTradPlus extImpTradPlus; | ||
try { | ||
extImpTradPlus = mapper.mapper().convertValue(extNode, EXT_TYPE_REFERENCE).getBidder(); | ||
} catch (IllegalArgumentException e) { | ||
throw new PreBidException(e.getMessage()); | ||
} | ||
return extImpTradPlus; | ||
} | ||
|
||
private void validateImpExt(ExtImpTradPlus extImpTradPlus) { | ||
if (StringUtils.isBlank(extImpTradPlus.getAccountId())) { | ||
throw new PreBidException("Invalid/Missing AccountID"); | ||
} | ||
} | ||
|
||
private HttpRequest<BidRequest> makeHttpRequest(ExtImpTradPlus extImpTradPlus, List<Imp> imps, | ||
BidRequest bidRequest) { | ||
final String uri = endpointUrl | ||
.replace(ZONE_ID, extImpTradPlus.getZoneId()) | ||
.replace(ACCOUNT_ID, extImpTradPlus.getAccountId()); | ||
|
||
final BidRequest outgoingRequest = bidRequest.toBuilder().imp(removeFirstImpExt(imps)).build(); | ||
|
||
return BidderUtil.defaultRequest(outgoingRequest, uri, mapper); | ||
} | ||
|
||
private static List<Imp> removeFirstImpExt(List<Imp> imps) { | ||
imps.set(0, imps.getFirst().toBuilder().ext(null).build()); | ||
return imps; | ||
} | ||
Comment on lines
+96
to
+99
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. this looks redundant since the list of imps always has a single imp |
||
|
||
@Override | ||
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
try { | ||
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
return Result.of(extractBids(bidResponse, httpCall.getRequest().getPayload()), Collections.emptyList()); | ||
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.
|
||
} catch (DecodeException | PreBidException e) { | ||
return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
} | ||
} | ||
|
||
private static List<BidderBid> extractBids(BidResponse bidResponse, BidRequest bidRequest) { | ||
return bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid()) | ||
? Collections.emptyList() | ||
: bidsFromResponse(bidResponse, bidRequest.getImp()); | ||
} | ||
|
||
private static List<BidderBid> bidsFromResponse(BidResponse bidResponse, List<Imp> imps) { | ||
return bidResponse.getSeatbid().stream() | ||
.filter(Objects::nonNull) | ||
.map(SeatBid::getBid) | ||
.filter(Objects::nonNull) | ||
.flatMap(Collection::stream) | ||
.map(bid -> BidderBid.of(bid, getBidType(bid.getImpid(), imps), bidResponse.getCur())) | ||
.toList(); | ||
} | ||
|
||
private static BidType getBidType(String impId, List<Imp> imps) { | ||
for (Imp imp : imps) { | ||
if (imp.getId().equals(impId)) { | ||
if (imp.getVideo() != null) { | ||
return BidType.video; | ||
} | ||
if (imp.getXNative() != null) { | ||
return BidType.xNative; | ||
} | ||
return BidType.banner; | ||
} | ||
} | ||
throw new PreBidException("Invalid bid imp ID #%s does not match any imp IDs from the original bid request" | ||
.formatted(impId)); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/org/prebid/server/proto/openrtb/ext/request/tradplus/ExtImpTradPlus.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,14 @@ | ||
package org.prebid.server.proto.openrtb.ext.request.tradplus; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Value; | ||
|
||
@Value(staticConstructor = "of") | ||
public class ExtImpTradPlus { | ||
|
||
@JsonProperty("accountId") | ||
String accountId; | ||
|
||
@JsonProperty("zoneId") | ||
String zoneId; | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/prebid/server/spring/config/bidder/TradPlusBidderConfiguration.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,41 @@ | ||
package org.prebid.server.spring.config.bidder; | ||
|
||
import org.prebid.server.bidder.BidderDeps; | ||
import org.prebid.server.bidder.tradplus.TradPlusBidder; | ||
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/tradplus.yaml", factory = YamlPropertySourceFactory.class) | ||
public class TradPlusBidderConfiguration { | ||
|
||
private static final String BIDDER_NAME = "tradplus"; | ||
|
||
@Bean("tradplusConfigurationProperties") | ||
@ConfigurationProperties("adapters.tradplus") | ||
BidderConfigurationProperties configurationProperties() { | ||
return new BidderConfigurationProperties(); | ||
} | ||
|
||
@Bean | ||
BidderDeps tradplusBidderDeps(BidderConfigurationProperties tradplusConfigurationProperties, | ||
@NotBlank @Value("${external-url}") String externalUrl, | ||
JacksonMapper mapper) { | ||
|
||
return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
.withConfig(tradplusConfigurationProperties) | ||
.usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
.bidderCreator(config -> new TradPlusBidder(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,11 @@ | ||
adapters: | ||
tradplus: | ||
endpoint: "https://{{ZoneID}}adx.tradplusad.com/{{AccountID}}/pserver" | ||
meta-info: | ||
maintainer-email: "[email protected]" | ||
app-media-types: | ||
- banner | ||
- video | ||
- native | ||
supported-vendors: | ||
vendor-id: 0 |
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 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "TradPlus Adapter Params", | ||
"description": "A schema which validates params accepted by the TradPlus adapter", | ||
"type": "object", | ||
"properties": { | ||
"accountId": { | ||
"type": "string", | ||
"description": "Account ID", | ||
"minLength": 1 | ||
}, | ||
"zoneId": { | ||
"type": "string", | ||
"description": "Zone ID" | ||
} | ||
}, | ||
"required": [ | ||
"accountId", | ||
"zoneId" | ||
] | ||
} |
Oops, something went wrong.
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.
using a map here is an overhead since only one single imp is really utilized to retrieve the ext object, please get rid of it