-
Notifications
You must be signed in to change notification settings - Fork 238
New adapter: HypeLab #4488
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
Minebomber
wants to merge
3
commits into
prebid:master
Choose a base branch
from
gohypelab:feat/hypelab-adapter
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
New adapter: HypeLab #4488
Changes from all commits
Commits
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
240 changes: 240 additions & 0 deletions
240
src/main/java/org/prebid/server/bidder/hypelab/HypeLabBidder.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,240 @@ | ||
| package org.prebid.server.bidder.hypelab; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.node.TextNode; | ||
| 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 io.vertx.core.MultiMap; | ||
| 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.ExtRequest; | ||
| import org.prebid.server.proto.openrtb.ext.request.hypelab.ExtImpHypeLab; | ||
| import org.prebid.server.proto.openrtb.ext.response.BidType; | ||
| import org.prebid.server.util.BidderUtil; | ||
| import org.prebid.server.util.HttpUtil; | ||
| import org.prebid.server.version.PrebidVersionProvider; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class HypeLabBidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final String DISPLAY_MANAGER = "HypeLab Prebid Server"; | ||
| private static final String SOURCE = "prebid-server"; | ||
| private static final String PROVIDER_VERSION_PREFIX = "prebid-server@"; | ||
| private static final String UNKNOWN_VERSION = "unknown"; | ||
| private static final TypeReference<ExtPrebid<?, ExtImpHypeLab>> HYPELAB_EXT_TYPE_REFERENCE = | ||
| new TypeReference<>() { | ||
| }; | ||
|
|
||
| private final String endpointUrl; | ||
| private final JacksonMapper mapper; | ||
| private final PrebidVersionProvider prebidVersionProvider; | ||
|
|
||
| public HypeLabBidder(String endpointUrl, JacksonMapper mapper, PrebidVersionProvider prebidVersionProvider) { | ||
| this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); | ||
| this.mapper = Objects.requireNonNull(mapper); | ||
| this.prebidVersionProvider = Objects.requireNonNull(prebidVersionProvider); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) { | ||
| final List<BidderError> errors = new ArrayList<>(); | ||
| final List<Imp> validImps = new ArrayList<>(); | ||
|
|
||
| for (Imp imp : request.getImp()) { | ||
| try { | ||
| final ExtImpHypeLab extImp = parseImpExt(imp); | ||
| validImps.add(makeOutgoingImp(imp, extImp)); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| if (validImps.isEmpty()) { | ||
| return Result.withErrors(errors); | ||
| } | ||
|
|
||
| final BidRequest outgoingRequest = request.toBuilder() | ||
| .imp(validImps) | ||
| .ext(makeOutgoingRequestExt(request.getExt())) | ||
| .build(); | ||
|
|
||
| return Result.of( | ||
| Collections.singletonList(BidderUtil.defaultRequest( | ||
| outgoingRequest, | ||
| headers(), | ||
| endpointUrl, | ||
| mapper)), | ||
| errors); | ||
| } | ||
|
|
||
| private Imp makeOutgoingImp(Imp imp, ExtImpHypeLab extImp) { | ||
| final String pbsVersion = pbsVersion(); | ||
|
|
||
| return imp.toBuilder() | ||
| .tagid(extImp.getPlacementSlug()) | ||
| .displaymanager(DISPLAY_MANAGER) | ||
| .displaymanagerver(pbsVersion) | ||
| .ext(mapper.mapper().valueToTree(ExtPrebid.of(null, extImp))) | ||
| .build(); | ||
| } | ||
|
|
||
| private ExtImpHypeLab parseImpExt(Imp imp) { | ||
| final ExtImpHypeLab extImp; | ||
| try { | ||
| extImp = mapper.mapper().convertValue(imp.getExt(), HYPELAB_EXT_TYPE_REFERENCE).getBidder(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException("imp %s: unable to unmarshal ext.bidder".formatted(imp.getId()), e); | ||
| } | ||
|
|
||
| if (StringUtils.isBlank(extImp.getPropertySlug()) || StringUtils.isBlank(extImp.getPlacementSlug())) { | ||
| throw new PreBidException("imp %s: property_slug and placement_slug are required".formatted(imp.getId())); | ||
| } | ||
|
|
||
| return extImp; | ||
| } | ||
|
|
||
| private ExtRequest makeOutgoingRequestExt(ExtRequest ext) { | ||
| final ExtRequest outgoingExt = ext != null ? ExtRequest.of(ext.getPrebid()) : ExtRequest.empty(); | ||
| if (ext != null) { | ||
| mapper.fillExtension(outgoingExt, ext.getProperties()); | ||
| } | ||
|
|
||
| outgoingExt.addProperty("source", TextNode.valueOf(SOURCE)); | ||
| outgoingExt.addProperty("provider_version", TextNode.valueOf(PROVIDER_VERSION_PREFIX + pbsVersion())); | ||
|
|
||
| return outgoingExt; | ||
| } | ||
|
|
||
| private static MultiMap headers() { | ||
| return HttpUtil.headers() | ||
| .add(HttpUtil.X_OPENRTB_VERSION_HEADER, "2.6"); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
| final BidResponse bidResponse; | ||
| try { | ||
| bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
| } catch (DecodeException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
|
|
||
| final List<BidderError> errors = new ArrayList<>(); | ||
| return Result.of(extractBids(httpCall.getRequest().getPayload(), bidResponse, errors), errors); | ||
| } | ||
|
|
||
| private static List<BidderBid> extractBids(BidRequest request, BidResponse response, List<BidderError> errors) { | ||
| if (response == null || CollectionUtils.isEmpty(response.getSeatbid())) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| final Map<String, Imp> impIdToImp = request.getImp().stream() | ||
| .collect(Collectors.toMap(Imp::getId, Function.identity())); | ||
|
|
||
| return response.getSeatbid().stream() | ||
| .filter(Objects::nonNull) | ||
| .flatMap(seatBid -> CollectionUtils.emptyIfNull(seatBid.getBid()).stream() | ||
| .filter(Objects::nonNull) | ||
| .map(bid -> makeBidderBid(bid, seatBid.getSeat(), impIdToImp, response.getCur(), errors)) | ||
| .filter(Objects::nonNull)) | ||
| .toList(); | ||
| } | ||
|
|
||
| private static BidderBid makeBidderBid(Bid bid, String seat, Map<String, Imp> impIdToImp, String currency, | ||
| List<BidderError> errors) { | ||
|
|
||
| try { | ||
| return BidderBid.of(bid, resolveBidType(bid, impIdToImp), seat, currency); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badServerResponse(e.getMessage())); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static BidType resolveBidType(Bid bid, Map<String, Imp> impIdToImp) { | ||
| return bidTypeFromMtype(bid.getMtype()) | ||
| .or(() -> bidTypeFromExt(bid)) | ||
| .or(() -> bidTypeFromAdm(bid.getAdm())) | ||
| .or(() -> bidTypeFromImp(bid, impIdToImp)) | ||
| .orElseThrow(() -> new PreBidException("unable to determine media type for bid %s on imp %s" | ||
| .formatted(bid.getId(), bid.getImpid()))); | ||
| } | ||
|
|
||
| private static Optional<BidType> bidTypeFromMtype(Integer mtype) { | ||
| return Optional.ofNullable(switch (mtype) { | ||
| case 1 -> BidType.banner; | ||
| case 2 -> BidType.video; | ||
| case 4 -> BidType.xNative; | ||
| case null, default -> null; | ||
| }); | ||
| } | ||
|
|
||
| private static Optional<BidType> bidTypeFromExt(Bid bid) { | ||
| return Optional.ofNullable(bid.getExt()) | ||
| .map(ext -> ext.get("hypelab")) | ||
| .map(hypelab -> hypelab.get("creative_type")) | ||
| .filter(JsonNode::isTextual) | ||
| .map(JsonNode::asText) | ||
| .map(creativeType -> switch (creativeType) { | ||
| case "display" -> BidType.banner; | ||
| case "video" -> BidType.video; | ||
| case "native" -> BidType.xNative; | ||
| default -> null; | ||
| }); | ||
| } | ||
|
|
||
| private static Optional<BidType> bidTypeFromAdm(String adm) { | ||
| return StringUtils.startsWith(StringUtils.trimToEmpty(adm), "<VAST") | ||
| ? Optional.of(BidType.video) | ||
| : Optional.empty(); | ||
| } | ||
|
|
||
| private static Optional<BidType> bidTypeFromImp(Bid bid, Map<String, Imp> impIdToImp) { | ||
| final Imp imp = impIdToImp.get(bid.getImpid()); | ||
| if (imp == null) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| BidType bidType = null; | ||
| int mediaTypeCount = 0; | ||
| if (imp.getBanner() != null) { | ||
| bidType = BidType.banner; | ||
| mediaTypeCount++; | ||
| } | ||
| if (imp.getVideo() != null) { | ||
| bidType = BidType.video; | ||
| mediaTypeCount++; | ||
| } | ||
| if (imp.getXNative() != null) { | ||
| bidType = BidType.xNative; | ||
| mediaTypeCount++; | ||
| } | ||
|
|
||
| return mediaTypeCount == 1 ? Optional.ofNullable(bidType) : Optional.empty(); | ||
| } | ||
|
|
||
| private String pbsVersion() { | ||
| return StringUtils.defaultIfBlank(prebidVersionProvider.getNameVersionRecord(), UNKNOWN_VERSION); | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
src/main/java/org/prebid/server/proto/openrtb/ext/request/hypelab/ExtImpHypeLab.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.proto.openrtb.ext.request.hypelab; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class ExtImpHypeLab { | ||
|
|
||
| @JsonProperty("property_slug") | ||
| String propertySlug; | ||
|
|
||
| @JsonProperty("placement_slug") | ||
| String placementSlug; | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/prebid/server/spring/config/bidder/HypeLabConfiguration.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,46 @@ | ||
| package org.prebid.server.spring.config.bidder; | ||
|
|
||
| import org.prebid.server.bidder.BidderDeps; | ||
| import org.prebid.server.bidder.hypelab.HypeLabBidder; | ||
| 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.prebid.server.version.PrebidVersionProvider; | ||
| 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/hypelab.yaml", factory = YamlPropertySourceFactory.class) | ||
| public class HypeLabConfiguration { | ||
|
|
||
| private static final String BIDDER_NAME = "hypelab"; | ||
|
|
||
| @Bean("hypelabConfigurationProperties") | ||
| @ConfigurationProperties("adapters.hypelab") | ||
| BidderConfigurationProperties configurationProperties() { | ||
| return new BidderConfigurationProperties(); | ||
| } | ||
|
|
||
| @Bean | ||
| BidderDeps hypelabBidderDeps(BidderConfigurationProperties hypelabConfigurationProperties, | ||
| @NotBlank @Value("${external-url}") String externalUrl, | ||
| PrebidVersionProvider prebidVersionProvider, | ||
| JacksonMapper mapper) { | ||
|
|
||
| return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
| .withConfig(hypelabConfigurationProperties) | ||
| .usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
| .bidderCreator(config -> new HypeLabBidder( | ||
| config.getEndpoint(), | ||
| mapper, | ||
| prebidVersionProvider)) | ||
| .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,18 @@ | ||
| adapters: | ||
| hypelab: | ||
| endpoint: https://api.hypelab.com/v1/rtb_requests | ||
| ortb-version: "2.6" | ||
| meta-info: | ||
| maintainer-email: sdk@hypelab.com | ||
| site-media-types: | ||
| - banner | ||
| - native | ||
| - video | ||
| supported-vendors: | ||
| vendor-id: 0 | ||
| usersync: | ||
| cookie-family-name: hypelab | ||
| redirect: | ||
| url: https://api.hypelab.com/v1/i?redirect_url={{redirect_url}}&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&gpp={{gpp}}&gpp_sid={{gpp_sid}} | ||
| support-cors: false | ||
| uid-macro: '$UID' |
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 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "HypeLab Adapter Params", | ||
| "description": "A schema which validates params accepted by the HypeLab adapter", | ||
| "type": "object", | ||
| "properties": { | ||
| "property_slug": { | ||
| "type": "string", | ||
| "minLength": 1, | ||
| "description": "HypeLab property slug" | ||
| }, | ||
| "placement_slug": { | ||
| "type": "string", | ||
| "minLength": 1, | ||
| "description": "HypeLab placement slug" | ||
| } | ||
| }, | ||
| "required": ["property_slug", "placement_slug"] | ||
| } |
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.