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

KiviAds: Add adapter #2156

Merged
merged 4 commits into from
Feb 20, 2023
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
152 changes: 152 additions & 0 deletions src/main/java/org/prebid/server/bidder/kiviads/KiviAdsBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package org.prebid.server.bidder.kiviads;

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.BidResponse;
import com.iab.openrtb.response.SeatBid;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.kiviads.proto.KiviAdsImpExtBidder;
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.kiviads.ExtImpKiviAds;
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.Objects;

public class KiviAdsBidder implements Bidder<BidRequest> {

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

private final String endpointUrl;
private final JacksonMapper mapper;

public KiviAdsBidder(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>> outgoingRequests = new ArrayList<>();

for (Imp imp : request.getImp()) {
final ExtImpKiviAds extImpKiviAds;
try {
extImpKiviAds = parseImpExt(imp);
} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}
outgoingRequests.add(createSingleRequest(modifyImp(imp, extImpKiviAds), request));
}

return Result.withValues(outgoingRequests);
}

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

private Imp modifyImp(Imp imp, ExtImpKiviAds extImpKiviAds) {
final KiviAdsImpExtBidder kiviAdsImpExtBidderWithType = getImpExtKiviAdsWithType(extImpKiviAds);
final ObjectNode modifiedImpExtBidder = mapper.mapper().createObjectNode();

modifiedImpExtBidder.set("bidder", mapper.mapper().valueToTree(kiviAdsImpExtBidderWithType));

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

private KiviAdsImpExtBidder getImpExtKiviAdsWithType(ExtImpKiviAds extImpKiviAds) {
final KiviAdsImpExtBidder.KiviAdsImpExtBidderBuilder impExtKiviAds = KiviAdsImpExtBidder.builder();

if (StringUtils.isNotEmpty(extImpKiviAds.getPlacementId())) {
impExtKiviAds
.type("publisher")
.placementId(extImpKiviAds.getPlacementId());
} else if (StringUtils.isNotEmpty(extImpKiviAds.getEndpointId())) {
impExtKiviAds
.type("network")
.endpointId(extImpKiviAds.getEndpointId());
}

return impExtKiviAds.build();
}

private HttpRequest<BidRequest> createSingleRequest(Imp imp, BidRequest request) {
final BidRequest outgoingRequest = request.toBuilder().imp(Collections.singletonList(imp)).build();

return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(HttpUtil.headers())
.body(mapper.encodeToBytes(outgoingRequest))
.payload(outgoingRequest)
.build();
}

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

private static 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.getImpid(), bidRequest.getImp()), bidResponse.getCur()))
.toList();
}

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

throw new PreBidException("Failed to find impression '%s'".formatted(impId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.prebid.server.bidder.kiviads.proto;

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

@Builder
@Value
public class KiviAdsImpExtBidder {

String type;

@JsonProperty("placementId")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
String placementId;

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

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

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

@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.kiviads.KiviAdsBidder;
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/kiviads.yaml", factory = YamlPropertySourceFactory.class)
public class KiviAdsBidderConfiguration {

private static final String BIDDER_NAME = "kiviads";

@Bean("kiviAdsConfigurationProperties")
@ConfigurationProperties("adapters.kiviads")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

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

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(kiviAdsConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new KiviAdsBidder(config.getEndpoint(), mapper))
.assemble();
}
}
21 changes: 21 additions & 0 deletions src/main/resources/bidder-config/kiviads.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
adapters:
kiviads:
endpoint: http://lb.kiviads.com/pserver
meta-info:
maintainer-email: prebid@kiviads.com
app-media-types:
- banner
- video
- native
site-media-types:
- banner
- video
- native
supported-vendors:
vendor-id: 0
usersync:
cookie-family-name: kiviads
iframe:
url: https://sync.kiviads.com/pserver?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&ccpa={{us_privacy}}&redir={{redirect_url}}
support-cors: false
uid-macro: '[UID]'
30 changes: 30 additions & 0 deletions src/main/resources/static/bidder-params/kiviads.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Kivi Adapter Params",
"description": "A schema which validates params accepted by the Kivi 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