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 Aceex adapter #1477

Merged
merged 9 commits into from
Sep 27, 2021
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
124 changes: 124 additions & 0 deletions src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.prebid.server.bidder.aceex;

import com.fasterxml.jackson.core.type.TypeReference;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Device;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpMethod;
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.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.aceex.ExtImpAceex;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.HttpUtil;
import org.prebid.server.util.ObjectUtil;

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

public class AceexBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpAceex>> ACEEX_EXT_TYPE_REFERENCE =
new TypeReference<ExtPrebid<?, ExtImpAceex>>() {
};
private static final String ACCOUNT_ID_MACRO = "{{AccountId}}";
private static final String X_OPENRTB_VERSION = "2.5";

private final String endpointUrl;
private final JacksonMapper mapper;

public AceexBidder(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 Imp firstImp = request.getImp().get(0);
final ExtImpAceex extImpAceex;

try {
extImpAceex = mapper.mapper().convertValue(firstImp.getExt(), ACEEX_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
return Result.withError(BidderError.badInput("Ext.bidder not provided"));
And1sS marked this conversation as resolved.
Show resolved Hide resolved
}

return Result.withValue(HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(resolveEndpoint(extImpAceex.getAccountId()))
.headers(constructHeaders(request))
.body(mapper.encode(request))
.payload(request)
.build());
}

private String resolveEndpoint(String accountId) {
return endpointUrl.replace(ACCOUNT_ID_MACRO, HttpUtil.encodeUrl(accountId));
}

private static MultiMap constructHeaders(BidRequest bidRequest) {
And1sS marked this conversation as resolved.
Show resolved Hide resolved
final Device device = bidRequest.getDevice();
MultiMap headers = HttpUtil.headers();

headers.set(HttpUtil.X_OPENRTB_VERSION_HEADER, X_OPENRTB_VERSION);
HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.USER_AGENT_HEADER,
ObjectUtil.getIfNotNull(device, Device::getUa));
HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER,
ObjectUtil.getIfNotNull(device, Device::getIpv6));
HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER,
ObjectUtil.getIfNotNull(device, Device::getIp));

return headers;
}

@Override
public final Result<List<BidderBid>> makeBids(HttpCall<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 e) {
return Result.withError(BidderError.badServerResponse("Bad Server Response"));
} catch (PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidResponse) {
final List<SeatBid> seatBids = ObjectUtil.getIfNotNull(bidResponse, BidResponse::getSeatbid);
final SeatBid firstSeatBid = CollectionUtils.isNotEmpty(seatBids) ? seatBids.get(0) : null;
if (firstSeatBid == null) {
throw new PreBidException("Empty SeatBid array");
}

return CollectionUtils.emptyIfNull(firstSeatBid.getBid()).stream()
.filter(Objects::nonNull)
.map(bid -> BidderBid.of(bid, getBidMediaType(bid.getImpid(), bidRequest.getImp()),
bidResponse.getCur()))
.collect(Collectors.toList());
}

private static BidType getBidMediaType(String impId, List<Imp> imps) {
for (Imp imp : imps) {
if (imp.getId().equals(impId)) {
if (imp.getVideo() != null) {
return BidType.video;
} else if (imp.getXNative() != null) {
return BidType.xNative;
}
}
}
return BidType.banner;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.prebid.server.proto.openrtb.ext.request.aceex;

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

@Value
@AllArgsConstructor(staticName = "of")
public class ExtImpAceex {

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

import org.prebid.server.bidder.BidderDeps;
import org.prebid.server.bidder.aceex.AceexBidder;
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.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/aceex.yaml", factory = YamlPropertySourceFactory.class)
public class AceexConfiguration {

private static final String BIDDER_NAME = "aceex";

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

@Autowired
private JacksonMapper mapper;

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

@Bean("aceexConfigurationProperties")
@ConfigurationProperties("adapters.aceex")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps aceexBidderDeps() {
return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(configProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new AceexBidder(config.getEndpoint(), mapper))
.assemble();
}
}
21 changes: 21 additions & 0 deletions src/main/resources/bidder-config/aceex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
adapters:
aceex:
endpoint: http://bl-us.aceex.io/?uqhash={{AccountId}}
meta-info:
maintainer-email: tech@aceex.io
app-media-types:
- banner
- video
- native
site-media-types:
- banner
- video
- native
supported-vendors:
vendor-id: 0
usersync:
url:
redirect-url:
cookie-family-name: aceex
type: redirect
support-cors: false
14 changes: 14 additions & 0 deletions src/main/resources/static/bidder-params/aceex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Aceex Adapter Params",
"description": "A schema which validates params accepted by the Aceex adapter",
"type": "object",
"properties": {
"accountid": {
"type": "string",
"description": "Account id",
"minLength": 1
}
},
"required": ["accountid"]
}
Loading