-
Notifications
You must be signed in to change notification settings - Fork 224
New Adapter: Goldbach #4300
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
Lightwood13
wants to merge
6
commits into
master
Choose a base branch
from
add-goldbach-bidder
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: Goldbach #4300
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
251 changes: 251 additions & 0 deletions
251
src/main/java/org/prebid/server/bidder/goldbach/GoldbachBidder.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,251 @@ | ||
| package org.prebid.server.bidder.goldbach; | ||
|
|
||
| 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.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.netty.handler.codec.http.HttpResponseStatus; | ||
| import org.prebid.server.bidder.Bidder; | ||
| import org.prebid.server.bidder.goldbach.proto.GoldbachExtImp; | ||
| import org.prebid.server.bidder.goldbach.proto.ExtRequestGoldbach; | ||
| 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.goldbach.ExtImpGoldbach; | ||
| import org.prebid.server.proto.openrtb.ext.response.BidType; | ||
| import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid; | ||
| import org.prebid.server.util.BidderUtil; | ||
| import org.prebid.server.util.HttpUtil; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class GoldbachBidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final TypeReference<ExtPrebid<?, ExtImpGoldbach>> GOLDBACH_EXT_TYPE_REFERENCE = | ||
| new TypeReference<>() { | ||
| }; | ||
|
|
||
| private static final TypeReference<ExtPrebid<ExtBidPrebid, ObjectNode>> EXT_PREBID_TYPE_REFERENCE = | ||
| new TypeReference<>() { | ||
| }; | ||
|
|
||
| private final String endpointUrl; | ||
| private final JacksonMapper mapper; | ||
|
|
||
| public GoldbachBidder(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<BidderError> errors = new ArrayList<>(); | ||
|
|
||
| final ExtRequestGoldbach extRequestGoldbach = parseRequestExt(request, errors); | ||
| final Map<String, List<Imp>> publisherToImps = new HashMap<>(); | ||
| for (Imp imp : request.getImp()) { | ||
| try { | ||
| final ExtImpGoldbach extImp = parseImpExt(imp); | ||
| final Imp updatedImp = modifyImp(imp, extImp); | ||
|
|
||
| publisherToImps.computeIfAbsent(extImp.getPublisherId(), k -> new ArrayList<>()) | ||
| .add(updatedImp); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| if (publisherToImps.isEmpty()) { | ||
| errors.add(BidderError.badInput("No valid impressions found")); | ||
| } | ||
|
|
||
| final List<HttpRequest<BidRequest>> httpRequests = publisherToImps.entrySet().stream() | ||
| .map(publisherIdAndImps -> | ||
| makeHttpRequestForPublisher(request, | ||
| extRequestGoldbach, | ||
| publisherIdAndImps.getKey(), | ||
| publisherIdAndImps.getValue(), | ||
| errors)) | ||
| .filter(Objects::nonNull) | ||
| .toList(); | ||
|
|
||
| return Result.of(httpRequests, errors); | ||
| } | ||
|
|
||
| private ExtRequestGoldbach parseRequestExt(BidRequest bidRequest, List<BidderError> errors) { | ||
| return Optional.ofNullable(bidRequest.getExt()) | ||
| .map(extRequest -> extRequest.getProperty("goldbach")) | ||
| .map(extRequestGoldbachRaw -> parseRequestExtGoldbach(extRequestGoldbachRaw, errors)) | ||
| .orElse(null); | ||
| } | ||
|
|
||
| private ExtRequestGoldbach parseRequestExtGoldbach(JsonNode extRequestGoldbachRaw, List<BidderError> errors) { | ||
| try { | ||
| return mapper.mapper().treeToValue(extRequestGoldbachRaw, ExtRequestGoldbach.class); | ||
| } catch (IllegalArgumentException | JsonProcessingException e) { | ||
| errors.add(BidderError.badInput("Failed to deserialize Goldbach bid request extension: " + e.getMessage())); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private ExtImpGoldbach parseImpExt(Imp imp) { | ||
| try { | ||
| return mapper.mapper().convertValue(imp.getExt(), GOLDBACH_EXT_TYPE_REFERENCE).getBidder(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException("Failed to deserialize Goldbach imp extension: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private Imp modifyImp(Imp imp, ExtImpGoldbach extImp) { | ||
| return imp.toBuilder() | ||
| .ext(mapper.mapper().createObjectNode().set("goldbach", mapper.mapper().valueToTree( | ||
| GoldbachExtImp.of(extImp.getSlotId(), extImp.getCustomTargeting())))) | ||
| .build(); | ||
| } | ||
|
|
||
| private HttpRequest<BidRequest> makeHttpRequestForPublisher(BidRequest bidRequest, | ||
| ExtRequestGoldbach extRequestGoldbach, | ||
| String publisherId, | ||
| List<Imp> imps, | ||
| List<BidderError> errors) { | ||
|
|
||
| try { | ||
| final BidRequest modifiedBidRequest = modifyBidRequest(bidRequest, extRequestGoldbach, publisherId, imps); | ||
| return BidderUtil.defaultRequest(modifiedBidRequest, endpointUrl, mapper); | ||
| } catch (PreBidException e) { | ||
Lightwood13 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private BidRequest modifyBidRequest(BidRequest bidRequest, | ||
| ExtRequestGoldbach extRequestGoldbach, | ||
| String publisherId, | ||
| List<Imp> imps) { | ||
|
|
||
| final ExtRequest modifiedExtRequest = modifyExtRequest(bidRequest.getExt(), extRequestGoldbach, publisherId); | ||
| return bidRequest.toBuilder() | ||
| .id("%s_%s".formatted(bidRequest.getId(), publisherId)) | ||
| .imp(imps) | ||
| .ext(modifiedExtRequest) | ||
| .build(); | ||
| } | ||
|
|
||
| private ExtRequest modifyExtRequest(ExtRequest extRequest, | ||
| ExtRequestGoldbach extRequestGoldbach, | ||
| String publisherId) { | ||
|
|
||
| final ExtRequestGoldbach modifiedExtRequestGoldbach = ExtRequestGoldbach.builder() | ||
| .publisherId(publisherId) | ||
| .mockResponse(extRequestGoldbach != null ? extRequestGoldbach.getMockResponse() : null) | ||
| .build(); | ||
|
|
||
| final ExtRequest modifiedExtRequest; | ||
|
|
||
| if (extRequest != null) { | ||
| modifiedExtRequest = ExtRequest.of(extRequest.getPrebid()); | ||
| mapper.fillExtension(modifiedExtRequest, extRequest); | ||
| } else { | ||
| modifiedExtRequest = ExtRequest.empty(); | ||
| } | ||
|
|
||
| modifiedExtRequest.addProperty("goldbach", mapper.mapper().valueToTree(modifiedExtRequestGoldbach)); | ||
| return modifiedExtRequest; | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
| if (httpCall.getResponse().getStatusCode() != HttpResponseStatus.CREATED.code()) { | ||
| return Result.withError( | ||
| BidderError.badServerResponse( | ||
| "unexpected status code: %d. Run with request.debug = 1 for more info".formatted( | ||
| httpCall.getResponse().getStatusCode()))); | ||
| } | ||
|
|
||
| final BidResponse bidResponse; | ||
| try { | ||
| bidResponse = decodeBodyToBidResponse(httpCall); | ||
| } catch (PreBidException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
|
|
||
| return bidsFromResponse(bidResponse); | ||
| } | ||
|
|
||
| private BidResponse decodeBodyToBidResponse(BidderCall<BidRequest> httpCall) { | ||
| try { | ||
| return mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
| } catch (DecodeException e) { | ||
| throw new PreBidException("Failed to parse response as BidResponse: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private Result<List<BidderBid>> bidsFromResponse(BidResponse bidResponse) { | ||
| final List<BidderError> errors = new ArrayList<>(); | ||
| final List<BidderBid> bidderBids = Stream.ofNullable(bidResponse) | ||
| .map(BidResponse::getSeatbid) | ||
| .filter(Objects::nonNull) | ||
| .flatMap(Collection::stream) | ||
| .filter(Objects::nonNull) | ||
| .map(SeatBid::getBid) | ||
| .filter(Objects::nonNull) | ||
| .flatMap(Collection::stream) | ||
| .filter(Objects::nonNull) | ||
| .map(bid -> makeBid(bid, bidResponse, errors)) | ||
| .filter(Objects::nonNull) | ||
| .toList(); | ||
|
|
||
| if (bidderBids.isEmpty()) { | ||
| errors.add(BidderError.badServerResponse("No valid bids found in response")); | ||
| } | ||
|
|
||
| return Result.of(bidderBids, errors); | ||
| } | ||
|
|
||
| private BidderBid makeBid(Bid bid, BidResponse bidResponse, List<BidderError> errors) { | ||
| try { | ||
| return BidderBid.of( | ||
| bid, | ||
| getBidType(bid), | ||
| bidResponse.getCur()); | ||
Lightwood13 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private BidType getBidType(Bid bid) { | ||
| return Optional.ofNullable(bid.getExt()) | ||
| .map(this::parseBidExt) | ||
| .map(ExtPrebid::getPrebid) | ||
| .map(ExtBidPrebid::getType) | ||
| .orElseThrow(() -> new PreBidException("No media type for bid " + bid.getId())); | ||
| } | ||
|
|
||
| private ExtPrebid<ExtBidPrebid, ObjectNode> parseBidExt(ObjectNode bidExt) { | ||
| try { | ||
| return mapper.mapper().convertValue(bidExt, EXT_PREBID_TYPE_REFERENCE); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException("Failed to deserialize ext for bid: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/main/java/org/prebid/server/bidder/goldbach/proto/ExtRequestGoldbach.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.bidder.goldbach.proto; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Builder; | ||
| import lombok.Value; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| @Builder(toBuilder = true) | ||
Lightwood13 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public class ExtRequestGoldbach { | ||
|
|
||
| @JsonProperty("publisherId") | ||
| String publisherId; | ||
|
|
||
| @JsonProperty("mockResponse") | ||
| Boolean mockResponse; | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/main/java/org/prebid/server/bidder/goldbach/proto/GoldbachExtImp.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.bidder.goldbach.proto; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class GoldbachExtImp { | ||
|
|
||
| @JsonProperty("slotId") | ||
| String slotId; | ||
|
|
||
| Map<String, List<String>> targetings; | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/prebid/server/proto/openrtb/ext/request/goldbach/ExtImpGoldbach.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,20 @@ | ||
| package org.prebid.server.proto.openrtb.ext.request.goldbach; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class ExtImpGoldbach { | ||
|
|
||
| @JsonProperty("publisherId") | ||
| String publisherId; | ||
|
|
||
| @JsonProperty("slotId") | ||
| String slotId; | ||
|
|
||
| @JsonProperty("customTargeting") | ||
| Map<String, List<String>> customTargeting; | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/prebid/server/spring/config/bidder/GoldbachConfiguration.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.goldbach.GoldbachBidder; | ||
| 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/goldbach.yaml", factory = YamlPropertySourceFactory.class) | ||
| public class GoldbachConfiguration { | ||
|
|
||
| private static final String BIDDER_NAME = "goldbach"; | ||
|
|
||
| @Bean("goldbachConfigurationProperties") | ||
| @ConfigurationProperties("adapters.goldbach") | ||
| BidderConfigurationProperties configurationProperties() { | ||
| return new BidderConfigurationProperties(); | ||
| } | ||
|
|
||
| @Bean | ||
| BidderDeps goldbachBidderDeps(BidderConfigurationProperties goldbachConfigurationProperties, | ||
| @NotBlank @Value("${external-url}") String externalUrl, | ||
| JacksonMapper mapper) { | ||
Lightwood13 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
| .withConfig(goldbachConfigurationProperties) | ||
| .usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
| .bidderCreator(config -> new GoldbachBidder(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,16 @@ | ||
| adapters: | ||
| goldbach: | ||
| endpoint: https://goldlayer-api.prod.gbads.net/openrtb/2.5/auction | ||
| endpoint-compression: gzip | ||
| meta-info: | ||
| maintainer-email: [email protected] | ||
| app-media-types: | ||
| - banner | ||
| - video | ||
| - native | ||
| site-media-types: | ||
| - banner | ||
| - video | ||
| - native | ||
| supported-vendors: | ||
| vendor-id: 580 |
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.