-
Notifications
You must be signed in to change notification settings - Fork 193
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
Adverxo: New adapter ported from Go #3705
base: master
Are you sure you want to change the base?
Changes from 8 commits
ae9b2f2
50062ce
84a0490
61c230f
2be2f83
a333369
37ee752
c21a5aa
86b00f3
97c61ec
aab6750
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package org.prebid.server.bidder.adverxo; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
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 org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
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.adverxo.ExtImpAdverxo; | ||
import org.prebid.server.proto.openrtb.ext.response.BidType; | ||
import org.prebid.server.util.BidderUtil; | ||
import org.prebid.server.util.HttpUtil; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class AdverxoBidder implements Bidder<BidRequest> { | ||
|
||
private static final TypeReference<ExtPrebid<?, ExtImpAdverxo>> ADVERXO_EXT_TYPE_REFERENCE = | ||
new TypeReference<>() { | ||
}; | ||
private static final String DEFAULT_BID_CURRENCY = "USD"; | ||
private static final String ADUNIT_MACROS_ENDPOINT = "{{adUnitId}}"; | ||
private static final String AUTH_MACROS_ENDPOINT = "{{auth}}"; | ||
private static final String PRICE_MACRO = "${AUCTION_PRICE}"; | ||
|
||
private final String endpointUrl; | ||
private final JacksonMapper mapper; | ||
private final CurrencyConversionService currencyConversionService; | ||
|
||
public AdverxoBidder(String endpointUrl, JacksonMapper mapper, | ||
CurrencyConversionService currencyConversionService) { | ||
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); | ||
this.mapper = mapper; | ||
this.currencyConversionService = currencyConversionService; | ||
} | ||
|
||
@Override | ||
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) { | ||
final List<BidderError> errors = new ArrayList<>(); | ||
final List<HttpRequest<BidRequest>> requests = new ArrayList<>(); | ||
|
||
for (Imp imp : request.getImp()) { | ||
try { | ||
final ExtImpAdverxo extImp = parseImpExt(imp); | ||
final String endpoint = resolveEndpoint(extImp); | ||
final Imp modifiedImp = modifyImp(request, imp); | ||
final BidRequest outgoingRequest = createRequest(request, modifiedImp); | ||
|
||
requests.add(BidderUtil.defaultRequest(outgoingRequest, endpoint, mapper)); | ||
} catch (PreBidException e) { | ||
errors.add(BidderError.badInput(e.getMessage())); | ||
} | ||
} | ||
|
||
return Result.of(requests, errors); | ||
} | ||
|
||
private ExtImpAdverxo parseImpExt(Imp imp) { | ||
try { | ||
return mapper.mapper().convertValue(imp.getExt(), ADVERXO_EXT_TYPE_REFERENCE).getBidder(); | ||
} catch (IllegalArgumentException e) { | ||
throw new PreBidException("Error parsing ext.imp.bidder: " + e.getMessage()); | ||
} | ||
} | ||
|
||
private String resolveEndpoint(ExtImpAdverxo extImp) { | ||
return endpointUrl | ||
.replace(ADUNIT_MACROS_ENDPOINT, | ||
extImp.getAdUnitId() == null ? StringUtils.EMPTY : String.valueOf(extImp.getAdUnitId())) | ||
.replace(AUTH_MACROS_ENDPOINT, HttpUtil.encodeUrl(StringUtils.defaultString(extImp.getAuth()))); | ||
} | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private Imp modifyImp(BidRequest bidRequest, Imp imp) { | ||
final Price resolvedBidFloor = resolveBidFloor(imp, bidRequest); | ||
|
||
return imp.toBuilder() | ||
.bidfloor(resolvedBidFloor.getValue()) | ||
.bidfloorcur(resolvedBidFloor.getCurrency()) | ||
.build(); | ||
} | ||
|
||
private Price resolveBidFloor(Imp imp, BidRequest bidRequest) { | ||
final Price initialBidFloorPrice = Price.of(imp.getBidfloorcur(), imp.getBidfloor()); | ||
return BidderUtil.shouldConvertBidFloor(initialBidFloorPrice, DEFAULT_BID_CURRENCY) | ||
? convertBidFloor(initialBidFloorPrice, bidRequest) | ||
: initialBidFloorPrice; | ||
} | ||
|
||
private Price convertBidFloor(Price bidFloorPrice, BidRequest bidRequest) { | ||
final BigDecimal convertedPrice = currencyConversionService.convertCurrency( | ||
bidFloorPrice.getValue(), | ||
bidRequest, | ||
bidFloorPrice.getCurrency(), | ||
DEFAULT_BID_CURRENCY); | ||
|
||
return Price.of(DEFAULT_BID_CURRENCY, convertedPrice); | ||
} | ||
|
||
private static BidRequest createRequest(BidRequest originalRequest, Imp modifiedImp) { | ||
return originalRequest.toBuilder() | ||
.imp(Collections.singletonList(modifiedImp)) | ||
.build(); | ||
} | ||
|
||
@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 List<BidderBid> extractBids(BidResponse bidResponse) { | ||
|
||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 -> makeBid(bid, bidResponse.getCur())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private BidderBid makeBid(Bid bid, String currency) { | ||
final BidType bidType = getBidType(bid.getMtype()); | ||
final String resolvedAdm = resolveAdmForBidType(bid, bidType); | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final Bid processedBid = processBidMacros(bid, resolvedAdm); | ||
|
||
return BidderBid.of(processedBid, bidType, currency); | ||
} | ||
|
||
private static BidType getBidType(Integer mType) { | ||
return switch (mType) { | ||
case 1 -> BidType.banner; | ||
case 2 -> BidType.video; | ||
case 4 -> BidType.xNative; | ||
case null, default -> throw new PreBidException("Unsupported mType " + mType); | ||
}; | ||
} | ||
|
||
private String resolveAdmForBidType(Bid bid, BidType bidType) { | ||
if (bidType != BidType.xNative) { | ||
return bid.getAdm(); | ||
} | ||
|
||
try { | ||
final JsonNode admNode = mapper.mapper().readTree(bid.getAdm()); | ||
final JsonNode nativeNode = admNode.get("native"); | ||
return nativeNode != null ? nativeNode.toString() : bid.getAdm(); | ||
} catch (JsonProcessingException e) { | ||
throw new PreBidException("Error parsing native ADM: " + e.getMessage()); | ||
} | ||
} | ||
|
||
private static Bid processBidMacros(Bid bid, String adm) { | ||
final String price = bid.getPrice() != null ? bid.getPrice().toPlainString() : "0"; | ||
|
||
return bid.toBuilder() | ||
.adm(replaceMacro(adm, price)) | ||
.build(); | ||
} | ||
|
||
private static String replaceMacro(String input, String value) { | ||
return input != null ? input.replace(PRICE_MACRO, value) : null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.prebid.server.proto.openrtb.ext.request.adverxo; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Value; | ||
|
||
@Value | ||
@AllArgsConstructor(staticName = "of") | ||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^^ |
||
public class ExtImpAdverxo { | ||
|
||
@JsonProperty("adUnitId") | ||
Integer adUnitId; | ||
@JsonProperty("auth") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for |
||
String auth; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.prebid.server.spring.config.bidder; | ||
|
||
import org.prebid.server.bidder.BidderDeps; | ||
import org.prebid.server.bidder.adverxo.AdverxoBidder; | ||
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/adverxo.yaml", factory = YamlPropertySourceFactory.class) | ||
public class AdverxoBidderConfiguration { | ||
|
||
private static final String BIDDER_NAME = "adverxo"; | ||
|
||
@Bean("adverxoConfigurationProperties") | ||
@ConfigurationProperties("adapters.adverxo") | ||
BidderConfigurationProperties configurationProperties() { | ||
return new BidderConfigurationProperties(); | ||
} | ||
|
||
@Bean | ||
BidderDeps adverxoBidderDeps(BidderConfigurationProperties adverxoConfigurationProperties, | ||
@NotBlank @Value("${external-url}") String externalUrl, | ||
JacksonMapper mapper, CurrencyConversionService currencyConversionService) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Place each parameter on new different line |
||
return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
.withConfig(adverxoConfigurationProperties) | ||
.usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
.bidderCreator(config -> new AdverxoBidder(config.getEndpoint(), mapper, currencyConversionService)) | ||
.assemble(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
adapters: | ||
adverxo: | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
endpoint: https://pbsadverxo.com/auction?adUnitId={{adUnitId}}&auth={{auth}} | ||
endpoint-compression: gzip | ||
aliases: | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
adport: | ||
enabled: false | ||
endpoint: https://adport.pbsadverxo.com/auction?id={{adUnitId}}&auth={{auth}} | ||
userSync: | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
enabled: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
cookie-family-name: adverxo | ||
iframe: | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
url: https://adport.pbsadverxo.com/usync?type=iframe&gdpr={{gdpr}}&consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}} | ||
userMacro: '$UID' | ||
support-cors: false | ||
redirect: | ||
url: https://adport.pbsadverxo.com/usync?type=image&gdpr={{gdpr}}&consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}} | ||
userMacro: '$UID' | ||
support-cors: false | ||
bidsmind: | ||
enabled: false | ||
endpoint: https://bidsmind.pbsadverxo.com/auction?id={{adUnitId}}&auth={{auth}} | ||
userSync: | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
enabled: true | ||
cookie-family-name: adverxo | ||
iframe: | ||
url: https://bidsmind.pbsadverxo.com/usync?type=iframe&gdpr={{gdpr}}&consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}} | ||
userMacro: '$UID' | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
support-cors: false | ||
redirect: | ||
url: https://bidsmind.pbsadverxo.com/usync?type=image&gdpr={{gdpr}}&consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}} | ||
userMacro: '$UID' | ||
support-cors: false | ||
mobupps: | ||
enabled: false | ||
endpoint: https://mobupps.pbsadverxo.com/auction?id={{adUnitId}}&auth={{auth}} | ||
userSync: | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
enabled: true | ||
cookie-family-name: adverxo | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
iframe: | ||
url: https://mobupps.pbsadverxo.com/usync?type=iframe&gdpr={{gdpr}}&consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}} | ||
userMacro: '$UID' | ||
support-cors: false | ||
redirect: | ||
url: https://mobupps.pbsadverxo.com/usync?type=image&gdpr={{gdpr}}&consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}} | ||
userMacro: '$UID' | ||
support-cors: false | ||
meta-info: | ||
maintainer-email: developer@adverxo.com | ||
app-media-types: | ||
- banner | ||
- native | ||
- video | ||
site-media-types: | ||
- banner | ||
- native | ||
- video | ||
supported-vendors: | ||
vendor-id: 0 | ||
userSync: | ||
cookie-family-name: adverxo | ||
iframe: | ||
url: https://pbsadverxo.com/usync?type=iframe&gdpr={{gdpr}}&consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}} | ||
support-cors: false | ||
userMacro: '$UID' | ||
redirect: | ||
url: https://pbsadverxo.com/usync?type=image&gdpr={{gdpr}}&consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect={{redirect_url}} | ||
support-cors: false | ||
userMacro: '$UID' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "Adverxo Adapter Params", | ||
"description": "A schema which validates params accepted by the Adverxo adapter", | ||
"type": "object", | ||
"properties": { | ||
"adUnitId": { | ||
"type": "integer", | ||
"minimum": 1, | ||
"description": "Unique identifier for the ad unit in Adverxo platform." | ||
}, | ||
"auth": { | ||
"type": "string", | ||
"minLength": 6, | ||
"description": "Authentication token provided by Adverxo platform for the AdUnit." | ||
} | ||
}, | ||
"required": ["adUnitId", "auth"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last nitpick and we are good to go: