Skip to content

Commit e5c5480

Browse files
authored
New Adapter: Sparteo (#3985)
1 parent 627586d commit e5c5480

File tree

12 files changed

+1469
-0
lines changed

12 files changed

+1469
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package org.prebid.server.bidder.sparteo;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
7+
import com.iab.openrtb.request.BidRequest;
8+
import com.iab.openrtb.request.Imp;
9+
import com.iab.openrtb.request.Publisher;
10+
import com.iab.openrtb.request.Site;
11+
import com.iab.openrtb.response.Bid;
12+
import com.iab.openrtb.response.BidResponse;
13+
import com.iab.openrtb.response.SeatBid;
14+
import org.apache.commons.collections4.CollectionUtils;
15+
import org.prebid.server.bidder.Bidder;
16+
import org.prebid.server.bidder.model.BidderBid;
17+
import org.prebid.server.bidder.model.BidderCall;
18+
import org.prebid.server.bidder.model.BidderError;
19+
import org.prebid.server.bidder.model.HttpRequest;
20+
import org.prebid.server.bidder.model.Result;
21+
import org.prebid.server.exception.PreBidException;
22+
import org.prebid.server.json.DecodeException;
23+
import org.prebid.server.json.JacksonMapper;
24+
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
25+
import org.prebid.server.proto.openrtb.ext.request.ExtPublisher;
26+
import org.prebid.server.proto.openrtb.ext.request.sparteo.ExtImpSparteo;
27+
import org.prebid.server.proto.openrtb.ext.response.BidType;
28+
import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid;
29+
import org.prebid.server.util.BidderUtil;
30+
import org.prebid.server.util.HttpUtil;
31+
32+
import java.util.ArrayList;
33+
import java.util.Collection;
34+
import java.util.Collections;
35+
import java.util.List;
36+
import java.util.Objects;
37+
import java.util.Optional;
38+
39+
public class SparteoBidder implements Bidder<BidRequest> {
40+
41+
private static final TypeReference<ExtPrebid<?, ExtImpSparteo>> TYPE_REFERENCE =
42+
new TypeReference<>() { };
43+
44+
private final String endpointUrl;
45+
private final JacksonMapper mapper;
46+
47+
public SparteoBidder(String endpointUrl, JacksonMapper mapper) {
48+
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
49+
this.mapper = Objects.requireNonNull(mapper);
50+
}
51+
52+
@Override
53+
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) {
54+
final List<BidderError> errors = new ArrayList<>();
55+
final List<Imp> modifiedImps = new ArrayList<>();
56+
String siteNetworkId = null;
57+
58+
for (Imp imp : request.getImp()) {
59+
if (siteNetworkId == null) {
60+
try {
61+
siteNetworkId = parseExtImp(imp).getNetworkId();
62+
} catch (PreBidException e) {
63+
errors.add(BidderError.badInput(
64+
"ignoring imp id=%s, error processing ext: %s".formatted(
65+
imp.getId(), e.getMessage())));
66+
}
67+
}
68+
69+
final ObjectNode modifiedExt = modifyImpExt(imp);
70+
modifiedImps.add(imp.toBuilder().ext(modifiedExt).build());
71+
}
72+
73+
if (modifiedImps.isEmpty()) {
74+
return Result.withErrors(errors);
75+
}
76+
77+
final BidRequest outgoingRequest = request.toBuilder()
78+
.imp(modifiedImps)
79+
.site(modifySite(request.getSite(), siteNetworkId, mapper))
80+
.build();
81+
82+
final HttpRequest<BidRequest> call = BidderUtil.defaultRequest(outgoingRequest, endpointUrl, mapper);
83+
84+
return Result.of(Collections.singletonList(call), errors);
85+
}
86+
87+
private ExtImpSparteo parseExtImp(Imp imp) {
88+
try {
89+
return mapper.mapper().convertValue(imp.getExt(), TYPE_REFERENCE).getBidder();
90+
} catch (IllegalArgumentException e) {
91+
throw new PreBidException("invalid imp.ext");
92+
}
93+
}
94+
95+
private static ObjectNode modifyImpExt(Imp imp) {
96+
final ObjectNode modifiedImpExt = imp.getExt().deepCopy();
97+
final ObjectNode sparteoNode = modifiedImpExt.putObject("sparteo");
98+
final JsonNode bidderJsonNode = modifiedImpExt.remove("bidder");
99+
sparteoNode.set("params", bidderJsonNode);
100+
101+
return modifiedImpExt;
102+
}
103+
104+
private Site modifySite(Site site, String siteNetworkId, JacksonMapper mapper) {
105+
if (site == null || site.getPublisher() == null || siteNetworkId == null) {
106+
return site;
107+
}
108+
109+
final Publisher originalPublisher = site.getPublisher();
110+
final ExtPublisher originalExt = originalPublisher.getExt();
111+
112+
final ExtPublisher modifiedExt = originalExt != null
113+
? ExtPublisher.of(originalExt.getPrebid())
114+
: ExtPublisher.empty();
115+
116+
if (originalExt != null) {
117+
mapper.fillExtension(modifiedExt, originalExt);
118+
}
119+
120+
final JsonNode paramsProperty = modifiedExt.getProperty("params");
121+
final ObjectNode paramsNode;
122+
123+
if (paramsProperty != null && paramsProperty.isObject()) {
124+
paramsNode = (ObjectNode) paramsProperty;
125+
} else {
126+
paramsNode = mapper.mapper().createObjectNode();
127+
modifiedExt.addProperty("params", paramsNode);
128+
}
129+
130+
paramsNode.put("networkId", siteNetworkId);
131+
132+
final Publisher modifiedPublisher = originalPublisher.toBuilder()
133+
.ext(modifiedExt)
134+
.build();
135+
136+
return site.toBuilder()
137+
.publisher(modifiedPublisher)
138+
.build();
139+
}
140+
141+
@Override
142+
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) {
143+
try {
144+
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
145+
final List<BidderError> errors = new ArrayList<>();
146+
return Result.of(extractBids(bidResponse, errors), errors);
147+
} catch (DecodeException e) {
148+
return Result.withError(BidderError.badServerResponse(e.getMessage()));
149+
}
150+
}
151+
152+
private List<BidderBid> extractBids(BidResponse bidResponse, List<BidderError> errors) {
153+
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
154+
return Collections.emptyList();
155+
}
156+
157+
return bidResponse.getSeatbid().stream()
158+
.filter(Objects::nonNull)
159+
.map(SeatBid::getBid)
160+
.filter(Objects::nonNull)
161+
.flatMap(Collection::stream)
162+
.filter(Objects::nonNull)
163+
.map(bid -> toBidderBid(bid, bidResponse.getCur(), errors))
164+
.filter(Objects::nonNull)
165+
.toList();
166+
}
167+
168+
private BidderBid toBidderBid(Bid bid, String currency, List<BidderError> errors) {
169+
try {
170+
final BidType bidType = getBidType(bid);
171+
172+
final Integer mtype = switch (bidType) {
173+
case banner -> 1;
174+
case video -> 2;
175+
case xNative -> 4;
176+
default -> null;
177+
};
178+
179+
final Bid bidWithMtype = mtype != null ? bid.toBuilder().mtype(mtype).build() : bid;
180+
181+
return BidderBid.of(bidWithMtype, bidType, currency);
182+
} catch (PreBidException e) {
183+
errors.add(BidderError.badServerResponse(e.getMessage()));
184+
return null;
185+
}
186+
}
187+
188+
private BidType getBidType(Bid bid) throws PreBidException {
189+
final BidType bidType = Optional.ofNullable(bid.getExt())
190+
.map(ext -> ext.get("prebid"))
191+
.filter(JsonNode::isObject)
192+
.map(this::parseExtBidPrebid)
193+
.map(ExtBidPrebid::getType)
194+
.orElseThrow(() -> new PreBidException(
195+
"Failed to parse bid mediatype for impression \"%s\"".formatted(bid.getImpid())));
196+
197+
if (bidType == BidType.audio) {
198+
throw new PreBidException(
199+
"Audio bid type not supported by this adapter for impression id: %s".formatted(bid.getImpid()));
200+
}
201+
202+
return bidType;
203+
}
204+
205+
private ExtBidPrebid parseExtBidPrebid(JsonNode prebidNode) {
206+
try {
207+
return mapper.mapper().treeToValue(prebidNode, ExtBidPrebid.class);
208+
} catch (JsonProcessingException e) {
209+
return null;
210+
}
211+
}
212+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.prebid.server.proto.openrtb.ext.request.sparteo;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.Value;
5+
6+
@Value(staticConstructor = "of")
7+
public class ExtImpSparteo {
8+
9+
@JsonProperty("networkId")
10+
String networkId;
11+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.prebid.server.spring.config.bidder;
2+
3+
import org.prebid.server.bidder.BidderDeps;
4+
import org.prebid.server.bidder.sparteo.SparteoBidder;
5+
import org.prebid.server.json.JacksonMapper;
6+
import org.prebid.server.spring.config.bidder.model.BidderConfigurationProperties;
7+
import org.prebid.server.spring.config.bidder.util.BidderDepsAssembler;
8+
import org.prebid.server.spring.config.bidder.util.UsersyncerCreator;
9+
import org.prebid.server.spring.env.YamlPropertySourceFactory;
10+
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.boot.context.properties.ConfigurationProperties;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.context.annotation.PropertySource;
15+
16+
import jakarta.validation.constraints.NotBlank;
17+
18+
@Configuration
19+
@PropertySource(value = "classpath:/bidder-config/sparteo.yaml",
20+
factory = YamlPropertySourceFactory.class)
21+
public class SparteoConfiguration {
22+
23+
private static final String BIDDER_NAME = "sparteo";
24+
25+
@Bean("sparteoConfigurationProperties")
26+
@ConfigurationProperties("adapters.sparteo")
27+
public BidderConfigurationProperties configurationProperties() {
28+
return new BidderConfigurationProperties();
29+
}
30+
31+
@Bean
32+
public BidderDeps sparteoBidderDeps(BidderConfigurationProperties sparteoConfigurationProperties,
33+
@NotBlank @Value("${external-url}") String externalUrl,
34+
JacksonMapper mapper) {
35+
36+
return BidderDepsAssembler.forBidder(BIDDER_NAME)
37+
.withConfig(sparteoConfigurationProperties)
38+
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
39+
.bidderCreator(config -> new SparteoBidder(config.getEndpoint(), mapper))
40+
.assemble();
41+
}
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
adapters:
2+
sparteo:
3+
endpoint: https://bid.sparteo.com/s2s-auction
4+
meta-info:
5+
maintainer-email: prebid@sparteo.com
6+
app-media-types:
7+
- banner
8+
- video
9+
- native
10+
site-media-types:
11+
- banner
12+
- video
13+
- native
14+
supported-vendors:
15+
vendor-id: 1028
16+
usersync:
17+
cookie-family-name: sparteo
18+
iframe:
19+
url: "https://sync.sparteo.com/s2s_sync?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect_url={{redirect_url}}"
20+
support-cors: true
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "Sparteo Params",
4+
"type": "object",
5+
"properties": {
6+
"networkId": {
7+
"type": "string",
8+
"description": "Sparteo network ID. This information will be given to you by the Sparteo team."
9+
},
10+
"custom1": {
11+
"type": "string",
12+
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore."
13+
},
14+
"custom2": {
15+
"type": "string",
16+
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore."
17+
},
18+
"custom3": {
19+
"type": "string",
20+
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore."
21+
},
22+
"custom4": {
23+
"type": "string",
24+
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore."
25+
},
26+
"custom5": {
27+
"type": "string",
28+
"description": "To be used in reporting. Alphanumeric strings ; case sensitive ; max 40 characters ; only allowed symbols are hyphen and underscore."
29+
}
30+
},
31+
"required": [
32+
"networkId"
33+
]
34+
}

0 commit comments

Comments
 (0)