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

Consolidate Unruly Adapter: Refactor #1662

Merged
merged 5 commits into from
Feb 18, 2022
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
105 changes: 48 additions & 57 deletions src/main/java/org/prebid/server/bidder/unruly/UnrulyBidder.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.prebid.server.bidder.unruly;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.node.ObjectNode;
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.MultiMap;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.collections4.CollectionUtils;
import org.prebid.server.bidder.Bidder;
Expand All @@ -14,12 +14,9 @@
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.bidder.unruly.proto.ImpExtUnruly;
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.unruly.ExtImpUnruly;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.HttpUtil;

Expand All @@ -32,10 +29,6 @@

public class UnrulyBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpUnruly>> UNRULY_EXT_TYPE_REFERENCE =
new TypeReference<>() {
};

private final String endpointUrl;
private final JacksonMapper mapper;

Expand All @@ -46,95 +39,93 @@ public UnrulyBidder(String endpointUrl, JacksonMapper mapper) {

@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) {
final List<BidderError> errors = new ArrayList<>();

final List<Imp> modifiedImps = new ArrayList<>();
for (Imp imp : request.getImp()) {
try {
final ExtImpUnruly extImpUnruly = parseImpExt(imp);
modifiedImps.add(modifyImp(imp, extImpUnruly));
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
}
}

final List<HttpRequest<BidRequest>> outgoingRequests = modifiedImps.stream()
.map(imp -> createSingleRequest(imp, request, endpointUrl))
final List<HttpRequest<BidRequest>> httpRequests = request.getImp().stream()
.map(this::modifyImp)
.map(imp -> createSingleRequest(imp, request))
.collect(Collectors.toList());

return Result.of(outgoingRequests, errors);
return Result.withValues(httpRequests);
}

private ExtImpUnruly parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), UNRULY_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException(e.getMessage(), e);
}
}

private Imp modifyImp(Imp imp, ExtImpUnruly extImpUnruly) {
final Imp.ImpBuilder modifiedImp = imp.toBuilder();

try {
modifiedImp.ext(mapper.mapper().valueToTree(ImpExtUnruly.of(extImpUnruly)));
} catch (IllegalArgumentException e) {
throw new PreBidException(e.getMessage(), e);
}
private Imp modifyImp(Imp imp) {
final ObjectNode modifiedExt = mapper.mapper().createObjectNode()
.set("bidder", imp.getExt().get("bidder"));

return modifiedImp.build();
return imp.toBuilder().ext(modifiedExt).build();
}

private HttpRequest<BidRequest> createSingleRequest(Imp modifiedImp, BidRequest request,
String endpointUrl) {
final BidRequest outgoingRequest = request.toBuilder().imp(Collections.singletonList(modifiedImp)).build();
private HttpRequest<BidRequest> createSingleRequest(Imp imp, BidRequest request) {
final BidRequest outgoingRequest = request.toBuilder().imp(Collections.singletonList(imp)).build();

return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(getHeaders())
.headers(HttpUtil.headers())
.body(mapper.encodeToBytes(outgoingRequest))
.payload(outgoingRequest)
.build();
}

private static MultiMap getHeaders() {
return HttpUtil.headers()
.add("X-Unruly-Origin", "Prebid-Server");
}

@Override
public Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) {
final List<BidderError> errors = new ArrayList<>();
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.withValues(extractBids(httpCall.getRequest().getPayload(), bidResponse));
return Result.of(extractBids(httpCall.getRequest().getPayload(), bidResponse, errors), errors);
} catch (DecodeException | PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidResponse) {
private static List<BidderBid> extractBids(BidRequest bidRequest,
BidResponse bidResponse,
List<BidderError> errors) {

return bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())
? Collections.emptyList()
: bidsFromResponse(bidRequest, bidResponse);
: bidsFromResponse(bidRequest, bidResponse, errors);
}

private static List<BidderBid> bidsFromResponse(BidRequest bidRequest, BidResponse bidResponse) {
private static List<BidderBid> bidsFromResponse(BidRequest bidRequest,
BidResponse bidResponse,
List<BidderError> errors) {

return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(bid, getBidType(bid.getImpid(), bidRequest.getImp()), bidResponse.getCur()))
.filter(Objects::nonNull)
.map(bid -> resolveBidderBid(bid, bidResponse.getCur(), bidRequest.getImp(), errors))
.collect(Collectors.toList());
}

private static BidderBid resolveBidderBid(Bid bid, String currency, List<Imp> imps, List<BidderError> errors) {
try {
return BidderBid.of(bid, getBidType(bid.getImpid(), imps), currency);
} catch (PreBidException e) {
errors.add(BidderError.badServerResponse(e.getMessage()));
return BidderBid.of(bid, BidType.banner, currency);
}
}

private static BidType getBidType(String impId, List<Imp> imps) {
final List<String> unmatchedImpIds = new ArrayList<>();

for (Imp imp : imps) {
if (imp.getId().equals(impId)) {
return BidType.video;
if (imp.getBanner() != null) {
return BidType.banner;
} else if (imp.getVideo() != null) {
return BidType.video;
}
throw new PreBidException("bid responses mediaType didn't match supported mediaTypes");
} else {
unmatchedImpIds.add(imp.getId());
}
}
throw new PreBidException(String.format("Failed to find impression %s", impId));

throw new PreBidException(
"Bid response imp ID " + impId + " not found in bid request containing imps" + unmatchedImpIds);
}
}

This file was deleted.

This file was deleted.

8 changes: 5 additions & 3 deletions src/main/resources/bidder-config/unruly.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
adapters:
unruly:
endpoint: http://targeting.unrulymedia.com/openrtb/2.2
endpoint: https://targeting.unrulymedia.com/unruly_prebid_server
meta-info:
maintainer-email: adspaces@unrulygroup.com
maintainer-email: prebidsupport@unrulygroup.com
app-media-types:
- banner
- video
site-media-types:
- banner
- video
supported-vendors:
vendor-id: 162
vendor-id: 36
usersync:
url: https://sync.1rx.io/usersync2/rmphb?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&redir=
redirect-url: /setuid?bidder=unruly&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&uid=[RX_UUID]
Expand Down
7 changes: 1 addition & 6 deletions src/main/resources/static/bidder-params/unruly.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
"description": "A schema which validates params accepted by the Unruly adapter",
"type": "object",
"properties": {
"uuid": {
"type": "string",
"description": "uuid"
},
"siteid": {
"type": "string",
"type": "integer",
"description": "ID for publisher site"
}
},
"required": [
"uuid",
"siteid"
]
}
Loading