Skip to content
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

Telaria bidder review #1077

Merged
merged 2 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 46 additions & 65 deletions src/main/java/org/prebid/server/bidder/telaria/TelariaBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.Publisher;
import com.iab.openrtb.request.Site;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.IOUtils;
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.HttpResponse;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.bidder.telaria.model.TelariaRequestExt;
import org.prebid.server.exception.PreBidException;
Expand All @@ -33,16 +31,15 @@
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.HttpUtil;

import java.io.FileInputStream;
import java.io.IOException;
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;
import java.util.zip.GZIPInputStream;

/**
* Telaria {@link Bidder} implementation.
*/
public class TelariaBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpTelaria>> TELARIA_EXT_TYPE_REFERENCE =
Expand All @@ -60,9 +57,6 @@ public TelariaBidder(String endpointUrl, JacksonMapper mapper) {
@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) {
final List<Imp> validImps = new ArrayList<>();
if (CollectionUtils.isEmpty(bidRequest.getImp())) {
return Result.withError(BidderError.badInput("Telaria: Missing Imp Object"));
}
try {
validateImp(bidRequest.getImp());
} catch (PreBidException e) {
Expand All @@ -88,26 +82,24 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
}

if (bidRequest.getSite() != null) {
requestBuilder.site(modifySite(seatCode, bidRequest.getSite()));
requestBuilder.site(modifySite(bidRequest.getSite(), seatCode));
} else if (bidRequest.getApp() != null) {
requestBuilder.app(modifyApp(seatCode, bidRequest.getApp()));
requestBuilder.app(modifyApp(bidRequest.getApp(), seatCode));
}

final BidRequest outgoingRequest = requestBuilder.imp(validImps).build();
final String body = mapper.encode(outgoingRequest);

return Result.of(Collections.singletonList(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(headers(bidRequest))
.payload(outgoingRequest)
.body(body)
.build()),
Collections.emptyList());
return Result.withValue(HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(headers(bidRequest))
.payload(outgoingRequest)
.body(body)
.build());
}

private void validateImp(List<Imp> imps) {
private static void validateImp(List<Imp> imps) {
boolean hasVideoObject = false;
for (Imp imp : imps) {
if (imp.getBanner() != null) {
Expand All @@ -121,17 +113,7 @@ private void validateImp(List<Imp> imps) {
}
}

private Imp updateImp(Imp imp, ExtImpTelaria extImp, String publisherId) {
if (StringUtils.isBlank(extImp.getSeatCode())) {
throw new PreBidException("Telaria: Seat Code required");
}
return imp.toBuilder()
.tagid(extImp.getAdCode())
.ext(mapper.mapper().valueToTree(ExtImpOutTelaria.of(imp.getTagid(), publisherId)))
.build();
}

private String getPublisherId(BidRequest bidRequest) {
private static String getPublisherId(BidRequest bidRequest) {
if (bidRequest.getSite() != null && bidRequest.getSite().getPublisher() != null) {
return bidRequest.getSite().getPublisher().getId();
} else if (bidRequest.getApp() != null && bidRequest.getApp().getPublisher() != null) {
Expand All @@ -148,24 +130,33 @@ private ExtImpTelaria parseImpExt(Imp imp) {
}
}

private Site modifySite(String seatCode, Site site) {
return site.toBuilder().publisher(createPublisher(seatCode, site.getPublisher())).build();
private Imp updateImp(Imp imp, ExtImpTelaria extImp, String publisherId) {
if (StringUtils.isBlank(extImp.getSeatCode())) {
throw new PreBidException("Telaria: Seat Code required");
}
return imp.toBuilder()
.tagid(extImp.getAdCode())
.ext(mapper.mapper().valueToTree(ExtImpOutTelaria.of(imp.getTagid(), publisherId)))
.build();
}

private App modifyApp(String seatCode, App app) {
return app.toBuilder().publisher(createPublisher(seatCode, app.getPublisher())).build();
private static Site modifySite(Site site, String seatCode) {
return site.toBuilder().publisher(createPublisher(site.getPublisher(), seatCode)).build();
}

private Publisher createPublisher(String seatCode, Publisher publisher) {
private static App modifyApp(App app, String seatCode) {
return app.toBuilder().publisher(createPublisher(app.getPublisher(), seatCode)).build();
}

private static Publisher createPublisher(Publisher publisher, String seatCode) {
return publisher != null
? publisher.toBuilder().id(seatCode).build()
: Publisher.builder().id(seatCode).build();
}

private MultiMap headers(BidRequest bidRequest) {
private static MultiMap headers(BidRequest bidRequest) {
final MultiMap headers = HttpUtil.headers()
.add(HttpUtil.X_OPENRTB_VERSION_HEADER, "2.5")
.add(HttpUtil.ACCEPT_ENCODING_HEADER, "gzip");
.add(HttpUtil.X_OPENRTB_VERSION_HEADER, "2.5");

final Device device = bidRequest.getDevice();
if (device != null) {
Expand All @@ -181,40 +172,30 @@ private MultiMap headers(BidRequest bidRequest) {
@Override
public Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
return Result.of(extractBids(getBidResponse(httpCall.getResponse())),
Collections.emptyList());
} catch (DecodeException | PreBidException | IOException e) {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.withValues(extractBids(bidResponse));
} catch (PreBidException | DecodeException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidResponse bidResponse) {
return bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())
? Collections.emptyList()
: bidsFromResponse(bidResponse);
}

private BidResponse getBidResponse(HttpResponse response) throws IOException {
if ("gzip".equals(response.getHeaders().get("Content-Encoding"))) {
response.getHeaders().remove("Content-Encoding");
return mapper.decodeValue(Buffer.buffer(decompress(response.getBody())), BidResponse.class);
private List<BidderBid> extractBids(BidResponse bidResponse) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
return mapper.decodeValue(response.getBody(), BidResponse.class);
return bidsFromResponse(bidResponse);
}

private static List<BidderBid> bidsFromResponse(BidResponse bidResponse) {
return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
final SeatBid firstSeatBid = bidResponse.getSeatbid().get(0);
final List<Bid> bids = firstSeatBid.getBid();

if (CollectionUtils.isEmpty(bids)) {
return Collections.emptyList();
}
return bids.stream()
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(bid, BidType.video, bidResponse.getCur()))
.collect(Collectors.toList());
}

private static byte[] decompress(String file) throws IOException {
try (GZIPInputStream gzipInput = new GZIPInputStream(new FileInputStream(file))) {
return IOUtils.toByteArray(gzipInput);
}
}
}
Loading