-
Notifications
You must be signed in to change notification settings - Fork 221
New Adapter: Nativery #4223
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
przemkaczmarek
wants to merge
11
commits into
master
Choose a base branch
from
New-Adapter-Nativery
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.
+1,088
−0
Open
New Adapter: Nativery #4223
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3b1abb9
New Adapter: Nativery
przemkaczmarek c9ceec1
fix comments
przemkaczmarek f92d15f
fix test
przemkaczmarek a9e734f
fix comments
przemkaczmarek 3a979e6
fix comments
przemkaczmarek 94a24ca
fix comments
przemkaczmarek 1165db7
fix test makeHttpRequestsShouldSetExtWithAmpTrue
przemkaczmarek 869eca1
fix comments
przemkaczmarek b99820d
fix tests
przemkaczmarek b98d69a
fix comments
przemkaczmarek 0fd6721
fix comments
przemkaczmarek 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
246 changes: 246 additions & 0 deletions
246
src/main/java/org/prebid/server/bidder/nativery/NativeryBidder.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,246 @@ | ||
| package org.prebid.server.bidder.nativery; | ||
|
|
||
| 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.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 org.apache.commons.collections4.CollectionUtils; | ||
| import org.apache.commons.collections4.ListUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.prebid.server.auction.model.Endpoint; | ||
| 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.HttpResponse; | ||
| 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.ExtRequest; | ||
| import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid; | ||
| import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidServer; | ||
| import org.prebid.server.proto.openrtb.ext.request.nativery.BidExtNativery; | ||
| import org.prebid.server.proto.openrtb.ext.request.nativery.ExtImpNativery; | ||
| 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.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| public class NativeryBidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final TypeReference<ExtPrebid<?, ExtImpNativery>> NATIVERY_EXT_TYPE_REFERENCE = | ||
| new TypeReference<>() { | ||
| }; | ||
|
|
||
| private static final TypeReference<ExtPrebid<ExtBidPrebid, ?>> EXT_PREBID_TYPE_REFERENCE = | ||
| new TypeReference<>() { | ||
| }; | ||
|
|
||
| private static final String DEFAULT_CURRENCY = "EUR"; | ||
|
|
||
| private final String endpointUrl; | ||
| private final JacksonMapper mapper; | ||
|
|
||
| public NativeryBidder(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>> httpRequests = new ArrayList<>(); | ||
| final List<BidderError> errors = new ArrayList<>(); | ||
|
|
||
| final String requestEndpointName = extractEndpointName(request); | ||
| final boolean isAmp = StringUtils.equals(requestEndpointName, Endpoint.openrtb2_amp.value()); | ||
|
|
||
| final List<Imp> validImps = new ArrayList<>(); | ||
| String widgetId = null; | ||
|
|
||
| for (Imp imp : request.getImp()) { | ||
| try { | ||
| final ExtImpNativery extImp = parseImpExt(imp); | ||
| if (widgetId == null && StringUtils.isNotBlank(extImp.getWidgetId())) { | ||
| widgetId = extImp.getWidgetId(); | ||
| } | ||
| validImps.add(imp); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| if (validImps.isEmpty()) { | ||
| return Result.withErrors(errors); | ||
| } | ||
|
|
||
| final ExtRequest updatedExt = buildRequestExtWithNativery(request.getExt(), isAmp, widgetId); | ||
|
|
||
| for (Imp imp : validImps) { | ||
| final BidRequest singleImpRequest = request.toBuilder() | ||
| .imp(Collections.singletonList(imp)) | ||
| .ext(updatedExt) | ||
| .build(); | ||
|
|
||
| httpRequests.add(BidderUtil.defaultRequest(singleImpRequest, endpointUrl, mapper)); | ||
| } | ||
|
|
||
| return Result.of(httpRequests, errors); | ||
| } | ||
|
|
||
| private static String extractEndpointName(BidRequest bidRequest) { | ||
| return Optional.ofNullable(bidRequest) | ||
| .map(BidRequest::getExt) | ||
| .map(ExtRequest::getPrebid) | ||
| .map(ExtRequestPrebid::getServer) | ||
| .map(ExtRequestPrebidServer::getEndpoint) | ||
| .orElse(null); | ||
| } | ||
|
|
||
| private ExtImpNativery parseImpExt(Imp imp) { | ||
| try { | ||
| return mapper.mapper().convertValue(imp.getExt(), NATIVERY_EXT_TYPE_REFERENCE).getBidder(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException("Failed to deserialize Nativery extension: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private ExtRequest buildRequestExtWithNativery(ExtRequest originalExt, boolean isAmp, String widgetId) { | ||
| final ExtRequest ext = originalExt != null ? originalExt : ExtRequest.empty(); | ||
|
|
||
| final JsonNode existing = ext.getProperty("nativery"); | ||
| final ObjectNode nativeryNode = existing != null && existing.isObject() | ||
| ? (ObjectNode) existing | ||
| : mapper.mapper().createObjectNode(); | ||
|
|
||
| nativeryNode.put("isAmp", isAmp); | ||
| if (StringUtils.isNotBlank(widgetId)) { | ||
| nativeryNode.put("widgetId", widgetId); | ||
| } | ||
|
|
||
| ext.addProperty("nativery", nativeryNode); | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return ext; | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
| final List<BidderError> errors = new ArrayList<>(); | ||
|
|
||
| final HttpResponse response = httpCall.getResponse(); | ||
|
|
||
| try { | ||
| final BidResponse bidResponse = mapper.decodeValue(response.getBody(), BidResponse.class); | ||
| final List<BidderBid> bidderBids = extractBids(bidResponse, errors); | ||
| return Result.of(bidderBids, 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(); | ||
| } | ||
|
|
||
| final String currency = StringUtils.defaultIfBlank(bidResponse.getCur(), DEFAULT_CURRENCY); | ||
|
|
||
| return bidResponse.getSeatbid().stream() | ||
| .filter(Objects::nonNull) | ||
| .map(SeatBid::getBid) | ||
| .filter(Objects::nonNull) | ||
| .flatMap(Collection::stream) | ||
| .filter(Objects::nonNull) | ||
| .map(bid -> resolveBidderBid(bid, currency, errors)) | ||
| .filter(Objects::nonNull) | ||
| .toList(); | ||
| } | ||
|
|
||
| private BidderBid resolveBidderBid(Bid bid, String currency, List<BidderError> errors) { | ||
| try { | ||
| final BidExtNativery nativeryExt = parseNativeryExt(bid.getExt()); | ||
|
|
||
| final BidType bidType = mapMediaType(nativeryExt.getBidAdMediaType()); | ||
| final List<String> advDomains = ListUtils.emptyIfNull( | ||
| nativeryExt.getBidAdvDomains()); | ||
|
|
||
| final Bid updatedBid = addBidMeta(bid, bidType.getName(), advDomains); | ||
| return BidderBid.of(updatedBid, bidType, currency); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private BidExtNativery parseNativeryExt(ObjectNode bidExt) { | ||
| return Optional.ofNullable(bidExt) | ||
| .map(ext -> ext.get("nativery")) | ||
| .filter(JsonNode::isObject) | ||
| .map(this::toBidExtNativery) | ||
| .orElseThrow(() -> new PreBidException("missing bid.ext.nativery")); | ||
| } | ||
|
|
||
| private BidExtNativery toBidExtNativery(JsonNode node) { | ||
| try { | ||
| return mapper.mapper().convertValue(node, BidExtNativery.class); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException("invalid bid.ext.nativery: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private static BidType mapMediaType(String mediaType) { | ||
| final String mt = StringUtils.defaultString(mediaType).toLowerCase(); | ||
| return switch (mt) { | ||
| case "native" -> BidType.xNative; | ||
| case "display", "banner", "rich_media" -> BidType.banner; | ||
| case "video" -> BidType.video; | ||
| default -> throw new PreBidException( | ||
| "unrecognized bid_ad_media_type in response from nativery: " + mediaType); | ||
| }; | ||
| } | ||
|
|
||
| private Bid addBidMeta(Bid bid, String mediaType, List<String> advDomains) { | ||
| final ExtBidPrebid prebid = parseExtBidPrebid(bid); | ||
|
|
||
| final ExtBidPrebidMeta modifiedMeta = Optional.ofNullable(prebid) | ||
| .map(ExtBidPrebid::getMeta) | ||
| .map(ExtBidPrebidMeta::toBuilder) | ||
| .orElseGet(ExtBidPrebidMeta::builder) | ||
| .mediaType(mediaType) | ||
| .advertiserDomains(advDomains) | ||
| .build(); | ||
|
|
||
| final ExtBidPrebid modifiedPrebid = Optional.ofNullable(prebid) | ||
| .map(ExtBidPrebid::toBuilder) | ||
| .orElseGet(ExtBidPrebid::builder) | ||
| .meta(modifiedMeta) | ||
| .build(); | ||
|
|
||
| return bid.toBuilder() | ||
| .ext(mapper.mapper().valueToTree(ExtPrebid.of(modifiedPrebid, null))) | ||
| .build(); | ||
| } | ||
|
|
||
| private ExtBidPrebid parseExtBidPrebid(Bid bid) { | ||
| try { | ||
| return mapper.mapper() | ||
| .convertValue(bid.getExt(), EXT_PREBID_TYPE_REFERENCE) | ||
| .getPrebid(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException("Failed to deserialize Prebid extension: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/main/java/org/prebid/server/proto/openrtb/ext/request/nativery/BidExtNativery.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.proto.openrtb.ext.request.nativery; | ||
|
|
||
| import lombok.Value; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class BidExtNativery { | ||
|
|
||
| String bidAdMediaType; | ||
|
|
||
| List<String> bidAdvDomains; | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/prebid/server/proto/openrtb/ext/request/nativery/ExtImpNativery.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.proto.openrtb.ext.request.nativery; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class ExtImpNativery { | ||
|
|
||
| @JsonProperty("widgetId") | ||
| String widgetId; | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/prebid/server/spring/config/bidder/NativeryBidderConfiguration.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,41 @@ | ||
| package org.prebid.server.spring.config.bidder; | ||
|
|
||
| import org.prebid.server.bidder.BidderDeps; | ||
| import org.prebid.server.bidder.nativery.NativeryBidder; | ||
| 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/nativery.yaml", factory = YamlPropertySourceFactory.class) | ||
| public class NativeryBidderConfiguration { | ||
|
|
||
| private static final String BIDDER_NAME = "nativery"; | ||
|
|
||
| @Bean("nativeryConfigurationProperties") | ||
| @ConfigurationProperties("adapters.nativery") | ||
| BidderConfigurationProperties configurationProperties() { | ||
| return new BidderConfigurationProperties(); | ||
| } | ||
|
|
||
| @Bean | ||
| BidderDeps nativeryBidderDeps(BidderConfigurationProperties nativeryConfigurationProperties, | ||
| @NotBlank @Value("${external-url}") String externalUrl, | ||
| JacksonMapper mapper) { | ||
|
|
||
| return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
| .withConfig(nativeryConfigurationProperties) | ||
| .usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
| .bidderCreator(config -> new NativeryBidder(config.getEndpoint(), 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,12 @@ | ||
| adapters: | ||
| nativery: | ||
| endpoint: "https://hb.nativery.com/openrtb2/auction" | ||
| ortb-version: "2.6" | ||
| meta-info: | ||
| maintainer-email: "developer@nativery.com" | ||
| site-media-types: | ||
| - banner | ||
| - video | ||
| - native | ||
| supported-vendors: | ||
| vendor-id: 1133 |
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,15 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Nativery Adapter Params", | ||
| "description": "A schema which validates params accepted by the Nativery adapter", | ||
|
|
||
| "type": "object", | ||
| "properties": { | ||
| "widgetId": { | ||
| "type": "string", | ||
| "description": "An ID which identifies this Nativery widget" | ||
| } | ||
| }, | ||
|
|
||
| "required": ["widgetId"] | ||
| } |
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.