Skip to content
Open
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
175 changes: 175 additions & 0 deletions src/main/java/org/prebid/server/bidder/synapsehx/SynapseHXBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package org.prebid.server.bidder.synapsehx;

import com.fasterxml.jackson.core.type.TypeReference;
import com.iab.openrtb.request.BidRequest;
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.apache.http.client.utils.URIBuilder;
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.json.DecodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.synapsehx.ExtImpSynapseHX;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class SynapseHXBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpSynapseHX>> SYNAPSE_HX_EXT_TYPE_REFERENCE =
new TypeReference<>() { };
Comment on lines +37 to +38

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new TypeReference<>() {
};


private static final TypeReference<ExtPrebid<ExtBidPrebid, ?>> EXT_PREBID_TYPE_REFERENCE =
new TypeReference<>() { };
Comment on lines +40 to +41

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


private static final String OPENRTB_VERSION = "2.6";

private final String endpoint;
private final JacksonMapper mapper;

public SynapseHXBidder(String endpoint, JacksonMapper mapper) {
this.endpoint = HttpUtil.validateUrl(Objects.requireNonNull(endpoint));
this.mapper = Objects.requireNonNull(mapper);
}

@Override
public final Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) {
final List<BidderError> errors = new ArrayList<>();

final BidRequest filteredBidRequest = bidRequest.toBuilder()
.imp(bidRequest.getImp()
.stream()
.map(imp -> validateImp(imp, errors))
.filter(Objects::nonNull)
.toList())
.build();

if (filteredBidRequest.getImp().isEmpty()) {
return Result.withErrors(errors);
}

final Imp firstImp = filteredBidRequest.getImp().getFirst();
final String tenantId;

try {
tenantId = mapper.mapper()
.convertValue(firstImp.getExt(), SYNAPSE_HX_EXT_TYPE_REFERENCE)
.getBidder()
.getTenantId();
} catch (IllegalArgumentException e) {
return Result.withError(BidderError.badInput("Failed to parse bidder parameters"));
}
Comment on lines +72 to +79

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, extract to separate method.


final MultiMap headers = HttpUtil.headers();

headers.add(HttpUtil.X_OPENRTB_VERSION_HEADER, OPENRTB_VERSION);

final URIBuilder uriBuilder;
try {
uriBuilder = new URIBuilder(endpoint);
} catch (URISyntaxException e) {
return Result.withError(BidderError.badInput("Invalid endpoint URI"));
}
Comment on lines +85 to +90

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, extract to separate method.


return Result.of(List.of(BidderUtil.defaultRequest(filteredBidRequest,
headers, uriBuilder.addParameter("pid", tenantId).toString(), mapper)), errors);
Comment on lines +92 to +93

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then this will be simplified to:

Result.withValue(BidderUtil.defaultRequest(request, headers, mapper));

}

private static Imp validateImp(Imp imp, List<BidderError> errors) {
if (imp.getBanner() == null && imp.getVideo() == null) {
errors.add(BidderError.badInput(
"imp[%s]: Unsupported media type, bidder supports only banner and video".formatted(imp.getId())));
return null;
}
if (imp.getXNative() != null || imp.getAudio() != null) {
return imp.toBuilder().xNative(null).audio(null).build();
}
return imp;
Comment on lines +96 to +105

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is not present in go + we have auction.filter-imp-media-type that will do the same job.

}

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

private List<BidderBid> extractBids(BidResponse bidResponse, List<BidderError> bidderErrors) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
return bidsFromResponse(bidResponse, bidderErrors);
}

private List<BidderBid> bidsFromResponse(BidResponse bidResponse, List<BidderError> bidderErrors) {
return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> makeBidderBid(bid, bidResponse, bidderErrors))
.filter(Objects::nonNull)
.toList();
}

private BidderBid makeBidderBid(Bid bid, BidResponse bidResponse, List<BidderError> bidderErrors) {
final BidType bidType = getBidType(bid, bidderErrors);
if (bidType == null) {
return null;
}

return BidderBid.of(bid, bidType, bidResponse.getCur());
Comment on lines +138 to +143

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified to:

return bidType == null
     ? null
     : BidderBid.of(bid, bidType, bidResponse.getCur())

}

private BidType getBidType(Bid bid, List<BidderError> errors) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at ConsumableBidder as a general approach reference, and its getBidType

final Integer mType = bid.getMtype();
if (mType != null) {
return switch (mType) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
default -> {
errors.add(BidderError.badServerResponse("Unsupported media type: %d".formatted(mType)));
yield null;
}
};
Comment on lines +149 to +156

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract to separate method, smth like bidTypeFromMType

}

final BidType bidType = Optional.ofNullable(bid.getExt())
.map(ext -> mapper.mapper().convertValue(ext, EXT_PREBID_TYPE_REFERENCE))
.map(ExtPrebid::getPrebid)
.map(ExtBidPrebid::getType)
.orElse(null);

return switch (bidType) {
case banner -> BidType.banner;
case video -> BidType.video;
case null, default -> {
errors.add(BidderError.badServerResponse("Unsupported media type: %s".formatted(bidType)));
yield null;
}
};
Comment on lines +159 to +172

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please, extract to separate method.

}

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

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

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

@JsonProperty("tenantId")
String tenantId;
}
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.synapsehx.SynapseHXBidder;
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/synapsehx.yaml", factory = YamlPropertySourceFactory.class)
public class SynapseHXBidderConfiguration {

private static final String BIDDER_NAME = "synapsehx";

@Bean("synapsehxConfigurationProperties")
@ConfigurationProperties("adapters.synapsehx")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

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

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(synapsehxConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new SynapseHXBidder(config.getEndpoint(), mapper))
.assemble();
}
}
23 changes: 23 additions & 0 deletions src/main/resources/bidder-config/synapsehx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
adapters:
synapsehx:
endpoint: "https://rtb.hx.compasonline.com/pbs"
openrtb-version: 2.6
meta-info:
maintainer-email: prebid@compas-inc.com
app-media-types:
- banner
- video
site-media-types:
- banner
- video
vendor-id: 0
usersync:
cookie-family-name: synapsehx
redirect:
url: "https://sync.hx.compasonline.com/pbserver/image?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&ccpa={{us_privacy}}&gpp={{gpp}}&gpp_sid={{gpp_sid}}&redirect={{redirect_url}}"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove " from yaml urls.

uid-macro: "${VISITOR_ID}"
support-cors: false
iframe:
url: "https://sync.hx.compasonline.com/pbserver/iframe?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&ccpa={{us_privacy}}&gpp={{gpp}}&gpp_sid={{gpp_sid}}&redirect={{redirect_url}}"
uid-macro: "${VISITOR_ID}"
support-cors: false
14 changes: 14 additions & 0 deletions src/main/resources/static/bidder-params/synapsehx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Synapse HX Adapter Params",
"description": "A schema which validates params accepted by the Synapse HX adapter",
"type": "object",
"properties": {
"tenantId": {
"type": "string",
"description": "Synapse HX tenant identifier",
"minLength": 1
}
},
"required": ["tenantId"]
}
Loading