Skip to content

New Adapter: TrustX#4428

Open
Lightwood13 wants to merge 2 commits intomasterfrom
add-trustx-bidder
Open

New Adapter: TrustX#4428
Lightwood13 wants to merge 2 commits intomasterfrom
add-trustx-bidder

Conversation

@Lightwood13
Copy link
Collaborator

🔧 Type of changes

  • new bid adapter

✨ What's the context?

#4375

.build();
}

private ExtImpTrustx tryParseImpExt(Imp imp) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

tryParseImpExt -> parseImpExt

Comment on lines +91 to +93
return impExt.toBuilder()
.gpid(adSlot != null ? adSlot : impExt.getGpid())
.build();
Copy link
Collaborator

Choose a reason for hiding this comment

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

    private static ExtImpTrustx modifyImpExt(ExtImpTrustx impExt) {
        final String adSlot = Optional.ofNullable(impExt.getData())
                .map(ExtImpTrustxData::getAdServer)
                .map(ExtImpTrustxDataAdServer::getAdSlot)
                .filter(StringUtils::isNotEmpty)
                .orElse(null);
        
        return adSlot != null ? impExt.toBuilder().gpid(adSlot).build() : impExt;
    }

Comment on lines +68 to +74
private Imp modifyImp(Imp imp) {
final ExtImpTrustx impExt = tryParseImpExt(imp);

return impExt == null ? imp : imp.toBuilder()
.ext(mapper.mapper().valueToTree(modifyImpExt(impExt)))
.build();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

    private Imp modifyImp(Imp imp) {
        final ExtImpTrustx impExt = parseImpExt(imp);
        final ExtImpTrustx modifiedImpExt = impExt != null ? modifyImpExt(impExt) : null;

        return impExt != modifiedImpExt
                ? imp.toBuilder().ext(mapper.mapper().valueToTree(modifiedImpExt)).build()
                : imp;
    }

Comment on lines +98 to +105
final String referrer = ObjectUtil.getIfNotNull(site, Site::getRef);
final String domain = ObjectUtil.getIfNotNull(site, Site::getDomain);

final Device device = request.getDevice();
final String ip = StringUtils.firstNonEmpty(
ObjectUtil.getIfNotNull(device, Device::getIpv6),
ObjectUtil.getIfNotNull(device, Device::getIp));
final String userAgent = ObjectUtil.getIfNotNull(device, Device::getUa);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We usually use ObjectUtil.getIfNotNull for cases like

ObjectUtil.getIfNotNull(request.getDevice(), Device::getIpv6)

so you don't need to make a local variable. For me,

device != null ? device.getIpv6() : null

looks simpler.


private Result<List<BidderBid>> bidsFromResponse(BidResponse bidResponse) {
final List<BidderError> errors = new ArrayList<>();
final List<BidderBid> bidderBids = Stream.ofNullable(bidResponse)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't be null

Comment on lines +154 to +223
private BidderBid makeBid(Bid bid, List<BidderError> errors) {
try {
final BidType bidType = getBidType(bid);

final ExtBidPrebidVideo videoInfo = (bidType == BidType.video) ? ExtBidPrebidVideo.of(
bid.getDur() != null && bid.getDur() > 0 ? bid.getDur() : null,
CollectionUtils.isNotEmpty(bid.getCat()) ? bid.getCat().getFirst() : null
) : null;

final Bid modifiedBid = Optional.ofNullable(tryParseBidExt(bid.getExt()))
.map(TrustxBidder::modifyBidExt)
.map(mapper.mapper()::<ObjectNode>valueToTree)
.map(extBid -> bid.toBuilder().ext(extBid).build())
.orElse(bid);

return BidderBid.builder()
.bid(modifiedBid)
.type(bidType)
.videoInfo(videoInfo)
.build();
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
return null;
}
}

private static BidType getBidType(Bid bid) {
final Integer markupType = bid.getMtype();
if (markupType == null) {
throw new PreBidException("Missing MType for bid: " + bid.getId());
}

return switch (markupType) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
default -> throw new PreBidException("Unsupported MType: %d".formatted(markupType));
};
}

private ExtPrebid<ExtBidPrebid, ExtBidBidderTrustx> tryParseBidExt(ObjectNode bidExt) {
try {
return mapper.mapper().convertValue(bidExt, TRUSTX_BID_EXT_TYPE_REFERENCE);
} catch (IllegalArgumentException e) {
return null;
}
}

private static ExtPrebid<ExtBidPrebid, ExtBidBidderTrustx> modifyBidExt(
ExtPrebid<ExtBidPrebid, ExtBidBidderTrustx> extBid) {

return Optional.ofNullable(extBid.getBidder())
.map(ExtBidBidderTrustx::getTrustx)
.map(ExtBidTrustx::getNetworkName)
.filter(StringUtils::isNotEmpty)
.map(networkName -> ExtBidPrebidMeta.builder().networkName(networkName).build())
.map(extBidPrebidMeta -> modifyBidExtMeta(extBid, extBidPrebidMeta))
.orElse(extBid);
}

private static ExtPrebid<ExtBidPrebid, ExtBidBidderTrustx> modifyBidExtMeta(
ExtPrebid<ExtBidPrebid, ExtBidBidderTrustx> extBid, ExtBidPrebidMeta extBidPrebidMeta) {

final ExtBidPrebid updatedExtBidPrebid = Optional.ofNullable(extBid.getPrebid())
.map(ExtBidPrebid::toBuilder)
.orElseGet(ExtBidPrebid::builder)
.meta(extBidPrebidMeta)
.build();

return ExtPrebid.of(updatedExtBidPrebid, extBid.getBidder());
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

    private BidderBid makeBidderBid(Bid bid, List<BidderError> errors) {
        final BidType bidType;
        try {
            bidType = getBidType(bid);
        } catch (PreBidException e) {
            errors.add(BidderError.badInput(e.getMessage()));
            return null;
        }

        return BidderBid.builder()
                .bid(modifyBid(bid))
                .type(bidType)
                .videoInfo(bidType == BidType.video ? makeExtBidPrebidVideo(bid) : null)
                .build();
    }

    private Bid modifyBid(Bid bid) {
        final ExtPrebid<ExtBidPrebid, ExtBidBidderTrustx> ext = parseBidExt(bid.getExt());
        if (ext == null) {
            return bid;
        }

        final ExtBidBidderTrustx extBidder = ext.getBidder();
        final String networkName = Optional.ofNullable(extBidder)
                .map(ExtBidBidderTrustx::getTrustx)
                .map(ExtBidTrustx::getNetworkName)
                .filter(StringUtils::isNotEmpty)
                .orElse(null);
        if (networkName == null) {
            return bid;
        }

        final ExtBidPrebid modifiedExtPrebid = Optional.ofNullable(ext.getPrebid())
                .map(ExtBidPrebid::toBuilder)
                .orElseGet(ExtBidPrebid::builder)
                .meta(ExtBidPrebidMeta.builder().networkName(networkName).build())
                .build();
        final ObjectNode modifiedExt = mapper.mapper().valueToTree(ExtPrebid.of(modifiedExtPrebid, extBidder));
        
        return bid.toBuilder().ext(modifiedExt).build();
    }

    private ExtPrebid<ExtBidPrebid, ExtBidBidderTrustx> parseBidExt(ObjectNode bidExt) {
        try {
            return mapper.mapper().convertValue(bidExt, TRUSTX_BID_EXT_TYPE_REFERENCE);
        } catch (IllegalArgumentException e) {
            return null;
        }
    }

    private static BidType getBidType(Bid bid) {
        final Integer markupType = bid.getMtype();
        if (markupType == null) {
            throw new PreBidException("Missing MType for bid: " + bid.getId());
        }

        return switch (markupType) {
            case 1 -> BidType.banner;
            case 2 -> BidType.video;
            default -> throw new PreBidException("Unsupported MType: %d".formatted(markupType));
        };
    }

    private static ExtBidPrebidVideo makeExtBidPrebidVideo(Bid bid) {
        final Integer dur = bid.getDur();
        final List<String> cat = bid.getCat();

        return ExtBidPrebidVideo.of(
                dur != null && dur > 0 ? dur : null,
                CollectionUtils.isNotEmpty(cat) ? cat.getFirst() : null);
    }

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

@Value
Copy link
Collaborator

Choose a reason for hiding this comment

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

add static constructor

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

@Value
Copy link
Collaborator

Choose a reason for hiding this comment

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

same

Comment on lines +31 to +33
BidderDeps trustxBidderDeps(BidderConfigurationProperties trustxConfigurationProperties,
@NotBlank @Value("${external-url}") String externalUrl,
JacksonMapper mapper) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

align lines

final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest);

// then
result.getValue().getFirst().getHeaders().getAll(HttpUtil.X_FORWARDED_FOR_HEADER);
Copy link
Collaborator

Choose a reason for hiding this comment

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

???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Port PR from PBS-Go: New Adapter: TRUSTX (remove Grid alias)

2 participants