Skip to content

Commit

Permalink
Escalax Bidder
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoxaAntoxic committed Sep 6, 2024
1 parent 65960a5 commit 383ae04
Show file tree
Hide file tree
Showing 19 changed files with 685 additions and 7 deletions.
140 changes: 140 additions & 0 deletions src/main/java/org/prebid/server/bidder/escalax/EscalaxBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package org.prebid.server.bidder.escalax;

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.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.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.escalax.ExtImpEscalax;
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.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class EscalaxBidder implements Bidder<BidRequest> {

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

private static final String X_OPENRTB_VERSION = "2.5";

private final String endpointUrl;
private final JacksonMapper mapper;

public EscalaxBidder(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().getFirst();
final ExtImpEscalax extImp;
try {
extImp = parseImpExt(firstImp);
} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}

return Result.withValue(makeHttpRequest(createRequest(request), extImp));
}

private static BidRequest createRequest(BidRequest request) {
return request.toBuilder().imp(prepareFirstImp(request.getImp())).build();
}

private static List<Imp> prepareFirstImp(List<Imp> imps) {
final Imp firstImp = imps.getFirst();
final List<Imp> updatedImps = new ArrayList<>(imps);
updatedImps.set(0, firstImp.toBuilder().ext(null).build());

return updatedImps;
}

private HttpRequest<BidRequest> makeHttpRequest(BidRequest bidRequest, ExtImpEscalax extImp) {
return BidderUtil.defaultRequest(bidRequest, makeHeaders(bidRequest.getDevice()), makeUrl(extImp), mapper);
}

private String makeUrl(ExtImpEscalax extImp) {
return endpointUrl
.replace("{{AccountID}}", extImp.getAccountId())
.replace("{{SourceId}}", extImp.getSourceId());
}

private MultiMap makeHeaders(Device device) {
final 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;
}

private ExtImpEscalax parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException("Error parsing escalaxExt - " + e.getMessage());
}
}

@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())) {
throw new PreBidException("Empty SeatBid array");
}

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()))
.toList();
}

private static BidType getBidType(Bid bid) {
final Integer mtype = bid.getMtype();
return switch (mtype) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
case 4 -> BidType.xNative;
case null, default -> throw new PreBidException("unsupported MType " + mtype);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.prebid.server.proto.openrtb.ext.request.escalax;

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

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

@JsonProperty("sourceId")
String sourceId;

@JsonProperty("accountId")
String accountId;
}
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.escalax.EscalaxBidder;
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/escalax.yaml", factory = YamlPropertySourceFactory.class)
public class EscalaxConfiguration {

private static final String BIDDER_NAME = "escalax";

@Bean("escalaxConfigurationProperties")
@ConfigurationProperties("adapters.escalax")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

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

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(escalaxConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new EscalaxBidder(config.getEndpoint(), mapper))
.assemble();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/bidder-config/aax.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
adapters:
aax:
endpoint: https://prebid.aaxads.com/rtb/pb/aax-prebid?src={{PREBID_SERVER_ENDPOINT}}
modifyingVastXmlAllowed: true
modifying-vast-xml-allowed: true
meta-info:
maintainer-email: [email protected]
app-media-types:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/bidder-config/aidem.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
adapters:
aidem:
endpoint: https://zero.aidemsrv.com/ortb/v2.6/bid/request?billing_id={{PublisherId}}
modifyingVastXmlAllowed: true
modifying-vast-xml-allowed: true
meta-info:
maintainer-email: [email protected]
app-media-types:
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/bidder-config/escalax.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
adapters:
escalax:
endpoint: http://bidder_us.escalax.io/?partner={{.SourceId}}&token={{.AccountID}}&type=pbs
modifying-vast-xml-allowed: true
endpoint-compression: gzip
meta-info:
maintainer-email: [email protected]
app-media-types:
- banner
- video
- native
site-media-types:
- banner
- video
- native
supported-vendors:
vendor-id: 0
2 changes: 1 addition & 1 deletion src/main/resources/bidder-config/freewheelssp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ adapters:
freewheelssp:
endpoint: https://ads.stickyadstv.com/openrtb/dsp
ortb-version: "2.6"
modifyingVastXmlAllowed: true
modifying-vast-xml-allowed: true
meta-info:
maintainer-email: [email protected]
app-media-types:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/bidder-config/kargo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ adapters:
endpoint: https://krk.kargo.com/api/v1/openrtb
ortb-version: "2.6"
endpoint-compression: gzip
modifyingVastXmlAllowed: true
modifying-vast-xml-allowed: true
meta-info:
maintainer-email: [email protected]
app-media-types:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/bidder-config/sovrn.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
adapters:
sovrn:
endpoint: http://ap.lijit.com/rtb/bid?src=prebid_server
modifyingVastXmlAllowed: true
modifying-vast-xml-allowed: true
meta-info:
maintainer-email: [email protected]
app-media-types:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/bidder-config/sovrnXsp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ adapters:
sovrnXsp:
endpoint: http://xsp.lijit.com/json/rtb/prebid/server
endpoint-compression: gzip
modifyingVastXmlAllowed: true
modifying-vast-xml-allowed: true
meta-info:
maintainer-email: [email protected]
app-media-types:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/bidder-config/trafficgate.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
adapters:
trafficgate:
endpoint: http://{{subdomain}}.bc-plugin.com/?c=o&m=rtb
modifyingVastXmlAllowed: true
modifying-vast-xml-allowed: true
meta-info:
maintainer-email: "[email protected]"
app-media-types:
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/static/bidder-params/escalax.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Escalax Adapter Params",
"description": "A schema which validates params accepted by the Escalax adapter",
"type": "object",
"properties": {
"accountId": {
"type": "string",
"description": "Account id",
"minLength": 1
},
"sourceId": {
"type": "string",
"description": "Source id",
"minLength": 1
}
},
"required": [
"accountId",
"sourceId"
]
}
Loading

0 comments on commit 383ae04

Please sign in to comment.