-
Notifications
You must be signed in to change notification settings - Fork 216
New Adapter: Sparteo #3985
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
Merged
+1,469
−0
Merged
New Adapter: Sparteo #3985
Changes from all commits
Commits
Show all changes
2 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
212 changes: 212 additions & 0 deletions
212
src/main/java/org/prebid/server/bidder/sparteo/SparteoBidder.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,212 @@ | ||
package org.prebid.server.bidder.sparteo; | ||
|
||
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.request.Publisher; | ||
import com.iab.openrtb.request.Site; | ||
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.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.ExtPublisher; | ||
import org.prebid.server.proto.openrtb.ext.request.sparteo.ExtImpSparteo; | ||
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.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class SparteoBidder implements Bidder<BidRequest> { | ||
t-sormonte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private static final TypeReference<ExtPrebid<?, ExtImpSparteo>> TYPE_REFERENCE = | ||
new TypeReference<>() { }; | ||
|
||
private final String endpointUrl; | ||
private final JacksonMapper mapper; | ||
|
||
public SparteoBidder(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 List<Imp> modifiedImps = new ArrayList<>(); | ||
String siteNetworkId = null; | ||
|
||
for (Imp imp : request.getImp()) { | ||
if (siteNetworkId == null) { | ||
try { | ||
siteNetworkId = parseExtImp(imp).getNetworkId(); | ||
} catch (PreBidException e) { | ||
errors.add(BidderError.badInput( | ||
"ignoring imp id=%s, error processing ext: %s".formatted( | ||
imp.getId(), e.getMessage()))); | ||
} | ||
} | ||
|
||
final ObjectNode modifiedExt = modifyImpExt(imp); | ||
modifiedImps.add(imp.toBuilder().ext(modifiedExt).build()); | ||
} | ||
|
||
if (modifiedImps.isEmpty()) { | ||
return Result.withErrors(errors); | ||
} | ||
|
||
final BidRequest outgoingRequest = request.toBuilder() | ||
.imp(modifiedImps) | ||
.site(modifySite(request.getSite(), siteNetworkId, mapper)) | ||
.build(); | ||
|
||
final HttpRequest<BidRequest> call = BidderUtil.defaultRequest(outgoingRequest, endpointUrl, mapper); | ||
|
||
return Result.of(Collections.singletonList(call), errors); | ||
} | ||
|
||
private ExtImpSparteo parseExtImp(Imp imp) { | ||
try { | ||
return mapper.mapper().convertValue(imp.getExt(), TYPE_REFERENCE).getBidder(); | ||
} catch (IllegalArgumentException e) { | ||
throw new PreBidException("invalid imp.ext"); | ||
} | ||
} | ||
|
||
private static ObjectNode modifyImpExt(Imp imp) { | ||
final ObjectNode modifiedImpExt = imp.getExt().deepCopy(); | ||
final ObjectNode sparteoNode = modifiedImpExt.putObject("sparteo"); | ||
final JsonNode bidderJsonNode = modifiedImpExt.remove("bidder"); | ||
sparteoNode.set("params", bidderJsonNode); | ||
|
||
return modifiedImpExt; | ||
} | ||
|
||
private Site modifySite(Site site, String siteNetworkId, JacksonMapper mapper) { | ||
if (site == null || site.getPublisher() == null || siteNetworkId == null) { | ||
return site; | ||
} | ||
t-sormonte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
final Publisher originalPublisher = site.getPublisher(); | ||
final ExtPublisher originalExt = originalPublisher.getExt(); | ||
|
||
final ExtPublisher modifiedExt = originalExt != null | ||
? ExtPublisher.of(originalExt.getPrebid()) | ||
: ExtPublisher.empty(); | ||
|
||
if (originalExt != null) { | ||
mapper.fillExtension(modifiedExt, originalExt); | ||
} | ||
|
||
final JsonNode paramsProperty = modifiedExt.getProperty("params"); | ||
final ObjectNode paramsNode; | ||
|
||
if (paramsProperty != null && paramsProperty.isObject()) { | ||
paramsNode = (ObjectNode) paramsProperty; | ||
} else { | ||
paramsNode = mapper.mapper().createObjectNode(); | ||
modifiedExt.addProperty("params", paramsNode); | ||
} | ||
|
||
paramsNode.put("networkId", siteNetworkId); | ||
|
||
final Publisher modifiedPublisher = originalPublisher.toBuilder() | ||
.ext(modifiedExt) | ||
.build(); | ||
|
||
return site.toBuilder() | ||
.publisher(modifiedPublisher) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
try { | ||
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
final List<BidderError> errors = new ArrayList<>(); | ||
return Result.of(extractBids(bidResponse, errors), 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(); | ||
} | ||
|
||
return bidResponse.getSeatbid().stream() | ||
.filter(Objects::nonNull) | ||
.map(SeatBid::getBid) | ||
.filter(Objects::nonNull) | ||
.flatMap(Collection::stream) | ||
.filter(Objects::nonNull) | ||
.map(bid -> toBidderBid(bid, bidResponse.getCur(), errors)) | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
} | ||
|
||
private BidderBid toBidderBid(Bid bid, String currency, List<BidderError> errors) { | ||
try { | ||
final BidType bidType = getBidType(bid); | ||
|
||
final Integer mtype = switch (bidType) { | ||
case banner -> 1; | ||
case video -> 2; | ||
case xNative -> 4; | ||
default -> null; | ||
}; | ||
|
||
final Bid bidWithMtype = mtype != null ? bid.toBuilder().mtype(mtype).build() : bid; | ||
|
||
return BidderBid.of(bidWithMtype, bidType, currency); | ||
} catch (PreBidException e) { | ||
errors.add(BidderError.badServerResponse(e.getMessage())); | ||
return null; | ||
} | ||
} | ||
|
||
private BidType getBidType(Bid bid) throws PreBidException { | ||
final BidType bidType = Optional.ofNullable(bid.getExt()) | ||
.map(ext -> ext.get("prebid")) | ||
.filter(JsonNode::isObject) | ||
.map(this::parseExtBidPrebid) | ||
.map(ExtBidPrebid::getType) | ||
.orElseThrow(() -> new PreBidException( | ||
"Failed to parse bid mediatype for impression \"%s\"".formatted(bid.getImpid()))); | ||
|
||
if (bidType == BidType.audio) { | ||
throw new PreBidException( | ||
"Audio bid type not supported by this adapter for impression id: %s".formatted(bid.getImpid())); | ||
} | ||
|
||
return bidType; | ||
} | ||
|
||
private ExtBidPrebid parseExtBidPrebid(JsonNode prebidNode) { | ||
try { | ||
return mapper.mapper().treeToValue(prebidNode, ExtBidPrebid.class); | ||
} catch (JsonProcessingException e) { | ||
return null; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/prebid/server/proto/openrtb/ext/request/sparteo/ExtImpSparteo.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.sparteo; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Value; | ||
|
||
@Value(staticConstructor = "of") | ||
public class ExtImpSparteo { | ||
|
||
@JsonProperty("networkId") | ||
String networkId; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/prebid/server/spring/config/bidder/SparteoConfiguration.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,42 @@ | ||
package org.prebid.server.spring.config.bidder; | ||
|
||
import org.prebid.server.bidder.BidderDeps; | ||
import org.prebid.server.bidder.sparteo.SparteoBidder; | ||
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/sparteo.yaml", | ||
factory = YamlPropertySourceFactory.class) | ||
public class SparteoConfiguration { | ||
|
||
private static final String BIDDER_NAME = "sparteo"; | ||
|
||
@Bean("sparteoConfigurationProperties") | ||
@ConfigurationProperties("adapters.sparteo") | ||
public BidderConfigurationProperties configurationProperties() { | ||
return new BidderConfigurationProperties(); | ||
} | ||
|
||
@Bean | ||
public BidderDeps sparteoBidderDeps(BidderConfigurationProperties sparteoConfigurationProperties, | ||
@NotBlank @Value("${external-url}") String externalUrl, | ||
JacksonMapper mapper) { | ||
|
||
return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
.withConfig(sparteoConfigurationProperties) | ||
.usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
.bidderCreator(config -> new SparteoBidder(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,20 @@ | ||
adapters: | ||
sparteo: | ||
endpoint: https://bid.sparteo.com/s2s-auction | ||
meta-info: | ||
maintainer-email: prebid@sparteo.com | ||
app-media-types: | ||
- banner | ||
- video | ||
- native | ||
site-media-types: | ||
- banner | ||
- video | ||
- native | ||
supported-vendors: | ||
vendor-id: 1028 | ||
usersync: | ||
cookie-family-name: sparteo | ||
iframe: | ||
url: "https://sync.sparteo.com/s2s_sync?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect_url={{redirect_url}}" | ||
support-cors: true |
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,34 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "Sparteo Params", | ||
"type": "object", | ||
"properties": { | ||
"networkId": { | ||
"type": "string", | ||
"description": "Sparteo network ID. This information will be given to you by the Sparteo team." | ||
}, | ||
"custom1": { | ||
"type": "string", | ||
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore." | ||
}, | ||
"custom2": { | ||
"type": "string", | ||
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore." | ||
}, | ||
"custom3": { | ||
"type": "string", | ||
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore." | ||
}, | ||
"custom4": { | ||
"type": "string", | ||
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore." | ||
}, | ||
"custom5": { | ||
"type": "string", | ||
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore." | ||
} | ||
}, | ||
"required": [ | ||
"networkId" | ||
] | ||
} |
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.