forked from prebid/prebid-server-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Port Mobkoi: New Adapter #1
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
Draft
mbonnafon
wants to merge
7
commits into
master
Choose a base branch
from
port-mobkoi-adapter-from-golang-to-java
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a376abd
Port Mobkoi: New Adapter
mbonnafon b2864fb
fixup after review
mbonnafon 832661e
fixup after 2nd review
mbonnafon 284ec10
fixup after 3rd review
mbonnafon 37d6883
invert functions order
mbonnafon 129795d
fixup after 4th review
mbonnafon f51138a
fixup after 5th review
mbonnafon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
src/main/java/org/prebid/server/bidder/mobkoi/MobkoiBidder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| package org.prebid.server.bidder.mobkoi; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.iab.openrtb.request.BidRequest; | ||
| import com.iab.openrtb.request.Imp; | ||
| import com.iab.openrtb.request.User; | ||
| import com.iab.openrtb.response.BidResponse; | ||
| import com.iab.openrtb.response.SeatBid; | ||
| import org.apache.commons.collections4.CollectionUtils; | ||
| 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.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.ExtUser; | ||
| import org.prebid.server.proto.openrtb.ext.request.mobkoi.ExtImpMobkoi; | ||
| import org.prebid.server.proto.openrtb.ext.response.BidType; | ||
| import org.prebid.server.util.BidderUtil; | ||
| import org.prebid.server.util.HttpUtil; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| public class MobkoiBidder implements Bidder<BidRequest> { | ||
|
|
||
| private static final TypeReference<ExtPrebid<?, ExtImpMobkoi>> MOBKOI_EXT_TYPE_REFERENCE = | ||
| new TypeReference<>() { | ||
| }; | ||
|
|
||
| private final String endpointUrl; | ||
| private final JacksonMapper mapper; | ||
|
|
||
| public MobkoiBidder(String endpointUrl, JacksonMapper mapper) { | ||
| this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); | ||
| this.mapper = Objects.requireNonNull(mapper); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) { | ||
| final Imp firstImp = bidRequest.getImp().getFirst(); | ||
|
|
||
| final ExtImpMobkoi extImpMobkoi; | ||
| final Imp modifiedFirstImp; | ||
| try { | ||
| extImpMobkoi = parseExtImp(firstImp); | ||
| modifiedFirstImp = modifyImp(firstImp, extImpMobkoi); | ||
| } catch (PreBidException e) { | ||
| return Result.withError(BidderError.badInput(e.getMessage())); | ||
| } | ||
|
|
||
| final String selectedEndpointUrl = resolveEndpoint(extImpMobkoi.getAdServerBaseUrl()); | ||
|
|
||
| return Result.withValue(BidderUtil.defaultRequest( | ||
| modifyBidRequest(bidRequest, modifiedFirstImp), | ||
| selectedEndpointUrl, | ||
| mapper)); | ||
| } | ||
|
|
||
| private ExtImpMobkoi parseExtImp(Imp imp) { | ||
| try { | ||
| return mapper.mapper().convertValue(imp.getExt(), MOBKOI_EXT_TYPE_REFERENCE).getBidder(); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new PreBidException( | ||
| "Invalid imp.ext for impression id %s. Error Information: %s" | ||
| .formatted(imp.getId(), e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| private Imp modifyImp(Imp firstImp, ExtImpMobkoi extImpMobkoi) { | ||
| if (StringUtils.isNotBlank(firstImp.getTagid())) { | ||
| return firstImp; | ||
| } | ||
|
|
||
| if (StringUtils.isNotBlank(extImpMobkoi.getPlacementId())) { | ||
| return firstImp.toBuilder().tagid(extImpMobkoi.getPlacementId()).build(); | ||
| } | ||
|
|
||
| throw new PreBidException("invalid because it comes with neither request.imp[0].tagId nor " | ||
| + "req.imp[0].ext.Bidder.placementId"); | ||
| } | ||
|
|
||
| // url is already validated with `bidder-params` json schema | ||
| private String resolveEndpoint(String customUri) { | ||
| if (customUri == null) { | ||
| return endpointUrl; | ||
| } | ||
| try { | ||
| final URI uri = new URI(customUri); | ||
| return uri.resolve("/bid").toString(); | ||
| } catch (IllegalArgumentException | URISyntaxException e) { | ||
| return endpointUrl; | ||
| } | ||
| } | ||
|
|
||
| private static BidRequest modifyBidRequest(BidRequest bidRequest, Imp modifiedFirstImp) { | ||
| final User user = modifyUser(bidRequest.getUser()); | ||
| final List<Imp> imps = updateFirstImpWith(bidRequest.getImp(), modifiedFirstImp); | ||
| return bidRequest.toBuilder().user(user).imp(imps).build(); | ||
| } | ||
|
|
||
| private static User modifyUser(User user) { | ||
| return Optional.ofNullable(user) | ||
| .map(User::getConsent) | ||
| .map(consent -> ExtUser.builder().consent(consent).build()) | ||
| .map(ext -> user.toBuilder().ext(ext).build()) | ||
| .orElse(user); | ||
| } | ||
|
|
||
| private static List<Imp> updateFirstImpWith(List<Imp> imps, Imp imp) { | ||
| final List<Imp> modifiedImps = new ArrayList<>(imps); | ||
| modifiedImps.set(0, imp); | ||
| return Collections.unmodifiableList(modifiedImps); | ||
| } | ||
|
|
||
| @Override | ||
| public final 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 | PreBidException e) { | ||
| return Result.withError(BidderError.badServerResponse(e.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| private static List<BidderBid> extractBids(BidResponse bidResponse) { | ||
| if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { | ||
| return Collections.emptyList(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. emptyList()
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| return bidsFromResponse(bidResponse); | ||
| } | ||
|
|
||
| private static List<BidderBid> bidsFromResponse(BidResponse bidResponse) { | ||
| return bidResponse.getSeatbid() | ||
| .stream() | ||
| .filter(Objects::nonNull) | ||
| .map(SeatBid::getBid) | ||
| .filter(Objects::nonNull) | ||
| .flatMap(Collection::stream) | ||
| .map(bid -> BidderBid.of(bid, BidType.banner, "mobkoi", bidResponse.getCur())) | ||
| .toList(); | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
src/main/java/org/prebid/server/proto/openrtb/ext/request/mobkoi/ExtImpMobkoi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package org.prebid.server.proto.openrtb.ext.request.mobkoi; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Value; | ||
|
|
||
| @Value(staticConstructor = "of") | ||
| public class ExtImpMobkoi { | ||
|
|
||
| @JsonProperty("placementId") | ||
| String placementId; | ||
|
|
||
| @JsonProperty("adServerBaseUrl") | ||
| String adServerBaseUrl; | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/prebid/server/spring/config/bidder/MobkoiConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.mobkoi.MobkoiBidder; | ||
| 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/mobkoi.yaml", factory = YamlPropertySourceFactory.class) | ||
| public class MobkoiConfiguration { | ||
|
|
||
| private static final String BIDDER_NAME = "mobkoi"; | ||
|
|
||
| @Bean("mobkoiConfigurationProperties") | ||
| @ConfigurationProperties("adapters.mobkoi") | ||
| BidderConfigurationProperties configurationProperties() { | ||
| return new BidderConfigurationProperties(); | ||
| } | ||
|
|
||
| @Bean | ||
| BidderDeps mobkoiBidderDeps(BidderConfigurationProperties mobkoiConfigurationProperties, | ||
| @NotBlank @Value("${external-url}") String externalUrl, | ||
| JacksonMapper mapper) { | ||
|
|
||
| return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
| .withConfig(mobkoiConfigurationProperties) | ||
| .usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
| .bidderCreator(config -> new MobkoiBidder(config.getEndpoint(), mapper)) | ||
| .assemble(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| adapters: | ||
| mobkoi: | ||
| endpoint: "https://pbs.maximus.mobkoi.com/bid" | ||
| ortb-version: "2.6" | ||
| meta-info: | ||
| maintainer-email: [email protected] | ||
| app-media-types: | ||
| site-media-types: | ||
| - banner | ||
| supported-vendors: | ||
| vendor-id: 898 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Mobkoi Adapter Params", | ||
| "description": "A schema which validates params accepted by the Mobkoi adapter", | ||
| "type": "object", | ||
| "properties": { | ||
| "placementId": { | ||
| "type": "string", | ||
| "description": "Placement ID" | ||
| }, | ||
| "adServerBaseUrl": { | ||
| "type": "string", | ||
| "description": "Mobkoi's ad server url", | ||
| "pattern": "^https?://[^.]+\\.mobkoi\\.com$" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bidResponse.getSeatbid().isEmpty() ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per contributing guidelines, I followed already existing adapters style, they all have this logic to check if it's empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.