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

Insticator: Add Bidder #3647

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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,264 @@
package org.prebid.server.bidder.insticator;

import com.fasterxml.jackson.core.type.TypeReference;
import com.iab.openrtb.request.App;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Device;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.Publisher;
import com.iab.openrtb.request.Site;
import com.iab.openrtb.request.Video;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import io.vertx.core.MultiMap;
import org.apache.commons.collections4.CollectionUtils;
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.Price;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.currency.CurrencyConversionService;
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.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.insticator.ExtImpInsticator;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;
import org.prebid.server.util.ObjectUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class InsticatorBidder implements Bidder<BidRequest> {

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

private static final String DEFAULT_BIDDER_CURRENCY = "USD";
private static final String INSTICATOR_FIELD = "insticator";
private static final InsticatorExtRequestCaller DEFAULT_INSTICATOR_CALLER =
InsticatorExtRequestCaller.of("Prebid-Server", "n/a");

private final CurrencyConversionService currencyConversionService;
private final String endpointUrl;
private final JacksonMapper mapper;

public InsticatorBidder(CurrencyConversionService currencyConversionService,
String endpointUrl,
JacksonMapper mapper) {

this.currencyConversionService = Objects.requireNonNull(currencyConversionService);
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
this.mapper = Objects.requireNonNull(mapper);
}

@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) {
final Map<String, List<Imp>> groupedImps = new HashMap<>();
final List<BidderError> errors = new ArrayList<>();

String publisherId = null;

for (Imp imp : request.getImp()) {
try {
validateImp(imp);
final ExtImpInsticator extImp = parseImpExt(imp);

if (publisherId == null) {
publisherId = extImp.getPublisherId();
}

final Imp modifiedImp = modifyImp(request, imp, extImp);
groupedImps.computeIfAbsent(extImp.getAdUnitId(), key -> new ArrayList<>()).add(modifiedImp);
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
}
}

final BidRequest modifiedRequest = modifyRequest(request, publisherId, errors);
final List<HttpRequest<BidRequest>> requests = groupedImps.values().stream()
.map(imps -> modifiedRequest.toBuilder().imp(imps).build())
.map(finalRequest -> BidderUtil.defaultRequest(
finalRequest,
makeHeaders(finalRequest.getDevice()),
endpointUrl,
mapper))
.toList();

return Result.of(requests, errors);
}

private void validateImp(Imp imp) {
final Video video = imp.getVideo();
if (video == null) {
return;
}

if (isInvalidDimension(video.getH())
|| isInvalidDimension(video.getW())
|| CollectionUtils.isNotEmpty(video.getMimes())) {

throw new PreBidException("One or more invalid or missing video field(s) w, h, mimes");
}
}

private static boolean isInvalidDimension(Integer dimension) {
return dimension == null || dimension == 0;
}

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

private Imp modifyImp(BidRequest request, Imp imp, ExtImpInsticator extImp) {
final Price bidFloorPrice = resolveBidFloor(request, imp);
return imp.toBuilder()
.ext(mapper.mapper().createObjectNode().set(INSTICATOR_FIELD, mapper.mapper().valueToTree(extImp)))
.bidfloorcur(bidFloorPrice.getCurrency())
.bidfloor(bidFloorPrice.getValue())
.build();
}

private Price resolveBidFloor(BidRequest bidRequest, Imp imp) {
final Price initialBidFloorPrice = Price.of(imp.getBidfloorcur(), imp.getBidfloor());
return BidderUtil.isValidPrice(initialBidFloorPrice)
? convertBidFloor(initialBidFloorPrice, bidRequest)
: initialBidFloorPrice;
}

private Price convertBidFloor(Price bidFloorPrice, BidRequest bidRequest) {
final BigDecimal convertedPrice = currencyConversionService.convertCurrency(
bidFloorPrice.getValue(),
bidRequest,
bidFloorPrice.getCurrency(),
DEFAULT_BIDDER_CURRENCY);

return Price.of(DEFAULT_BIDDER_CURRENCY, BidderUtil.roundFloor(convertedPrice));
}

private BidRequest modifyRequest(BidRequest request, String publisherId, List<BidderError> errors) {
return request.toBuilder()
.site(modifySite(request.getSite(), publisherId))
.app(modifyApp(request.getApp(), publisherId))
.ext(modifyExtRequest(request.getExt(), errors))
.build();
}

private static Site modifySite(Site site, String id) {
return Optional.ofNullable(site)
.map(Site::toBuilder)
.map(builder -> builder.publisher(modifyPublisher(site.getPublisher(), id)))
.map(Site.SiteBuilder::build)
.orElse(null);
}

