Skip to content

Commit

Permalink
Playdigo: New Adapter (prebid#3210)
Browse files Browse the repository at this point in the history
  • Loading branch information
przemkaczmarek authored Jun 3, 2024
1 parent 17ba578 commit 4626c55
Show file tree
Hide file tree
Showing 13 changed files with 792 additions and 0 deletions.
148 changes: 148 additions & 0 deletions src/main/java/org/prebid/server/bidder/playdigo/PlaydigoBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package org.prebid.server.bidder.playdigo;

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.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import io.micrometer.common.util.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
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.playdigo.ExtImpPlaydigo;
import org.prebid.server.proto.openrtb.ext.request.playdigo.PlaydigoImpExt;
import org.prebid.server.proto.openrtb.ext.response.BidType;
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;

public class PlaydigoBidder implements Bidder<BidRequest> {

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

private static final String PUBLISHER_PROPERTY = "publisher";
private static final String NETWORK_PROPERTY = "network";
private static final String BIDDER_PROPERTY = "bidder";

private final String endpointUrl;
private final JacksonMapper mapper;

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

for (Imp imp : request.getImp()) {
final ExtImpPlaydigo extImpPlaydigo;
try {
extImpPlaydigo = parseExtImp(imp);
final Imp modifiedImp = modifyImp(imp, extImpPlaydigo);
httpRequests.add(makeHttpRequest(request, modifiedImp));
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
}
}

if (httpRequests.isEmpty()) {
return Result.withError(BidderError.badInput("found no valid impressions"));
}

return Result.of(httpRequests, errors);
}

private ExtImpPlaydigo parseExtImp(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), PLAYDIGO_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException(e.getMessage());
}
}

private Imp modifyImp(Imp imp, ExtImpPlaydigo extImpPlaydigo) {
final PlaydigoImpExt impExtPlaydigoWithType = resolveImpExt(extImpPlaydigo);
final ObjectNode modifiedImpExtBidder = mapper.mapper().createObjectNode();
modifiedImpExtBidder.set(BIDDER_PROPERTY, mapper.mapper().valueToTree(impExtPlaydigoWithType));

return imp.toBuilder().ext(modifiedImpExtBidder).build();
}

private PlaydigoImpExt resolveImpExt(ExtImpPlaydigo extImpPlaydigo) {
final PlaydigoImpExt.PlaydigoImpExtBuilder builder = PlaydigoImpExt.builder();

if (StringUtils.isNotEmpty(extImpPlaydigo.getPlacementId())) {
builder.type(PUBLISHER_PROPERTY).placementId(extImpPlaydigo.getPlacementId());
} else if (StringUtils.isNotEmpty(extImpPlaydigo.getEndpointId())) {
builder.type(NETWORK_PROPERTY).endpointId(extImpPlaydigo.getEndpointId());
}

return builder.build();
}

private HttpRequest<BidRequest> makeHttpRequest(BidRequest request, Imp imp) {
final BidRequest outgoingRequest = request.toBuilder().imp(List.of(imp)).build();

return BidderUtil.defaultRequest(outgoingRequest, endpointUrl, mapper);
}

@Override
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
final List<BidderBid> bids = extractBids(httpCall.getRequest().getPayload(), bidResponse);
return Result.withValues(bids);
} catch (DecodeException | PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidResponse) {
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 -> BidderBid.of(bid, getBidType(bid), bidResponse.getCur()))
.toList();
}

private BidType getBidType(Bid bid) {
final Integer markupType = ObjectUtils.defaultIfNull(bid.getMtype(), 0);

return switch (markupType) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
case 3 -> BidType.audio;
case 4 -> BidType.xNative;
default -> throw new PreBidException(
"could not define media type for impression: " + bid.getImpid());
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.prebid.server.proto.openrtb.ext.request.playdigo;

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

@Value(staticConstructor = "of")
public class ExtImpPlaydigo {

@JsonProperty("placementId")
String placementId;

@JsonProperty("endpointId")
String endpointId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.prebid.server.proto.openrtb.ext.request.playdigo;

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

@Value
@Builder
public class PlaydigoImpExt {

String type;

@JsonProperty("placementId")
String placementId;

@JsonProperty("endpointId")
String endpointId;
}
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.playdigo.PlaydigoBidder;
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/playdigo.yaml", factory = YamlPropertySourceFactory.class)
public class PlaydigoConfiguration {

private static final String BIDDER_NAME = "playdigo";

@Bean("playdigoConfigurationProperties")
@ConfigurationProperties("adapters.playdigo")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps playdigoBidderDeps(BidderConfigurationProperties playdigoConfigurationProperties,
@NotBlank @Value("${external-url}") String externalUrl,
JacksonMapper mapper) {

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(playdigoConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new PlaydigoBidder(config.getEndpoint(), mapper))
.assemble();
}
}
17 changes: 17 additions & 0 deletions src/main/resources/bidder-config/playdigo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
adapters:
playdigo:
endpoint: https://server.playdigo.com/pserver
geoscope:
- USA
meta-info:
maintainer-email: [email protected]
app-media-types:
- banner
- video
- native
site-media-types:
- banner
- video
- native
supported-vendors:
vendor-id: 0
30 changes: 30 additions & 0 deletions src/main/resources/static/bidder-params/playdigo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Playdigo Adapter Params",
"description": "A schema which validates params accepted by the Playdigo adapter",
"type": "object",
"properties": {
"placementId": {
"type": "string",
"minLength": 1,
"description": "Placement ID"
},
"endpointId": {
"type": "string",
"minLength": 1,
"description": "Endpoint ID"
}
},
"oneOf": [
{
"required": [
"placementId"
]
},
{
"required": [
"endpointId"
]
}
]
}
Loading

0 comments on commit 4626c55

Please sign in to comment.