-
Notifications
You must be signed in to change notification settings - Fork 239
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.
+911
−0
Open
New adapter: HypeLab #4488
Changes from 1 commit
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
241 changes: 241 additions & 0 deletions
241
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,241 @@ | ||
| package org.prebid.server.bidder.hypelab; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| 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 io.vertx.core.MultiMap; | ||
| import io.vertx.core.http.HttpMethod; | ||
| 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.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| 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 UNKNOWN_VERSION = "unknown"; | ||
|
|
||
| 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 { | ||
| validImps.add(makeOutgoingImp(imp)); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| if (validImps.isEmpty()) { | ||
| return Result.of(Collections.emptyList(), errors); | ||
| } | ||
|
|
||
| final BidRequest outgoingRequest = request.toBuilder() | ||
| .imp(validImps) | ||
| .ext(makeOutgoingRequestExt(request.getExt())) | ||
| .build(); | ||
|
|
||
| return Result.of(Collections.singletonList( | ||
| HttpRequest.<BidRequest>builder() | ||
| .method(HttpMethod.POST) | ||
| .uri(endpointUrl) | ||
| .headers(headers()) | ||
| .impIds(BidderUtil.impIds(outgoingRequest)) | ||
| .body(mapper.encodeToBytes(outgoingRequest)) | ||
| .payload(outgoingRequest) | ||
| .build()), | ||
| errors); | ||
|
CTMBNara marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private Imp makeOutgoingImp(Imp imp) { | ||
| final ExtImpHypeLab extImp = parseImpExt(imp); | ||
|
CTMBNara marked this conversation as resolved.
Outdated
|
||
| 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) { | ||
| if (imp.getExt() == null) { | ||
| throw new PreBidException("imp %s: unable to unmarshal ext".formatted(imp.getId())); | ||
| } | ||
|
Collaborator
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.
|
||
|
|
||
| final JsonNode bidderNode = imp.getExt().get("bidder"); | ||
| if (bidderNode == null || bidderNode.isNull()) { | ||
| throw new PreBidException("imp %s: unable to unmarshal ext.bidder".formatted(imp.getId())); | ||
| } | ||
|
CTMBNara marked this conversation as resolved.
Outdated
|
||
|
|
||
| final ExtImpHypeLab extImp; | ||
| try { | ||
| extImp = mapper.mapper().convertValue(bidderNode, ExtImpHypeLab.class); | ||
|
CTMBNara marked this conversation as resolved.
Outdated
|
||
| } 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) { | ||
| outgoingExt.addProperties(ext.getProperties()); | ||
|
CTMBNara marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| outgoingExt.addProperty("source", mapper.mapper().valueToTree(SOURCE)); | ||
| outgoingExt.addProperty("provider_version", mapper.mapper().valueToTree(pbsVersion())); | ||
|
CTMBNara marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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, imp -> imp)); | ||
|
Collaborator
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.
|
||
|
|
||
| return response.getSeatbid().stream() | ||
| .filter(Objects::nonNull) | ||
| .map(seatBid -> bidsFromSeatBid(seatBid, impIdToImp, response.getCur(), errors)) | ||
| .flatMap(Collection::stream) | ||
| .toList(); | ||
| } | ||
|
|
||
| private static List<BidderBid> bidsFromSeatBid(SeatBid seatBid, Map<String, Imp> impIdToImp, String currency, | ||
| List<BidderError> errors) { | ||
|
|
||
| final List<Bid> bids = seatBid.getBid(); | ||
| if (CollectionUtils.isEmpty(bids)) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| return bids.stream() | ||
| .filter(Objects::nonNull) | ||
| .map(bid -> makeBidderBid(bid, seatBid.getSeat(), impIdToImp, currency, errors)) | ||
| .filter(Objects::nonNull) | ||
| .toList(); | ||
| } | ||
|
|
||
|
CTMBNara marked this conversation as resolved.
|
||
| 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) { | ||
| final BidType bidTypeFromMtype = bidTypeFromMtype(bid.getMtype()); | ||
| if (bidTypeFromMtype != null) { | ||
| return bidTypeFromMtype; | ||
| } | ||
|
|
||
| final BidType bidTypeFromExt = bidTypeFromExt(bid); | ||
| if (bidTypeFromExt != null) { | ||
| return bidTypeFromExt; | ||
| } | ||
|
|
||
| if (StringUtils.startsWith(StringUtils.trimToEmpty(bid.getAdm()), "<VAST")) { | ||
| return BidType.video; | ||
| } | ||
|
|
||
| if (impIdToImp.containsKey(bid.getImpid())) { | ||
| return BidderUtil.getBidType(bid, impIdToImp); | ||
| } | ||
|
CTMBNara marked this conversation as resolved.
Outdated
|
||
|
|
||
| throw new PreBidException("unable to determine media type for bid %s on imp %s" | ||
| .formatted(bid.getId(), bid.getImpid())); | ||
| } | ||
|
|
||
| private static BidType bidTypeFromMtype(Integer mtype) { | ||
| return switch (Objects.requireNonNullElse(mtype, 0)) { | ||
| case 1 -> BidType.banner; | ||
| case 2 -> BidType.video; | ||
| case 4 -> BidType.xNative; | ||
| default -> null; | ||
| }; | ||
| } | ||
|
|
||
| private static BidType bidTypeFromExt(Bid bid) { | ||
| final JsonNode hypelabExt = bid.getExt() != null ? bid.getExt().get("hypelab") : null; | ||
| final String creativeType = hypelabExt != null ? hypelabExt.path("creative_type").asText() : null; | ||
|
|
||
| return switch (StringUtils.defaultString(creativeType)) { | ||
| case "display" -> BidType.banner; | ||
| case "video" -> BidType.video; | ||
| default -> null; | ||
|
CTMBNara marked this conversation as resolved.
Outdated
|
||
| }; | ||
| } | ||
|
|
||
| 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,21 @@ | ||
| adapters: | ||
| hypelab: | ||
| endpoint: https://api.hypelab.com/v1/rtb_requests | ||
| ortb-version: "2.6" | ||
| aliases: | ||
| hype: ~ | ||
| meta-info: | ||
| maintainer-email: sdk@hypelab.com | ||
| app-media-types: [] | ||
|
CTMBNara marked this conversation as resolved.
Outdated
|
||
| 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.