private static App modifyApp(App app, String id) {
return Optional.ofNullable(app)
.map(App::toBuilder)
.map(builder -> builder.publisher(modifyPublisher(app.getPublisher(), id)))
.map(App.AppBuilder::build)
.orElse(null);
}

private static Publisher modifyPublisher(Publisher publisher, String id) {
return Optional.ofNullable(publisher)
.map(Publisher::toBuilder)
.orElseGet(Publisher::builder)
.id(id)
.build();
}

private ExtRequest modifyExtRequest(ExtRequest extRequest, List<BidderError> errors) {
final ExtRequest modifiedExtRequest = extRequest == null ? ExtRequest.empty() : extRequest;
final InsticatorExtRequest existingInsticator;

try {
existingInsticator = mapper.mapper().convertValue(
modifiedExtRequest.getProperty(INSTICATOR_FIELD),
InsticatorExtRequest.class);
} catch (IllegalArgumentException e) {
errors.add(BidderError.badInput(e.getMessage()));
return modifiedExtRequest;
}

modifiedExtRequest.addProperty(
INSTICATOR_FIELD,
mapper.mapper().valueToTree(buildInsticator(existingInsticator)));

return modifiedExtRequest;
}
AntoxaAntoxic marked this conversation as resolved.
Show resolved Hide resolved

private static InsticatorExtRequest buildInsticator(InsticatorExtRequest existingInsticator) {
if (existingInsticator == null || CollectionUtils.isEmpty(existingInsticator.getCaller())) {
return InsticatorExtRequest.of(Collections.singletonList(DEFAULT_INSTICATOR_CALLER));
}

final List<InsticatorExtRequestCaller> callers = new ArrayList<>(existingInsticator.getCaller());
callers.add(DEFAULT_INSTICATOR_CALLER);
return InsticatorExtRequest.of(callers);
}

private static MultiMap makeHeaders(Device device) {
final MultiMap headers = HttpUtil.headers();
HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.USER_AGENT_HEADER,
ObjectUtil.getIfNotNull(device, Device::getUa));
HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER,
ObjectUtil.getIfNotNull(device, Device::getIp));
HttpUtil.addHeaderIfValueIsNotEmpty(headers, "IP",
ObjectUtil.getIfNotNull(device, Device::getIp));
HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER,
ObjectUtil.getIfNotNull(device, Device::getIpv6));

return headers;
}

@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(bidResponse));
} catch (DecodeException | PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(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()))
.filter(Objects::nonNull)
.toList();
}

private static BidType getBidType(Bid bid) {
return switch (bid.getMtype()) {
case 2 -> BidType.video;
case null, default -> BidType.banner;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.prebid.server.bidder.insticator;

import lombok.Value;

import java.util.List;

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

List<InsticatorExtRequestCaller> caller;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.prebid.server.bidder.insticator;

import lombok.Value;

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

String name;

String version;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.prebid.server.proto.openrtb.ext.request.insticator;

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

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

@JsonProperty("adUnitId")
String adUnitId;

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

import org.prebid.server.bidder.BidderDeps;
import org.prebid.server.bidder.insticator.InsticatorBidder;
import org.prebid.server.currency.CurrencyConversionService;
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/insticator.yaml", factory = YamlPropertySourceFactory.class)
public class InsticatorConfiguration {

private static final String BIDDER_NAME = "insticator";

@Bean("insticatorConfigurationProperties")
@ConfigurationProperties("adapters.insticator")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps insticatorBidderDeps(BidderConfigurationProperties insticatorConfigurationProperties,
@NotBlank @Value("${external-url}") String externalUrl,
CurrencyConversionService currencyConversionService,
JacksonMapper mapper) {

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(insticatorConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new InsticatorBidder(currencyConversionService, config.getEndpoint(), mapper))
.assemble();
}
}
19 changes: 19 additions & 0 deletions src/main/resources/bidder-config/insticator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
adapters:
insticator:
endpoint: https://ex.ingage.tech/v1/prebidserver
meta-info:
maintainer-email: [email protected]
app-media-types:
- banner
- video
site-media-types:
- banner
- video
supported-vendors:
vendor-id: 910
usersync:
cookie-family-name: insticator
iframe:
url: https://usync.ingage.tech?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}}
support-cors: false
uid-macro: '$UID'
22 changes: 22 additions & 0 deletions src/main/resources/static/bidder-params/insticator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Insticator Adapter Params",
"description": "A schema which validates params accepted by Insticator",
"type": "object",
"properties": {
"adUnitId": {
"type": "string",
"description": "Ad Unit Id",
"minLength": 1
},
"publisherId": {
"type": "string",
"description": "Publisher Id",
"minLength": 1
}
},
"required": [
"adUnitId",
"publisherId"
]
}
Loading
Loading