Skip to content

Commit

Permalink
Add Axonix bidder (#1376)
Browse files Browse the repository at this point in the history
* Add Axonix bidder

* Add tests

* Refactoring
  • Loading branch information
And1sS authored and nickluck8 committed Aug 10, 2021
1 parent 7b8573e commit a790957
Show file tree
Hide file tree
Showing 12 changed files with 622 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/main/java/org/prebid/server/bidder/axonix/AxonixBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.prebid.server.bidder.axonix;

import com.fasterxml.jackson.core.type.TypeReference;
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.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.axonix.ExtImpAxonix;
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;

/**
* Axonix {@link Bidder} implementation.
*/
public class AxonixBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpAxonix>> AXONIX_EXT_TYPE_REFERENCE =
new TypeReference<ExtPrebid<?, ExtImpAxonix>>() {
};
public static final String URL_SUPPLY_ID_MACRO = "{{SupplyId}}";

private final JacksonMapper mapper;
private final String endpointUrl;

public AxonixBidder(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 ExtImpAxonix extImpAxonix;
try {
extImpAxonix = parseImpExt(request.getImp().get(0));
} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}

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

private ExtImpAxonix parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), AXONIX_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException("Imp.ext could not be parsed");
}
}

private String resolveEndpoint(String supplyId) {
return endpointUrl.replace(URL_SUPPLY_ID_MACRO, HttpUtil.encodeUrl(supplyId));
}

@Override
public Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.of(extractBids(bidRequest, bidResponse), Collections.emptyList());
} 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, getMediaType(bid.getImpid(), bidRequest.getImp()), bidResponse.getCur()))
.collect(Collectors.toList());
}

private static BidType getMediaType(String impId, List<Imp> imps) {
for (Imp imp : imps) {
if (impId.equals(imp.getId())) {
if (imp.getXNative() != null) {
return BidType.xNative;
} else if (imp.getVideo() != null) {
return BidType.video;
}
return BidType.banner;
}
}
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.axonix;

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

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

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

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

private static final String BIDDER_NAME = "axonix";

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

@Autowired
private JacksonMapper mapper;

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

@Bean("axonixConfigurationProperties")
@ConfigurationProperties("adapters.axonix")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps axonixBidderDeps() {
return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(configProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new AxonixBidder(config.getEndpoint(), mapper))
.assemble();
}
}

27 changes: 27 additions & 0 deletions src/main/resources/bidder-config/axonix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
adapters:
axonix:
enabled: false
endpoint: https://openrtb-us-east-1.axonix.com/supply/prebid-server/{{SupplyId}}
pbs-enforces-gdpr: true
pbs-enforces-ccpa: true
modifying-vast-xml-allowed: true
deprecated-names:
aliases: {}
meta-info:
maintainer-email: support.axonix@emodoinc.com
app-media-types:
- banner
- video
- native
site-media-types:
- banner
- video
- native
supported-vendors:
vendor-id: 678
usersync:
url:
redirect-url:
cookie-family-name: axonix
type: redirect
support-cors: false
14 changes: 14 additions & 0 deletions src/main/resources/static/bidder-params/axonix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Axonix Adapter Params",
"description": "A schema which validates params accepted by the Axonix adapter",
"type": "object",
"properties": {
"supplyId": {
"type": "string",
"minLength": 1,
"description": "Unique supply identifier"
}
},
"required": ["supplyId"]
}
Loading

0 comments on commit a790957

Please sign in to comment.