Skip to content

Commit

Permalink
imds: Break out endpoint generation into new method and change to pas…
Browse files Browse the repository at this point in the history
…sing version as string rather than object.
  • Loading branch information
ecammit committed Jul 5, 2023
1 parent 9b635b4 commit b9d61cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
33 changes: 20 additions & 13 deletions src/main/java/org/prebid/server/bidder/imds/ImdsBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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.version.PrebidVersionProvider;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -45,9 +44,9 @@ public class ImdsBidder implements Bidder<BidRequest> {
private final String prebidVersion;
private final JacksonMapper mapper;

public ImdsBidder(String endpointUrl, PrebidVersionProvider prebidVersionProvider, JacksonMapper mapper) {
public ImdsBidder(String endpointUrl, String prebidVersion, JacksonMapper mapper) {
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
this.prebidVersion = Objects.requireNonNull(prebidVersionProvider.getNameVersionRecord());
this.prebidVersion = Objects.requireNonNull(prebidVersion);
this.mapper = Objects.requireNonNull(mapper);
}

Expand Down Expand Up @@ -83,16 +82,24 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
.ext(mapper.fillExtension(ExtRequest.empty(), ExtRequestImds.of(firstExtImp.getSeatId())))
.build();

return Result.of(Collections.singletonList(
return Result.of(
Collections.singletonList(
BidderUtil.defaultRequest(
outgoingRequest,
endpointUrl
.replaceAll("\\{\\{AccountID}}",
URLEncoder.encode(firstExtImp.getSeatId(), StandardCharsets.UTF_8))
.replaceAll("\\{\\{SourceId}}",
URLEncoder.encode(prebidVersion, StandardCharsets.UTF_8)),
mapper)),
errors);
outgoingRequest,
generateEndpointUrl(firstExtImp),
mapper
)
),
errors
);
}

private String generateEndpointUrl(ExtImpImds firstExtImp) {
final String accountId = URLEncoder.encode(firstExtImp.getSeatId(), StandardCharsets.UTF_8);
final String sourceId = URLEncoder.encode(prebidVersion, StandardCharsets.UTF_8);
return endpointUrl
.replaceAll("\\{\\{AccountID}}", accountId)
.replaceAll("\\{\\{SourceId}}", sourceId);
}

private ExtImpImds parseAndValidateExtImp(ObjectNode impExt) {
Expand Down Expand Up @@ -149,7 +156,7 @@ private static BidderBid mapBidToBidderBid(Bid bid, List<Imp> imps, String curre
}

private static BidType getBidType(String impId, List<Imp> imps) {
for (Imp imp : imps) {
for (final Imp imp : imps) {
if (imp.getId().equals(impId)) {
if (imp.getBanner() != null) {
return BidType.banner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ BidderDeps imdsBidderDeps(BidderConfigurationProperties imdsConfigurationPropert
return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(imdsConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new ImdsBidder(config.getEndpoint(), prebidVersionProvider, mapper))
.bidderCreator(config -> new ImdsBidder(
config.getEndpoint(),
prebidVersionProvider.getNameVersionRecord(),
mapper)
)
.assemble();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
import static java.util.function.Function.identity;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.when;
import static org.prebid.server.proto.openrtb.ext.response.BidType.banner;
import static org.prebid.server.proto.openrtb.ext.response.BidType.video;

public class ImdsBidderTest extends VertxTest {

private static final String ENDPOINT_URL = "https://pbs.endpoint.com/{{AccountID}}?src={{SourceId}}&adapter=imds";
private static final String PREBID_VERSION = "pbs-java/0.1.2";

private ImdsBidder imdsBidder;

Expand All @@ -55,14 +55,13 @@ public class ImdsBidderTest extends VertxTest {

@Before
public void setUp() {
when(prebidVersionProvider.getNameVersionRecord()).thenReturn("pbs-java/0.1.2");
imdsBidder = new ImdsBidder(ENDPOINT_URL, prebidVersionProvider, jacksonMapper);
imdsBidder = new ImdsBidder(ENDPOINT_URL, PREBID_VERSION, jacksonMapper);
}

@Test
public void creationShouldFailOnInvalidEndpointUrl() {
assertThatIllegalArgumentException().isThrownBy(
() -> new ImdsBidder("invalid_url", prebidVersionProvider, jacksonMapper));
() -> new ImdsBidder("invalid_url", PREBID_VERSION, jacksonMapper));
}

@Test
Expand Down

0 comments on commit b9d61cc

Please sign in to comment.