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

Core: Add new bidder Kargo #1933

Merged
merged 5 commits into from
Jul 7, 2022
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
103 changes: 103 additions & 0 deletions src/main/java/org/prebid/server/bidder/kargo/KargoBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.prebid.server.bidder.kargo;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.collections4.CollectionUtils;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.kargo.model.KargoExtBid;
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.response.BidType;
import org.prebid.server.util.HttpUtil;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class KargoBidder implements Bidder<BidRequest> {

private final String endpointUrl;
private final JacksonMapper mapper;

public KargoBidder(String endpointUrl, JacksonMapper mapper) {
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
this.mapper = Objects.requireNonNull(mapper);
}

@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) {
return Result.withValue(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(HttpUtil.headers())
.body(mapper.encodeToBytes(bidRequest))
.payload(bidRequest)
.build());
}

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

private List<BidderBid> extractBids(BidResponse bidResponse) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
return bidsFromResponse(bidResponse);
}

private List<BidderBid> bidsFromResponse(BidResponse bidResponse) {
return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(bid, resolvedBidType(bid), bidResponse.getCur()))
.collect(Collectors.toList());
}

private BidType resolvedBidType(Bid bid) {
final KargoExtBid kargoExtBid = bid.getExt() != null ? getMappedBidExt(bid.getExt()) : null;
final String mediaType = kargoExtBid != null ? kargoExtBid.getMediaType() : null;

if (mediaType == null) {
return BidType.banner;
}

switch (mediaType) {
case "video":
return BidType.video;
case "native":
return BidType.xNative;
default:
return BidType.banner;
}
}

private KargoExtBid getMappedBidExt(ObjectNode objectNode) {
try {
return mapper.mapper().convertValue(objectNode, KargoExtBid.class);
} catch (IllegalArgumentException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.prebid.server.bidder.kargo.model;

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

@Value
public class KargoExtBid {

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

import lombok.Value;

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

String adSlotID;
}
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.kargo.KargoBidder;
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 javax.validation.constraints.NotBlank;

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

private static final String BIDDER_NAME = "kargo";

@Bean("kargoConfigurationProperties")
@ConfigurationProperties("adapters.kargo")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

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

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(kargoConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new KargoBidder(config.getEndpoint(), mapper))
.assemble();
}

}
19 changes: 19 additions & 0 deletions src/main/resources/bidder-config/kargo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
adapters:
kargo:
endpoint: https://krk.kargo.com/api/v1/openrtb
modifyingVastXmlAllowed: true
meta-info:
maintainer-email: kraken@kargo.com
app-media-types:
site-media-types:
- banner
- video
- native
supported-vendors:
vendor-id: 972
usersync:
url:
redirect-url:
cookie-family-name: kargo
type: redirect
support-cors: false
15 changes: 15 additions & 0 deletions src/main/resources/static/bidder-params/kargo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Kargo Adapter Params",
"description": "A schema which validates params accepted by the Kargo adapter",
"type": "object",

"properties": {
"adSlotID": {
"type": "string",
"description": "An ID which identifies the adslot placement. Equivalent to the id of target inventory, ad unit code, or placement id"
}
},

"required": ["adSlotID"]
}
Loading