-
Notifications
You must be signed in to change notification settings - Fork 226
Add new bidder: Impactify #1553
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
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
9c6764f
Rename ext field properly
yevhenii-viktorov 7eabee7
Rename ext field properly
yevhenii-viktorov caa9c4b
Cleanup code, bid implementation
yevhenii-viktorov b91ca71
Add makeBids logic
yevhenii-viktorov 0870c61
Add makeBids logic
yevhenii-viktorov b69c2bc
Add some tests to Impactify
yevhenii-viktorov a614f69
Add test where imp contains correct curr values
yevhenii-viktorov 9f10523
Fix logic on currency conversion and add valid test case for it
yevhenii-viktorov 300e7db
Add test for currency conversion
yevhenii-viktorov 0bd8f64
Add TODO for myself
yevhenii-viktorov 598efb1
WIP
yevhenii-viktorov 7ee8976
Add IT tests, change some logic
yevhenii-viktorov fa517b5
Add json files for it tests
yevhenii-viktorov 9b55d51
Add json files for it tests
yevhenii-viktorov d5335d2
Remove test after refactoring
yevhenii-viktorov 02d0f37
Merge remote-tracking branch 'Prebid/master' into impactify
yevhenii-viktorov 546adba
New bidder: Impactify
yevhenii-viktorov b84a578
Merge remote-tracking branch 'Prebid/master' into impactify
yevhenii-viktorov 4a5ddc9
Fix line break in it test: ImpactifyTest
yevhenii-viktorov 5e8515b
Merge remote-tracking branch 'Prebid/impactify' into impactify
yevhenii-viktorov 5684652
Rearrange code for methods to be in correct order
yevhenii-viktorov 8e8b681
Remove blank lines
yevhenii-viktorov 884f805
Refactor tests
yevhenii-viktorov 3c64253
Fix code review comment issues
yevhenii-viktorov a7657ca
Remove blank line
yevhenii-viktorov 4998908
Remove final
yevhenii-viktorov d74a0da
Remove final
yevhenii-viktorov 916569d
Merge remote-tracking branch 'Prebid/master' into impactify
yevhenii-viktorov cf01e33
Reverted back and adapted JacksonMapper to new version
yevhenii-viktorov 47b926e
Fix minor cosmetic stuff
yevhenii-viktorov 2307d64
Fix CR comments
yevhenii-viktorov efc4cee
Merge remote-tracking branch 'Prebid/master' into impactify
yevhenii-viktorov 08f4b80
Change logic so makeBids fail on failing get BidType
yevhenii-viktorov e25162a
Another bunch of CR fixes
yevhenii-viktorov 84f2083
re-implement how new ext is created for Impactify logic, some CR fixes
yevhenii-viktorov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
201 changes: 201 additions & 0 deletions
201
src/main/java/org/prebid/server/bidder/impactify/ImpactifyBidder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| package org.prebid.server.bidder.impactify; | ||
|
|
||
| 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.request.Site; | ||
| import com.iab.openrtb.request.User; | ||
| import com.iab.openrtb.response.BidResponse; | ||
| import com.iab.openrtb.response.SeatBid; | ||
| import io.vertx.core.MultiMap; | ||
| import io.vertx.core.http.HttpMethod; | ||
| 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.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.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.impactify.ExtImpImpactify; | ||
| 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 ImpactifyBidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final TypeReference<ExtPrebid<?, ExtImpImpactify>> IMPACTIFY_EXT_TYPE_REFERENCE = | ||
| new TypeReference<ExtPrebid<?, ExtImpImpactify>>() { | ||
| }; | ||
| private static final String X_OPENRTB_VERSION = "2.5"; | ||
| private static final String BIDDER_CURRENCY = "USD"; | ||
|
|
||
| private final String endpointUrl; | ||
| private final JacksonMapper mapper; | ||
| private final CurrencyConversionService currencyConversionService; | ||
|
|
||
| public ImpactifyBidder(String endpointUrl, JacksonMapper mapper, CurrencyConversionService conversionService) { | ||
| this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); | ||
| this.mapper = Objects.requireNonNull(mapper); | ||
| this.currencyConversionService = Objects.requireNonNull(conversionService); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) { | ||
| final List<Imp> updatedImps = new ArrayList<>(); | ||
|
|
||
| for (Imp imp : request.getImp()) { | ||
| try { | ||
| final BigDecimal resolvedBidFloor = resolveBidFloor(request, imp); | ||
| updatedImps.add(updateImp(imp, resolvedBidFloor)); | ||
| } catch (PreBidException e) { | ||
| return Result.withError(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| final BidRequest updatedBidRequest = updateBidRequest(request, updatedImps); | ||
|
|
||
| return Result.withValue(HttpRequest.<BidRequest>builder() | ||
| .method(HttpMethod.POST) | ||
| .uri(endpointUrl) | ||
| .headers(constructHeaders(updatedBidRequest)) | ||
| .body(mapper.encodeToBytes(updatedBidRequest)) | ||
| .payload(updatedBidRequest) | ||
| .build()); | ||
| } | ||
|
|
||
| private BigDecimal resolveBidFloor(BidRequest request, Imp imp) { | ||
| return shouldConvertBidFloor(imp.getBidfloor(), imp.getBidfloorcur()) | ||
| ? convertBidFloorCurrency(imp.getBidfloor(), request, imp.getId(), imp.getBidfloorcur()) | ||
| : imp.getBidfloor(); | ||
| } | ||
|
|
||
| private static boolean shouldConvertBidFloor(BigDecimal bidFloor, String bidFloorCur) { | ||
| return BidderUtil.isValidPrice(bidFloor) && !StringUtils.equalsIgnoreCase(bidFloorCur, BIDDER_CURRENCY); | ||
| } | ||
|
|
||
| private BigDecimal convertBidFloorCurrency(BigDecimal bidFloor, | ||
| BidRequest bidRequest, | ||
| String impId, | ||
| String bidFloorCur) { | ||
| try { | ||
| return currencyConversionService | ||
| .convertCurrency(bidFloor, bidRequest, bidFloorCur, BIDDER_CURRENCY); | ||
| } catch (PreBidException e) { | ||
| throw new PreBidException(String.format( | ||
| "Unable to convert provided bid floor currency from %s to %s for imp `%s`", | ||
| bidFloorCur, BIDDER_CURRENCY, impId)); | ||
| } | ||
| } | ||
|
|
||
| private Imp updateImp(Imp imp, BigDecimal bidFloor) { | ||
| return imp.toBuilder() | ||
| .bidfloorcur(BIDDER_CURRENCY) | ||
| .bidfloor(bidFloor) | ||
| .ext(mapper.mapper().createObjectNode() | ||
| .set("impactify", mapper.mapper().valueToTree(parseExtImp(imp)))) | ||
| .build(); | ||
| } | ||
|
|
||
| private ExtImpImpactify parseExtImp(Imp imp) { | ||
| try { | ||
| return mapper.mapper() | ||
| .convertValue(imp.getExt(), IMPACTIFY_EXT_TYPE_REFERENCE) | ||
| .getBidder(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException(String.format("Unable to decode the impression ext for id: %s", imp.getId())); | ||
| } | ||
| } | ||
|
|
||
| private static BidRequest updateBidRequest(BidRequest request, List<Imp> updatedImps) { | ||
| return request.toBuilder() | ||
| .imp(updatedImps) | ||
| .cur(Collections.singletonList(BIDDER_CURRENCY)) | ||
| .build(); | ||
| } | ||
|
|
||
| private static MultiMap constructHeaders(BidRequest bidRequest) { | ||
| final MultiMap headers = HttpUtil.headers().set(HttpUtil.X_OPENRTB_VERSION_HEADER, X_OPENRTB_VERSION); | ||
|
|
||
| final Device device = bidRequest.getDevice(); | ||
| if (device != null) { | ||
| HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.USER_AGENT_HEADER, device.getUa()); | ||
|
|
||
| final String deviceIp = device.getIp(); | ||
| if (deviceIp != null) { | ||
| HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER, deviceIp); | ||
| } else { | ||
| HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER, device.getIpv6()); | ||
| } | ||
| } | ||
|
|
||
| final Site site = bidRequest.getSite(); | ||
| if (site != null) { | ||
| HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.REFERER_HEADER, site.getPage()); | ||
| } | ||
|
|
||
| final User user = bidRequest.getUser(); | ||
| final String userUid = user != null ? user.getBuyeruid() : null; | ||
| if (StringUtils.isNotBlank(userUid)) { | ||
| headers.set(HttpUtil.COOKIE_HEADER, "uids=" + userUid); | ||
| } | ||
|
|
||
| return headers; | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
| try { | ||
| final List<BidderError> errors = new ArrayList<>(); | ||
| final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
| return Result.of(extractBids(bidRequest, bidResponse), errors); | ||
| } catch (DecodeException | PreBidException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| private List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidResponse) { | ||
| if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { | ||
| return Collections.emptyList(); | ||
| } | ||
| return bidsFromResponse(bidResponse, bidRequest); | ||
| } | ||
|
|
||
| private List<BidderBid> bidsFromResponse(BidResponse bidResponse, BidRequest bidRequest) { | ||
| return bidResponse.getSeatbid().stream() | ||
| .filter(Objects::nonNull) | ||
| .map(SeatBid::getBid) | ||
| .flatMap(Collection::stream) | ||
| .map(bid -> BidderBid.of(bid, getBidType(bid.getImpid(), bidRequest.getImp()), bidResponse.getCur())) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static BidType getBidType(String impId, List<Imp> imps) { | ||
| for (Imp imp : imps) { | ||
| if (imp.getId().equals(impId)) { | ||
| if (imp.getBanner() != null) { | ||
| return BidType.banner; | ||
| } | ||
| if (imp.getVideo() != null) { | ||
| return BidType.video; | ||
| } | ||
| } | ||
| } | ||
| throw new PreBidException( | ||
| String.format("Failed to find a supported media type impression with ID: '%s'", impId)); | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
src/main/java/org/prebid/server/proto/openrtb/ext/request/impactify/ExtImpImpactify.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package org.prebid.server.proto.openrtb.ext.request.impactify; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class ExtImpImpactify { | ||
|
|
||
| @JsonProperty("appId") | ||
| String appId; | ||
|
|
||
| String format; | ||
|
|
||
| String style; | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/main/java/org/prebid/server/spring/config/bidder/ImpactifyConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.prebid.server.spring.config.bidder; | ||
|
|
||
| import org.prebid.server.bidder.BidderDeps; | ||
| import org.prebid.server.bidder.impactify.ImpactifyBidder; | ||
| 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 javax.validation.constraints.NotBlank; | ||
|
|
||
| @Configuration | ||
| @PropertySource(value = "classpath:/bidder-config/impactify.yaml", factory = YamlPropertySourceFactory.class) | ||
| public class ImpactifyConfiguration { | ||
|
|
||
| private static final String BIDDER_NAME = "impactify"; | ||
|
|
||
| @Bean("impactifyConfigurationProperties") | ||
| @ConfigurationProperties("adapters.impactify") | ||
| BidderConfigurationProperties configurationProperties() { | ||
| return new BidderConfigurationProperties(); | ||
| } | ||
|
|
||
| @Bean | ||
| BidderDeps impactifyBidderDeps(BidderConfigurationProperties impactifyConfigurationProperties, | ||
| @NotBlank @Value("${external-url}") String externalUrl, | ||
| CurrencyConversionService currencyConversionService, | ||
| JacksonMapper mapper) { | ||
|
|
||
| return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
| .withConfig(impactifyConfigurationProperties) | ||
| .usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
| .bidderCreator(config -> new ImpactifyBidder(config.getEndpoint(), mapper, currencyConversionService)) | ||
| .assemble(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| adapters: | ||
| impactify: | ||
| endpoint: https://sonic.impactify.media/bidder | ||
| meta-info: | ||
| maintainer-email: support@impactify.io | ||
yevhenii-viktorov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| app-media-types: | ||
| site-media-types: | ||
| - banner | ||
| - video | ||
| supported-vendors: | ||
| vendor-id: 606 | ||
| usersync: | ||
| url: https://sonic.impactify.media/static/cookie_sync.html?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redirect_url= | ||
| redirect-url: /setuid?bidder=impactify&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&uid={IMPACTIFY_UID} | ||
| cookie-family-name: impactify | ||
| type: iframe | ||
| support-cors: false | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Impactify Adapter Params", | ||
| "description": "A schema which validates params accepted by the Impactify adapter", | ||
| "type": "object", | ||
| "properties": { | ||
| "appId": { | ||
| "type": "string", | ||
| "description": "The appId of your website", | ||
| "minLength": 1 | ||
| }, | ||
| "format": { | ||
| "type": "string", | ||
| "description": "The format of the ad", | ||
| "minLength": 1 | ||
| }, | ||
| "style": { | ||
| "type": "string", | ||
| "description": "The style of the ad", | ||
| "minLength": 1 | ||
| } | ||
| }, | ||
| "required": [ | ||
| "appId", | ||
| "format", | ||
| "style" | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.