Skip to content
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

Add Smartadserver bidder #872

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.prebid.server.bidder.smartadserver;

import com.fasterxml.jackson.core.type.TypeReference;
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 io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.http.HttpMethod;
import org.apache.http.client.utils.URIBuilder;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.model.BidderBid;
import org.prebid.server.bidder.model.BidderError;
import org.prebid.server.bidder.model.HttpCall;
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.smartadserver.ExtImpSmartadserver;
import org.prebid.server.proto.openrtb.ext.response.BidType;
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.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* Smartadserver {@link Bidder} implementation.
*/
public class SmartadserverBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpSmartadserver>> SMARTADSERVER_EXT_TYPE_REFERENCE =
new TypeReference<ExtPrebid<?, ExtImpSmartadserver>>() {
};

private final String endpointUrl;
private final JacksonMapper mapper;

public SmartadserverBidder(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<HttpRequest<BidRequest>> result = new ArrayList<>();

for (Imp imp : request.getImp()) {
try {
final ExtImpSmartadserver extImpSmartadserver = parseImpExt(imp);
final BidRequest updateRequest = request.toBuilder()
.imp(Collections.singletonList(imp))
.site(Site.builder()
.publisher(Publisher.builder()
.id(String.valueOf(extImpSmartadserver.getNetworkId()))
.build())
.build())
.build();
result.add(createSingleRequest(updateRequest, getUri()));
} catch (PreBidException e) {
return Result.emptyWithError(BidderError.badInput(e.getMessage()));
}
}
return Result.of(result, Collections.emptyList());
}

private ExtImpSmartadserver parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), SMARTADSERVER_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException(e.getMessage(), e);
}
}

private String getUri() {
final URIBuilder uriBuilder = new URIBuilder()
.setPath(String.join("api/bid", endpointUrl))
.addParameter("callerId", "5");

return uriBuilder.toString();
}

private HttpRequest<BidRequest> createSingleRequest(BidRequest request, String url) {
final BidRequest outgoingRequest = request.toBuilder().build();
final String body = mapper.encode(outgoingRequest);
return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(url)
.headers(HttpUtil.headers())
.body(body)
.payload(outgoingRequest)
.build();
}

@Override
public Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) {
if (httpCall.getResponse().getStatusCode() == HttpResponseStatus.NO_CONTENT.code()) {
return Result.of(Collections.emptyList(), Collections.emptyList());
}

try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return extractBids(httpCall.getRequest().getPayload(), bidResponse);
} catch (DecodeException | PreBidException e) {
return Result.emptyWithError(BidderError.badServerResponse(e.getMessage()));
}
}

private Result<List<BidderBid>> extractBids(BidRequest bidRequest, BidResponse bidResponse) {
if (bidResponse == null || bidResponse.getSeatbid() == null) {
return Result.of(Collections.emptyList(), Collections.emptyList());
}
final List<BidderError> errors = new ArrayList<>();
final List<BidderBid> bidderBids = bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> toBidderBid(bidRequest, bid, bidResponse.getCur(), errors))
.filter(Objects::nonNull)
.collect(Collectors.toList());
return Result.of(bidderBids, errors);
}

private BidderBid toBidderBid(BidRequest bidRequest, Bid bid, String currency, List<BidderError> errors) {
try {
final BidType bidType = getBidType(bid.getImpid(), bidRequest.getImp());
return BidderBid.of(bid, bidType, currency);
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
return null;
}
}

private static BidType getBidType(String impId, List<Imp> imps) {
for (Imp imp : imps) {
if (imp.getId().equals(impId)) {
return imp.getVideo() != null ? BidType.video : BidType.banner;
}
}
return BidType.banner;
}

@Override
public Map<String, String> extractTargeting(ObjectNode ext) {
return Collections.emptyMap();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.prebid.server.proto.openrtb.ext.request.smartadserver;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Defines the contract for bidrequest.imp[i].ext.smartadserver
*/
@AllArgsConstructor(staticName = "of")
@Value
public class ExtImpSmartadserver {

@JsonProperty("siteId")
Integer siteId;

@JsonProperty("pageId")
Integer pageId;

@JsonProperty("formatId")
Integer formatId;

@JsonProperty("networkId")
Integer networkId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.prebid.server.spring.config.bidder;

import org.prebid.server.bidder.BidderDeps;
import org.prebid.server.bidder.smartadserver.SmartadserverBidder;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.spring.config.bidder.model.BidderConfigurationProperties;
import org.prebid.server.spring.config.bidder.model.UsersyncConfigurationProperties;
import org.prebid.server.spring.config.bidder.util.BidderDepsAssembler;
import org.prebid.server.spring.config.bidder.util.BidderInfoCreator;
import org.prebid.server.spring.config.bidder.util.UsersyncerCreator;
import org.prebid.server.spring.env.YamlPropertySourceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 javax.validation.constraints.NotBlank;

@Configuration
@PropertySource(value = "classpath:/bidder-config/smartadserver.yaml", factory = YamlPropertySourceFactory.class)
public class SmartadserverConfiguration {

private static final String BIDDER_NAME = "smartadserver";

@Value("${external-url}")
@NotBlank
private String externalUrl;

@Autowired
private JacksonMapper mapper;

@Autowired
@Qualifier("smartadserverConfigurationProperties")
private BidderConfigurationProperties configProperties;

@Bean("smartadserverConfigurationProperties")
@ConfigurationProperties("adapters.smartadserver")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps smartadserverBidderDeps() {
final UsersyncConfigurationProperties usersync = configProperties.getUsersync();

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(configProperties)
.bidderInfo(BidderInfoCreator.create(configProperties))
.usersyncerCreator(UsersyncerCreator.create(usersync, externalUrl))
.bidderCreator(() -> new SmartadserverBidder(configProperties.getEndpoint(), mapper))
.assemble();
}
}
25 changes: 25 additions & 0 deletions src/main/resources/bidder-config/smartadserver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
adapters:
smartadserver:
enabled: false
endpoint: https://ssb.smartadserver.com
pbs-enforces-gdpr: true
pbs-enforces-ccpa: true
modifying-vast-xml-allowed: true
deprecated-names:
aliases:
meta-info:
maintainer-email: support@smartadserver.com
app-media-types:
- banner
- video
site-media-types:
- banner
- video
supported-vendors:
vendor-id: 328
usersync:
url: https://ssbsync.smartadserver.com/api/sync?callerId=5&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirectUri=
redirect-url: /setuid?bidder=smartadserver&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&uid=[ssb_sync_pid]
cookie-family-name: smartadserver
type: redirect
support-cors: false
35 changes: 35 additions & 0 deletions src/main/resources/static/bidder-params/smartadserver.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Smartadserver Adapter Params",
"description": "A schema which validates params accepted by the Smartadserver adapter",

"type": "object",
"properties": {
"siteId": {
"type": "integer",
"description": "The site id.",
"minimum": 1
},
"pageId": {
"type": "integer",
"description": "The page id.",
"minimum": 1
},
"formatId": {
"type": "integer",
"description": "The format id.",
"minimum": 1
},
"networkId": {
"type": "integer",
"description": "The network id.",
"minimum": 1
}
},
"dependencies": {
"siteId": { "required": ["pageId", "formatId"] },
"pageId": { "required": ["siteId", "formatId"] },
"formatId": { "required": ["siteId", "pageId"] }
},
"required": ["networkId"]
}
Loading