-
Notifications
You must be signed in to change notification settings - Fork 190
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
New adapter readpeak (#3142) #3198
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8d3377d
ReadPeak : New adapter (#3142)
przemkaczmarek 2fa2343
ReadPeak : new Adapter
przemkaczmarek f151fbd
ReadPeak : new Adapter
przemkaczmarek 1e7ec8b
resolve comments
przemkaczmarek 68a1434
resolve comments
przemkaczmarek 49a0d30
resolve comments
przemkaczmarek 6e60898
resolve comments
przemkaczmarek 209780f
New adapter: ReadPeak
przemkaczmarek 98588a1
New adapter: ReadPeak
przemkaczmarek 51a5b6d
New adapter: ReadPeak
przemkaczmarek 14a31d7
resolve comments
przemkaczmarek 6a62f01
resolve comments
przemkaczmarek 12ec3c7
resolve comments
przemkaczmarek 61c4604
resolve comments
przemkaczmarek 32f5be4
resolve comments
przemkaczmarek f3573d4
resolve comments
przemkaczmarek 4675508
resolve comments
przemkaczmarek 704c4d6
Fix comments
AntoxaAntoxic 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
223 changes: 223 additions & 0 deletions
223
src/main/java/org/prebid/server/bidder/readpeak/ReadPeakBidder.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,223 @@ | ||
package org.prebid.server.bidder.readpeak; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.iab.openrtb.request.App; | ||
import com.iab.openrtb.request.BidRequest; | ||
import com.iab.openrtb.request.Imp; | ||
import com.iab.openrtb.request.Publisher; | ||
import com.iab.openrtb.request.Site; | ||
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.ObjectUtils; | ||
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.readpeak.ExtImpReadPeak; | ||
import org.prebid.server.proto.openrtb.ext.response.BidType; | ||
import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid; | ||
import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebidMeta; | ||
import org.prebid.server.util.BidderUtil; | ||
import org.prebid.server.util.HttpUtil; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class ReadPeakBidder implements Bidder<BidRequest> { | ||
|
||
private static final TypeReference<ExtPrebid<?, ExtImpReadPeak>> READPEAK_EXT_TYPE_REFERENCE = | ||
new TypeReference<>() { | ||
}; | ||
|
||
private static final String PRICE_MACRO = "${AUCTION_PRICE}"; | ||
|
||
private final String endpointUrl; | ||
private final JacksonMapper mapper; | ||
|
||
public ReadPeakBidder(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<Imp> modifiedImps = new ArrayList<>(); | ||
final List<ExtImpReadPeak> extImpReadPeaks = new ArrayList<>(); | ||
final List<BidderError> errors = new ArrayList<>(); | ||
|
||
for (Imp imp : request.getImp()) { | ||
try { | ||
final ExtImpReadPeak extImpReadPeak = parseImpExt(imp); | ||
final Imp modifiedImp = modifyImp(imp, extImpReadPeak); | ||
modifiedImps.add(modifiedImp); | ||
extImpReadPeaks.add(extImpReadPeak); | ||
} catch (PreBidException e) { | ||
errors.add(BidderError.badInput(e.getMessage())); | ||
} | ||
} | ||
|
||
if (modifiedImps.isEmpty()) { | ||
return Result.withError(BidderError | ||
.badInput(String.format("Failed to find compatible impressions for request %s", request.getId()))); | ||
} | ||
|
||
final BidRequest modifiedRequest = request.toBuilder().imp(modifiedImps).build(); | ||
final HttpRequest<BidRequest> httpRequest = makeHttpRequest(modifiedRequest, extImpReadPeaks.get(0)); | ||
|
||
return Result.of(Collections.singletonList(httpRequest), errors); | ||
} | ||
|
||
private ExtImpReadPeak parseImpExt(Imp imp) { | ||
try { | ||
return mapper.mapper().convertValue(imp.getExt(), READPEAK_EXT_TYPE_REFERENCE).getBidder(); | ||
} catch (IllegalArgumentException e) { | ||
throw new PreBidException("Failed to deserialize ReadPeak extension: " + e.getMessage()); | ||
} | ||
} | ||
|
||
private Imp modifyImp(Imp imp, ExtImpReadPeak extImpReadPeak) { | ||
return imp.toBuilder() | ||
.bidfloor(extImpReadPeak.getBidFloor() != null ? extImpReadPeak.getBidFloor() : imp.getBidfloor()) | ||
.tagid(StringUtils.isNotBlank(extImpReadPeak.getTagId()) ? extImpReadPeak.getTagId() : imp.getTagid()) | ||
.build(); | ||
} | ||
|
||
private HttpRequest<BidRequest> makeHttpRequest(BidRequest request, ExtImpReadPeak extImpReadPeak) { | ||
final Publisher publisher = Publisher.builder() | ||
.id(extImpReadPeak.getPublisherId()) | ||
.build(); | ||
|
||
final BidRequest.BidRequestBuilder requestBuilder = request.toBuilder(); | ||
|
||
final boolean hasSite = request.getSite() != null; | ||
final boolean hasApp = !hasSite && request.getApp() != null; | ||
|
||
final BidRequest outgoingRequest = requestBuilder | ||
.site(hasSite ? modifySite(request.getSite(), | ||
extImpReadPeak.getSiteId(), publisher) : request.getSite()) | ||
.app(hasApp ? modifyApp(request.getApp(), extImpReadPeak, publisher) : request.getApp()) | ||
.build(); | ||
|
||
return BidderUtil.defaultRequest(outgoingRequest, endpointUrl, mapper); | ||
} | ||
|
||
private Site modifySite(Site site, String siteId, Publisher publisher) { | ||
return site.toBuilder() | ||
.id(StringUtils.isNotBlank(siteId) ? siteId : site.getId()) | ||
.publisher(publisher) | ||
.build(); | ||
} | ||
|
||
private App modifyApp(App app, ExtImpReadPeak extImpReadPeak, Publisher publisher) { | ||
return app.toBuilder() | ||
.id(StringUtils.isNotBlank(extImpReadPeak.getSiteId()) ? extImpReadPeak.getSiteId() : app.getId()) | ||
.publisher(publisher) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
try { | ||
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
return extractBids(bidResponse); | ||
} catch (DecodeException e) { | ||
return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
} | ||
} | ||
|
||
private Result<List<BidderBid>> extractBids(BidResponse bidResponse) { | ||
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { | ||
return Result.withValues(Collections.emptyList()); | ||
} | ||
return bidsFromResponse(bidResponse); | ||
} | ||
|
||
private Result<List<BidderBid>> bidsFromResponse(BidResponse bidResponse) { | ||
final List<BidderError> errors = new ArrayList<>(); | ||
final List<BidderBid> validBids = new ArrayList<>(); | ||
|
||
for (SeatBid seatBid : bidResponse.getSeatbid()) { | ||
if (seatBid != null) { | ||
for (Bid bid : seatBid.getBid()) { | ||
if (bid != null) { | ||
try { | ||
final BidderBid bidderBid = makeBid(bid, bidResponse); | ||
validBids.add(bidderBid); | ||
} catch (PreBidException e) { | ||
errors.add(BidderError.badInput(e.getMessage())); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!errors.isEmpty()) { | ||
return Result.withErrors(errors); | ||
} | ||
|
||
return Result.withValues(validBids); | ||
} | ||
|
||
private BidderBid makeBid(Bid bid, BidResponse bidResponse) { | ||
final Bid resolvedBid = resolveMacros(bid); | ||
final BidType bidType = getBidType(bid); | ||
final Bid updatedBid = addBidMeta(resolvedBid); | ||
return BidderBid.of(updatedBid, bidType, bidResponse.getCur()); | ||
} | ||
|
||
private Bid resolveMacros(Bid bid) { | ||
final BigDecimal price = bid.getPrice(); | ||
final String priceAsString = price != null ? price.toPlainString() : "0"; | ||
|
||
return bid.toBuilder() | ||
.nurl(StringUtils.replace(bid.getNurl(), PRICE_MACRO, priceAsString)) | ||
.adm(StringUtils.replace(bid.getAdm(), PRICE_MACRO, priceAsString)) | ||
.burl(StringUtils.replace(bid.getBurl(), PRICE_MACRO, priceAsString)) | ||
.build(); | ||
} | ||
|
||
private BidType getBidType(Bid bid) { | ||
final Integer markupType = ObjectUtils.defaultIfNull(bid.getMtype(), 0); | ||
|
||
return switch (markupType) { | ||
case 1 -> BidType.banner; | ||
case 4 -> BidType.xNative; | ||
default -> throw new PreBidException( | ||
"Unable to fetch mediaType " + bid.getMtype() + " in multi-format: " + bid.getImpid()); | ||
}; | ||
przemkaczmarek marked this conversation as resolved.
Show resolved
Hide resolved
AntoxaAntoxic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private Bid addBidMeta(Bid bid) { | ||
przemkaczmarek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final ExtBidPrebidMeta extBidPrebidMeta = ExtBidPrebidMeta.builder() | ||
.advertiserDomains(bid.getAdomain()) | ||
.build(); | ||
|
||
final ExtBidPrebidMeta modifiedMeta = ExtBidPrebidMeta.builder() | ||
.advertiserDomains(bid.getAdomain()) | ||
.build(); | ||
|
||
final ExtBidPrebid modifiedPrebid = ExtBidPrebid.builder() | ||
.meta(modifiedMeta) | ||
.build(); | ||
|
||
final ObjectNode modifiedBidExt = mapper.mapper() | ||
.valueToTree(ExtPrebid.of(modifiedPrebid, null)); | ||
|
||
return bid.toBuilder() | ||
.ext(modifiedBidExt) | ||
.build(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/org/prebid/server/proto/openrtb/ext/request/readpeak/ExtImpReadPeak.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,22 @@ | ||
package org.prebid.server.proto.openrtb.ext.request.readpeak; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Value; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@Value(staticConstructor = "of") | ||
public class ExtImpReadPeak { | ||
|
||
@JsonProperty("publisherId") | ||
String publisherId; | ||
|
||
@JsonProperty("siteId") | ||
String siteId; | ||
|
||
@JsonProperty("bidfloor") | ||
BigDecimal bidFloor; | ||
|
||
@JsonProperty("tagId") | ||
String tagId; | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/prebid/server/spring/config/bidder/ReadPeakConfiguration.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.readpeak.ReadPeakBidder; | ||
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/readpeak.yaml", factory = YamlPropertySourceFactory.class) | ||
public class ReadPeakConfiguration { | ||
|
||
private static final String BIDDER_NAME = "readpeak"; | ||
|
||
@Bean("readpeakConfigurationProperties") | ||
@ConfigurationProperties("adapters.readpeak") | ||
BidderConfigurationProperties configurationProperties() { | ||
return new BidderConfigurationProperties(); | ||
} | ||
|
||
@Bean | ||
BidderDeps readpeakBidderDeps(BidderConfigurationProperties readpeakConfigurationProperties, | ||
@NotBlank @Value("${external-url}") String externalUrl, | ||
JacksonMapper mapper) { | ||
|
||
return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
.withConfig(readpeakConfigurationProperties) | ||
.usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
.bidderCreator(config -> new ReadPeakBidder(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,15 @@ | ||
adapters: | ||
readpeak: | ||
endpoint: https://dsp.readpeak.com/header/prebid | ||
geoscope: | ||
- EEA | ||
meta-info: | ||
maintainer-email: devteam@readpeak.com | ||
app-media-types: | ||
- banner | ||
- native | ||
site-media-types: | ||
- banner | ||
- native | ||
supported-vendors: | ||
vendor-id: 290 |
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,25 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "Readpeak Adapter Params", | ||
"description": "A schema which validates params accepted by the Readpeak adapter", | ||
"type": "object", | ||
"properties": { | ||
"publisherId": { | ||
"type": "string", | ||
"description": "Publisher ID provided by Readpeak" | ||
}, | ||
"siteId": { | ||
"type": "string", | ||
"description": "Site/Media ID provided by Readpeak" | ||
}, | ||
"bidfloor": { | ||
"type": "number", | ||
"description": "CPM Bid Floor" | ||
}, | ||
"tagId": { | ||
"type": "string", | ||
"description": "Ad placement identifier" | ||
} | ||
}, | ||
"required": ["publisherId"] | ||
} |
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.
please use the similar style