-
Notifications
You must be signed in to change notification settings - Fork 224
Adverxo: New adapter ported from Go #3705
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
Changes from 4 commits
ae9b2f2
50062ce
84a0490
61c230f
2be2f83
a333369
37ee752
c21a5aa
86b00f3
97c61ec
aab6750
6f0427d
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,218 @@ | ||
| 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 io.vertx.core.http.HttpMethod; | ||
| import org.apache.commons.collections4.CollectionUtils; | ||
| import org.apache.commons.lang3.ObjectUtils; | ||
| 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.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.HttpUtil; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| 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(imp, request); | ||
| final BidRequest outgoingRequest = createRequest(request, modifiedImp); | ||
|
|
||
| requests.add(createHttpRequest(outgoingRequest, endpoint, imp.getId())); | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } 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 extImpAdverxo) { | ||
| final String adUnitAsString = Optional.of(extImpAdverxo.getAdUnitId()) | ||
| .map(Object::toString) | ||
| .orElse(StringUtils.EMPTY); | ||
| final String authAsString = Optional.ofNullable(extImpAdverxo.getAuth()) | ||
| .map(Object::toString) | ||
| .orElse(StringUtils.EMPTY); | ||
|
|
||
| return endpointUrl | ||
| .replace(ADUNIT_MACROS_ENDPOINT, adUnitAsString) | ||
| .replace(AUTH_MACROS_ENDPOINT, authAsString); | ||
| } | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private Imp modifyImp(Imp imp, BidRequest request) { | ||
| final BigDecimal bidFloor = imp.getBidfloor(); | ||
| final String bidFloorCur = imp.getBidfloorcur(); | ||
|
|
||
| if (bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) > 0 | ||
| && StringUtils.isNotBlank(bidFloorCur) | ||
| && !StringUtils.equalsIgnoreCase(bidFloorCur, DEFAULT_BID_CURRENCY)) { | ||
|
|
||
| final BigDecimal convertedPrice = currencyConversionService.convertCurrency( | ||
| bidFloor, | ||
| request, | ||
| bidFloorCur, | ||
| DEFAULT_BID_CURRENCY | ||
| ); | ||
|
|
||
| return imp.toBuilder() | ||
| .bidfloor(convertedPrice) | ||
| .bidfloorcur(DEFAULT_BID_CURRENCY) | ||
| .build(); | ||
| } | ||
| return imp; | ||
| } | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private BidRequest createRequest(BidRequest originalRequest, Imp modifiedImp) { | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return originalRequest.toBuilder() | ||
| .imp(Collections.singletonList(modifiedImp)) | ||
| .build(); | ||
| } | ||
|
|
||
| private HttpRequest<BidRequest> createHttpRequest(BidRequest outgoingRequest, | ||
| String endpoint, | ||
| String impId) { | ||
| return HttpRequest.<BidRequest>builder() | ||
| .method(HttpMethod.POST) | ||
| .uri(endpoint) | ||
| .headers(HttpUtil.headers()) | ||
| .body(mapper.encodeToBytes(outgoingRequest)) | ||
| .impIds(Collections.singleton(impId)) | ||
| .payload(outgoingRequest) | ||
| .build(); | ||
| } | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public 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.withValues(extractBids(bidResponse, bidderErrors)); | ||
| } catch (DecodeException | PreBidException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| private List<BidderBid> extractBids(BidResponse bidResponse, | ||
| List<BidderError> bidderErrors) { | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| final String currency = bidResponse.getCur(); | ||
| final List<BidderBid> bidderBids = new ArrayList<>(); | ||
|
|
||
| for (SeatBid seatBid : bidResponse.getSeatbid()) { | ||
| if (CollectionUtils.isEmpty(seatBid.getBid())) { | ||
| continue; | ||
| } | ||
|
|
||
| for (Bid bid : seatBid.getBid()) { | ||
| final BidType bidType = getBidType(bid); | ||
| final String resolvedAdm = resolveAdmForBidType(bid, bidType); | ||
| final Bid processedBid = processBidMacros(bid, resolvedAdm); | ||
|
|
||
| bidderBids.add(BidderBid.of(processedBid, bidType, currency)); | ||
| } | ||
| } | ||
|
|
||
| return bidderBids; | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private BidType getBidType(Bid bid) { | ||
| final Integer markupType = ObjectUtils.defaultIfNull(bid.getMtype(), 0); | ||
|
|
||
| return switch (markupType) { | ||
| case 1 -> BidType.banner; | ||
| case 2 -> BidType.video; | ||
| case 4 -> BidType.xNative; | ||
| default -> throw new PreBidException( | ||
| "could not define media type for impression: " + bid.getImpid()); | ||
| }; | ||
| } | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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 Bid processBidMacros(Bid bid, String adm) { | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final String price = bid.getPrice() != null ? bid.getPrice().toPlainString() : "0"; | ||
|
|
||
| return bid.toBuilder() | ||
| .adm(replaceMacro(adm, price)) | ||
| .nurl(replaceMacro(bid.getNurl(), price)) | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .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") | ||
|
||
| public class ExtImpAdverxo { | ||
|
|
||
| @JsonProperty("adUnitId") | ||
| int adUnitId; | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @JsonProperty("auth") | ||
|
||
| 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) { | ||
|
|
||
|
Collaborator
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,27 @@ | ||
| adapters: | ||
| adverxo: | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| endpoint: "https://pbsadverxo.com/auction?adUnitId={{adUnitId}}&auth={{auth}}" | ||
| endpoint-compression: gzip | ||
| 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 | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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"] | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.