-
Notifications
You must be signed in to change notification settings - Fork 224
New Adapter: Clydo #4299
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
base: master
Are you sure you want to change the base?
New Adapter: Clydo #4299
Changes from all commits
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,186 @@ | ||
| package org.prebid.server.bidder.clydo; | ||
|
|
||
| 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.netty.handler.codec.http.HttpHeaderValues; | ||
| import io.vertx.core.MultiMap; | ||
| 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.clydo.ExtImpClydo; | ||
| 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.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ClydoBidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final TypeReference<ExtPrebid<?, ExtImpClydo>> CLYDO_EXT_TYPE_REFERENCE = | ||
| new TypeReference<>() { | ||
| }; | ||
|
|
||
| private static final String REGION_MACRO = "{{Region}}"; | ||
| private static final String PARTNER_ID_MACRO = "{{PartnerId}}"; | ||
| private static final String DEFAULT_REGION = "us"; | ||
| private static final String X_OPENRTB_VERSION = "2.5"; | ||
|
|
||
| private final String endpointUrl; | ||
| private final JacksonMapper mapper; | ||
|
|
||
| public ClydoBidder(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 List<BidderError> errors = new ArrayList<>(); | ||
| final List<HttpRequest<BidRequest>> httpRequests = new ArrayList<>(); | ||
|
|
||
| for (Imp imp : request.getImp()) { | ||
| try { | ||
| final ExtImpClydo extImpClydo = parseExtImp(imp); | ||
| final HttpRequest<BidRequest> httpRequest = makeHttpRequest(request, imp, extImpClydo); | ||
| httpRequests.add(httpRequest); | ||
| } catch (PreBidException e) { | ||
| errors.add(BidderError.badInput(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| if (httpRequests.isEmpty()) { | ||
| return Result.withError(BidderError.badInput("found no valid impressions")); | ||
| } | ||
|
|
||
| return Result.of(httpRequests, errors); | ||
| } | ||
|
|
||
| private ExtImpClydo parseExtImp(Imp imp) { | ||
| try { | ||
| return mapper.mapper().convertValue(imp.getExt(), CLYDO_EXT_TYPE_REFERENCE).getBidder(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException("Cannot deserialize ExtImpClydo: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private HttpRequest<BidRequest> makeHttpRequest(BidRequest request, Imp imp, ExtImpClydo extImpClydo) { | ||
| final BidRequest outgoingRequest = request.toBuilder().imp(List.of(imp)).build(); | ||
|
|
||
| return BidderUtil.defaultRequest( | ||
| outgoingRequest, | ||
| constructHeaders(request), | ||
| resolveUrl(endpointUrl, extImpClydo), mapper); | ||
| } | ||
|
|
||
| private static MultiMap constructHeaders(BidRequest bidRequest) { | ||
| final Device device = bidRequest.getDevice(); | ||
| final MultiMap headers = HttpUtil.headers(); | ||
|
|
||
| headers.set(HttpUtil.X_OPENRTB_VERSION_HEADER, X_OPENRTB_VERSION); | ||
| headers.set(HttpUtil.ACCEPT_HEADER, HttpHeaderValues.APPLICATION_JSON); | ||
| headers.set(HttpUtil.CONTENT_TYPE_HEADER, HttpUtil.APPLICATION_JSON_CONTENT_TYPE); | ||
| 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)); | ||
| HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.USER_AGENT_HEADER, | ||
| ObjectUtil.getIfNotNull(device, Device::getUa)); | ||
|
|
||
| return headers; | ||
| } | ||
|
|
||
| private static String resolveUrl(String endpoint, ExtImpClydo extImp) { | ||
| return endpoint | ||
| .replace(REGION_MACRO, getRegionInfo(extImp)) | ||
| .replace(PARTNER_ID_MACRO, HttpUtil.encodeUrl(extImp.getPartnerId())); | ||
| } | ||
|
|
||
| private static String getRegionInfo(ExtImpClydo extImp) { | ||
| final String region = extImp.getRegion(); | ||
|
|
||
| return switch (region) { | ||
| case "us", "usw", "eu", "apac" -> region; | ||
| case null, default -> DEFAULT_REGION; | ||
| }; | ||
przemkaczmarek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) { | ||
| try { | ||
| final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); | ||
| final List<BidderBid> bidderBids = extractBids(bidRequest, bidResponse); | ||
| return Result.withValues(bidderBids); | ||
| } catch (DecodeException | PreBidException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
przemkaczmarek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidResponse) { | ||
| if (bidResponse == null || bidResponse.getSeatbid() == null || bidResponse.getSeatbid().isEmpty()) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| final Map<String, BidType> impIdToBidTypeMap = buildImpIdToBidTypeMap(bidRequest); | ||
|
|
||
| return bidResponse.getSeatbid().stream() | ||
| .filter(Objects::nonNull) | ||
| .map(SeatBid::getBid) | ||
| .filter(Objects::nonNull) | ||
| .flatMap(List::stream) | ||
| .filter(Objects::nonNull) | ||
| .map(bid -> BidderBid.of(bid, resolveBidType(bid, impIdToBidTypeMap), bidResponse.getCur())) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static Map<String, BidType> buildImpIdToBidTypeMap(BidRequest bidRequest) { | ||
| final Map<String, BidType> impIdToBidTypeMap = new HashMap<>(); | ||
| for (Imp imp : bidRequest.getImp()) { | ||
| final String impId = imp.getId(); | ||
| final BidType bidType = determineBidType(imp); | ||
| if (bidType == null) { | ||
| throw new PreBidException("Failed to get media type"); | ||
| } | ||
|
Comment on lines
+159
to
+161
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. can't be |
||
|
|
||
| impIdToBidTypeMap.put(impId, bidType); | ||
| } | ||
|
|
||
| return impIdToBidTypeMap; | ||
| } | ||
|
|
||
| private static BidType determineBidType(Imp imp) { | ||
| if (imp.getAudio() != null) { | ||
| return BidType.audio; | ||
| } else if (imp.getVideo() != null) { | ||
| return BidType.video; | ||
| } else if (imp.getXNative() != null) { | ||
| return BidType.xNative; | ||
| } else if (imp.getBanner() != null) { | ||
| return BidType.banner; | ||
| } | ||
|
|
||
| throw new PreBidException("Failed to get media type"); | ||
| } | ||
|
|
||
| private static BidType resolveBidType(Bid bid, Map<String, BidType> impIdToBidTypeMap) { | ||
| return impIdToBidTypeMap.getOrDefault(bid.getImpid(), BidType.banner); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package org.prebid.server.proto.openrtb.ext.request.clydo; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class ExtImpClydo { | ||
|
|
||
| @JsonProperty("partnerId") | ||
| String partnerId; | ||
|
|
||
| @JsonProperty("region") | ||
|
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. No need for |
||
| String region; | ||
| } | ||
| 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.clydo.ClydoBidder; | ||
| 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/clydo.yaml", factory = YamlPropertySourceFactory.class) | ||
| public class ClydoConfiguration { | ||
|
|
||
| private static final String BIDDER_NAME = "clydo"; | ||
|
|
||
| @Bean("clydoConfigurationProperties") | ||
| @ConfigurationProperties("adapters.clydo") | ||
| BidderConfigurationProperties configurationProperties() { | ||
| return new BidderConfigurationProperties(); | ||
| } | ||
|
|
||
| @Bean | ||
| BidderDeps clydoBidderDeps(BidderConfigurationProperties clydoConfigurationProperties, | ||
| @NotBlank @Value("${external-url}") String externalUrl, | ||
| JacksonMapper mapper) { | ||
|
Comment on lines
+31
to
+33
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. Align |
||
|
|
||
| return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
| .withConfig(clydoConfigurationProperties) | ||
| .usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
| .bidderCreator(config -> new ClydoBidder(config.getEndpoint(), mapper)) | ||
| .assemble(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| adapters: | ||
| clydo: | ||
| endpoint: http://region={{Region}}.clydo.io/partnerId={{PartnerId}} | ||
| modifying-vast-xml-allowed: true | ||
| endpoint-compression: gzip | ||
| geoscope: | ||
| - global | ||
| meta-info: | ||
| maintainer-email: cto@clydo.io | ||
| app-media-types: | ||
| - banner | ||
| - video | ||
| - audio | ||
| - native | ||
| site-media-types: | ||
| - banner | ||
| - video | ||
| - audio | ||
| - native | ||
| supported-vendors: | ||
| vendor-id: 0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Clydo Adapter Params", | ||
| "description": "A schema which validates params accepted by the Clydo adapter", | ||
| "type": "object", | ||
| "properties": { | ||
| "partnerId": { | ||
| "type": "string", | ||
| "description": "Partner ID", | ||
| "minLength": 1 | ||
| }, | ||
| "region": { | ||
| "type": "string", | ||
| "description": "Regional endpoint identifier (us, usw, eu, apac)", | ||
| "enum": ["us", "usw", "eu", "apac"] | ||
| } | ||
| }, | ||
|
|
||
| "required": ["partnerId"] | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.