Repository navigation
Port Peak226: New Adapter #4659 #4700
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
Open
phozhyi
wants to merge
10
commits into
master
Choose a base branch
from
port-peak226
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
42ee885
New Adapter: Peak226 (#4659)
phozhyi 533fa1e
New Adapter: Peak226 (#4659)
phozhyi 327f635
New Adapter: Peak226 (#4659)
phozhyi 4db6e27
New Adapter: Peak226 (#4659)
phozhyi f8f248d
New Adapter: Peak226 (#4659)
phozhyi 7d796a4
New Adapter: Matterfull RTB (#4647)
phozhyi 9a740f5
New Adapter: Matterfull RTB (#4647)
phozhyi 4c7dfaa
New Adapter: Matterfull RTB (#4647)
phozhyi b1a6b98
New Adapter: Matterfull RTB (#4647)
phozhyi c363595
New Adapter: Matterfull RTB (#4647)
phozhyi 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
276 changes: 276 additions & 0 deletions
276
src/main/java/org/prebid/server/bidder/peak226/Peak226Bidder.java
This file contains hidden or 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,276 @@ | ||
| package org.prebid.server.bidder.peak226; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import com.iab.openrtb.request.App; | ||
| import com.iab.openrtb.request.BidRequest; | ||
| import com.iab.openrtb.request.Device; | ||
| 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 org.apache.commons.collections4.CollectionUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.commons.lang3.Strings; | ||
| 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.Price; | ||
| import org.prebid.server.bidder.model.Result; | ||
| import org.prebid.server.bidder.peak226.model.Peak226Response; | ||
| import org.prebid.server.bidder.peak226.model.Peak226SeatBid; | ||
| import org.prebid.server.bidder.peak226.model.RegionPublisher; | ||
| import org.prebid.server.currency.CurrencyConversionService; | ||
| 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.peak226.ExtImpPeak226; | ||
| import org.prebid.server.proto.openrtb.ext.response.BidType; | ||
| import org.prebid.server.util.BidderUtil; | ||
| import org.prebid.server.util.Uri; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| public class Peak226Bidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final TypeReference<ExtPrebid<?, ExtImpPeak226>> EXT_TYPE_REFERENCE = new TypeReference<>() { | ||
| }; | ||
| private static final String USD_CURRENCY = "USD"; | ||
| private static final String DEFAULT_REGION = "us"; | ||
| private static final String ZERO_IFA = "00000000-0000-0000-0000-000000000000"; | ||
| private static final String REGION_MACRO = "Region"; | ||
| private static final String PRICE_MACRO = "${AUCTION_PRICE}"; | ||
|
|
||
| private final Uri endpointUrl; | ||
| private final CurrencyConversionService currencyConversionService; | ||
| private final JacksonMapper mapper; | ||
|
|
||
| public Peak226Bidder(String endpointUrl, | ||
| CurrencyConversionService currencyConversionService, | ||
| JacksonMapper mapper) { | ||
|
|
||
| this.endpointUrl = Uri.of(endpointUrl); | ||
|
phozhyi marked this conversation as resolved.
Resolved
|
||
| this.currencyConversionService = Objects.requireNonNull(currencyConversionService); | ||
| this.mapper = Objects.requireNonNull(mapper); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) { | ||
| final Map<RegionPublisher, List<Imp>> impsByRegion = new LinkedHashMap<>(); | ||
| final List<BidderError> errors = new ArrayList<>(); | ||
|
|
||
| for (Imp imp : bidRequest.getImp()) { | ||
| try { | ||
| final ExtImpPeak226 impExt = parseImpExt(imp); | ||
| final RegionPublisher regionPublisher = computeRegionPublisher(impExt); | ||
| final Price impFloorPrice = getBidFloor(imp, bidRequest); | ||
| final Imp modifiedImp = modifyImp(imp, impExt, impFloorPrice); | ||
|
|
||
| impsByRegion.computeIfAbsent(regionPublisher, _ -> new ArrayList<>()) | ||
| .add(modifiedImp); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| if (impsByRegion.isEmpty()) { | ||
| return Result.withErrors(errors); | ||
| } | ||
|
|
||
| final Device device = sanitizeDevice(bidRequest.getDevice()); | ||
|
|
||
| final List<HttpRequest<BidRequest>> result = impsByRegion.entrySet().stream() | ||
| .map(entry -> | ||
| createSingleRequest(entry.getValue(), bidRequest, entry.getKey(), device)) | ||
| .toList(); | ||
|
|
||
| return Result.of(result, errors); | ||
| } | ||
|
|
||
| private ExtImpPeak226 parseImpExt(Imp imp) throws PreBidException { | ||
| try { | ||
| return mapper.mapper().convertValue(imp.getExt(), EXT_TYPE_REFERENCE).getBidder(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException("Invalid ext. Imp.Id: " + imp.getId()); | ||
| } | ||
| } | ||
|
|
||
| private RegionPublisher computeRegionPublisher(ExtImpPeak226 impExt) { | ||
| final String region = StringUtils.isBlank(impExt.getRegion()) | ||
| ? DEFAULT_REGION | ||
| : impExt.getRegion().toLowerCase(Locale.ROOT); | ||
|
|
||
| return RegionPublisher.of(region, impExt.getPublisherId()); | ||
| } | ||
|
|
||
| private Imp modifyImp(Imp imp, ExtImpPeak226 impExt, Price impFloorPrice) { | ||
| return imp.toBuilder() | ||
| .ext(stripBidderExt(imp)) | ||
| .tagid(impExt.getPlacementId()) | ||
| .bidfloor(impFloorPrice.getValue()) | ||
| .bidfloorcur(impFloorPrice.getCurrency()) | ||
| .build(); | ||
| } | ||
|
|
||
| private ObjectNode stripBidderExt(Imp imp) { | ||
| final ObjectNode modifiedExt = imp.getExt().deepCopy(); | ||
| modifiedExt.remove("bidder"); | ||
| return modifiedExt.isEmpty() ? null : modifiedExt; | ||
| } | ||
|
|
||
| private Price getBidFloor(Imp imp, BidRequest bidRequest) { | ||
| final Price price = Price.of(imp.getBidfloorcur(), imp.getBidfloor()); | ||
| if (BidderUtil.shouldConvertBidFloor(price, USD_CURRENCY)) { | ||
| return convertBidFloor(price, bidRequest); | ||
| } | ||
| return Price.of( | ||
| StringUtils.defaultIfBlank(price.getCurrency(), USD_CURRENCY), | ||
| price.getValue()); | ||
| } | ||
|
|
||
| private Price convertBidFloor(Price bidFloorPrice, BidRequest bidRequest) { | ||
| final BigDecimal convertedPrice = currencyConversionService.convertCurrency( | ||
| bidFloorPrice.getValue(), | ||
| bidRequest, | ||
| bidFloorPrice.getCurrency(), | ||
| USD_CURRENCY); | ||
|
|
||
| return Price.of(USD_CURRENCY, convertedPrice); | ||
| } | ||
|
|
||
| private Device sanitizeDevice(Device device) { | ||
| return device == null || !ZERO_IFA.equals(device.getIfa()) | ||
| ? device | ||
| : device.toBuilder().ifa("").build(); | ||
| } | ||
|
|
||
| private HttpRequest<BidRequest> createSingleRequest(List<Imp> imps, | ||
| BidRequest request, | ||
| RegionPublisher regionPublisher, | ||
| Device device) { | ||
|
|
||
| final BidRequest modifiedRequest = modifyRequest(imps, request, regionPublisher.getPublisherId(), device); | ||
| return BidderUtil.defaultRequest(modifiedRequest, resolveUrl(regionPublisher.getRegion()), mapper); | ||
| } | ||
|
|
||
| private BidRequest modifyRequest(List<Imp> imps, BidRequest request, String publisherId, Device device) { | ||
| final Site site = request.getSite(); | ||
| final App app = request.getApp(); | ||
|
|
||
| return request.toBuilder() | ||
| .device(device) | ||
| .imp(imps) | ||
| .site(site != null ? modifySite(site, publisherId) : null) | ||
| .app(app != null ? modifyApp(app, publisherId) : null) | ||
| .build(); | ||
| } | ||
|
|
||
| private static Site modifySite(Site site, String publisherId) { | ||
| return site.toBuilder().publisher(modifyPublisher(site.getPublisher(), publisherId)).build(); | ||
| } | ||
|
|
||
| private static App modifyApp(App app, String publisherId) { | ||
| return app.toBuilder().publisher(modifyPublisher(app.getPublisher(), publisherId)).build(); | ||
| } | ||
|
|
||
| private static Publisher modifyPublisher(Publisher publisher, String publisherId) { | ||
| return Optional.ofNullable(publisher) | ||
| .map(Publisher::toBuilder) | ||
| .orElseGet(Publisher::builder) | ||
| .id(publisherId) | ||
| .build(); | ||
| } | ||
|
|
||
| private String resolveUrl(String region) { | ||
| return endpointUrl.replaceMacro(REGION_MACRO, region).expand(); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
| try { | ||
| final Peak226Response bidResponse = | ||
| mapper.decodeValue(httpCall.getResponse().getBody(), Peak226Response.class); | ||
| return extractBids(bidResponse); | ||
| } catch (DecodeException | PreBidException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| public Result<List<BidderBid>> extractBids(Peak226Response bidResponse) { | ||
| if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { | ||
| return Result.empty(); | ||
| } | ||
|
|
||
| final List<BidderError> errors = new ArrayList<>(); | ||
| final List<BidderBid> bidderBids = new ArrayList<>(); | ||
| for (Peak226SeatBid seatBid : bidResponse.getSeatbid()) { | ||
| if (CollectionUtils.isEmpty(seatBid.getBid())) { | ||
| continue; | ||
| } | ||
|
|
||
| for (ObjectNode bid : seatBid.getBid()) { | ||
| try { | ||
| bidderBids.add(createBidderBid(bid, bidResponse.getCur())); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return Result.of(bidderBids, errors); | ||
| } | ||
|
|
||
| private BidderBid createBidderBid(ObjectNode bidNode, String currency) { | ||
| try { | ||
| final Bid bid = mapper.mapper().treeToValue(bidNode, Bid.class); | ||
| final Bid modifiedBid = bid.toBuilder().adm(getAdm(bidNode, bid)).build(); | ||
|
|
||
| return BidderBid.of(resolveMacros(modifiedBid), getMediaTypeForBid(bid), currency); | ||
| } catch (JsonProcessingException | IllegalArgumentException e) { | ||
| throw new PreBidException(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private String getAdm(ObjectNode bidNode, Bid bid) { | ||
| final JsonNode admNative = bidNode.at("/adm_native"); | ||
| final String bidAdm = bid.getAdm(); | ||
|
|
||
| return admNative != null && !admNative.isEmpty() && StringUtils.isBlank(bidAdm) | ||
| ? mapper.encodeToString(admNative) | ||
| : bidAdm; | ||
| } | ||
|
|
||
| private Bid resolveMacros(Bid bid) { | ||
| final BigDecimal price = bid.getPrice(); | ||
| final String priceAsString = price != null ? price.toPlainString() : "0"; | ||
|
|
||
| return bid.toBuilder() | ||
| .adm(Strings.CS.replace(bid.getAdm(), PRICE_MACRO, priceAsString)) | ||
| .nurl(Strings.CS.replace(bid.getNurl(), PRICE_MACRO, priceAsString)) | ||
| .burl(Strings.CS.replace(bid.getBurl(), PRICE_MACRO, priceAsString)) | ||
| .build(); | ||
| } | ||
|
phozhyi marked this conversation as resolved.
Resolved
|
||
|
|
||
| private static BidType getMediaTypeForBid(Bid bid) { | ||
| final Integer mType = bid.getMtype(); | ||
| return switch (mType) { | ||
| case 1 -> BidType.banner; | ||
| case 2 -> BidType.video; | ||
| case 4 -> BidType.xNative; | ||
| case null, default -> throw new PreBidException( | ||
| "Unrecognized bid type for impression %s".formatted(bid.getImpid())); | ||
| }; | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
src/main/java/org/prebid/server/bidder/peak226/model/Peak226Response.java
This file contains hidden or 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.bidder.peak226.model; | ||
|
|
||
| import lombok.Value; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class Peak226Response { | ||
|
|
||
| String cur; | ||
|
|
||
| List<Peak226SeatBid> seatbid; | ||
|
|
||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/prebid/server/bidder/peak226/model/Peak226SeatBid.java
This file contains hidden or 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.peak226.model; | ||
|
|
||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import lombok.Value; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class Peak226SeatBid { | ||
|
|
||
| List<ObjectNode> bid; | ||
|
phozhyi marked this conversation as resolved.
Resolved
|
||
|
|
||
| } | ||
11 changes: 11 additions & 0 deletions
11
src/main/java/org/prebid/server/bidder/peak226/model/RegionPublisher.java
This file contains hidden or 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.bidder.peak226.model; | ||
|
|
||
| import lombok.Value; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class RegionPublisher { | ||
|
|
||
| String region; | ||
|
|
||
| String publisherId; | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/prebid/server/proto/openrtb/ext/request/peak226/ExtImpPeak226.java
This file contains hidden or 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 @@ | ||
| package org.prebid.server.proto.openrtb.ext.request.peak226; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class ExtImpPeak226 { | ||
|
|
||
| @JsonProperty("publisherId") | ||
| String publisherId; | ||
|
|
||
| @JsonProperty("placementId") | ||
| String placementId; | ||
|
|
||
| String region; | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/org/prebid/server/spring/config/bidder/Peak226Configuration.java
This file contains hidden or 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,40 @@ | ||
| package org.prebid.server.spring.config.bidder; | ||
|
|
||
| import org.prebid.server.bidder.BidderDeps; | ||
| import org.prebid.server.bidder.peak226.Peak226Bidder; | ||
| import org.prebid.server.currency.CurrencyConversionService; | ||
| 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.env.YamlPropertySourceFactory; | ||
| 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; | ||
|
|
||
| @Configuration | ||
| @PropertySource(value = "classpath:/bidder-config/peak226.yaml", factory = YamlPropertySourceFactory.class) | ||
| public class Peak226Configuration { | ||
|
|
||
| private static final String BIDDER_NAME = "peak226"; | ||
|
|
||
| @Bean("peak226ConfigurationProperties") | ||
| @ConfigurationProperties("adapters.peak226") | ||
| BidderConfigurationProperties configurationProperties() { | ||
| return new BidderConfigurationProperties(); | ||
| } | ||
|
|
||
| @Bean | ||
| BidderDeps peak226BidderDeps(BidderConfigurationProperties peak226ConfigurationProperties, | ||
| CurrencyConversionService currencyConversionService, | ||
| JacksonMapper mapper) { | ||
|
|
||
| return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
| .withConfig(peak226ConfigurationProperties) | ||
| .bidderCreator(config -> new Peak226Bidder( | ||
| config.getEndpoint(), | ||
| currencyConversionService, | ||
| mapper)) | ||
| .assemble(); | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| adapters: | ||
| peak226: | ||
| endpoint: https://{Region}.a.viddea.com/edge_direct | ||
| ortb-version: "2.6" | ||
| modifying-vast-xml-allowed: true | ||
| geoscope: | ||
| - global | ||
| meta-info: | ||
| maintainer-email: support@edge226.com | ||
| app-media-types: | ||
| - banner | ||
| - video | ||
| - native | ||
| site-media-types: | ||
| - banner | ||
| - video | ||
| - native | ||
| supported-vendors: | ||
| vendor-id: 1202 |
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.
Uh oh!
There was an error while loading. Please reload this page.