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

Add the GothamAds Bidder #2536

Merged
merged 2 commits into from
Aug 18, 2023
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
148 changes: 148 additions & 0 deletions src/main/java/org/prebid/server/bidder/gotthamads/GothamAdsBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package org.prebid.server.bidder.gotthamads;

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 io.vertx.core.MultiMap;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.collections4.CollectionUtils;
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.gothamads.GothamAdsImpExt;
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.List;
import java.util.Objects;
import java.util.Optional;

public class GothamAdsBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, GothamAdsImpExt>> TYPE_REFERENCE = new TypeReference<>() {
};
private static final String ACCOUNT_ID_MACRO = "{{AccountId}}";
private static final String X_OPENRTB_VERSION = "2.5";

private final String endpointUrl;
private final JacksonMapper mapper;

public GothamAdsBidder(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 GothamAdsImpExt impExt;
final Imp firstImp = request.getImp().get(0);
try {
impExt = parseImpExt(firstImp);
} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}

final BidRequest bidRequest = cleanUpFirstImpExt(request);
final HttpRequest<BidRequest> httpRequest = HttpRequest.<BidRequest>builder()
AntoxaAntoxic marked this conversation as resolved.
Show resolved Hide resolved
.method(HttpMethod.POST)
.uri(resolveEndpoint(impExt.getAccountId()))
.headers(makeHeaders(request))
.impIds(BidderUtil.impIds(bidRequest))
.body(mapper.encodeToBytes(bidRequest))
.payload(bidRequest)
.build();
return Result.withValue(httpRequest);
}

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

private static BidRequest cleanUpFirstImpExt(BidRequest request) {
final List<Imp> imps = new ArrayList<>(request.getImp());
imps.set(0, request.getImp().get(0).toBuilder().ext(null).build());
return request.toBuilder().imp(imps).build();
}

private String resolveEndpoint(String accountId) {
return endpointUrl.replace(ACCOUNT_ID_MACRO, HttpUtil.encodeUrl(accountId));
}

private static MultiMap makeHeaders(BidRequest bidRequest) {
final Device device = bidRequest.getDevice();
final MultiMap headers = HttpUtil.headers();

headers.set(HttpUtil.X_OPENRTB_VERSION_HEADER, X_OPENRTB_VERSION);
HttpUtil.addHeaderIfValueIsNotEmpty(
headers,
HttpUtil.USER_AGENT_HEADER,
ObjectUtil.getIfNotNull(device, Device::getUa));
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));

return headers;
}

@Override
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.withValues(extractBids(bidResponse));
} catch (DecodeException e) {
return Result.withError(BidderError.badServerResponse("Bad Server Response"));
} catch (PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidResponse bidResponse) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
throw new PreBidException("Empty SeatBid array");
}

return bidResponse.getSeatbid()
.stream()
.flatMap(seatBid -> Optional.ofNullable(seatBid.getBid()).orElse(List.of()).stream())
.map(bid -> BidderBid.of(bid, getBidMediaType(bid), bidResponse.getCur()))
.toList();
}

private static BidType getBidMediaType(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;
case 4 -> BidType.xNative;
default -> throw new PreBidException(
"Unable to fetch mediaType " + bid.getMtype() + " in multi-format: " + bid.getImpid());
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.prebid.server.proto.openrtb.ext.request.gothamads;

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

@Value(staticConstructor = "of")
public class GothamAdsImpExt {

@JsonProperty("accountId")
String accountId;

}
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.gotthamads.GothamAdsBidder;
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/gothamads.yaml", factory = YamlPropertySourceFactory.class)
public class GothamAdsConfiguration {

private static final String BIDDER_NAME = "gothamads";

@Bean("gothamAdsConfigurationProperties")
@ConfigurationProperties("adapters.gothamads")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps gothamadsBidderDeps(BidderConfigurationProperties gothamAdsConfigurationProperties,
@NotBlank @Value("${external-url}") String externalUrl,
JacksonMapper mapper) {

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(gothamAdsConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new GothamAdsBidder(config.getEndpoint(), mapper))
.assemble();
}
}
15 changes: 15 additions & 0 deletions src/main/resources/bidder-config/gothamads.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
adapters:
gothamads:
endpoint: http://us-e-node1.gothamads.com/?pass={{AccountID}}
meta-info:
maintainer-email: [email protected]
app-media-types:
- banner
- video
- native
site-media-types:
- banner
- video
- native
supported-vendors:
vendor-id: 0
16 changes: 16 additions & 0 deletions src/main/resources/static/bidder-params/gothamads.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Gothamads Adapter Params",
"description": "A schema which validates params accepted by the Gothamads adapter",
"type": "object",
"properties": {
"accountId": {
"type": "string",
"description": "Account id",
"minLength": 1
}
},
"required": [
"accountId"
]
}
Loading
Loading