-
Notifications
You must be signed in to change notification settings - Fork 765
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
Filter Multi-Format Requests #4162
Open
pm-isha-bharti
wants to merge
5
commits into
prebid:master
Choose a base branch
from
pm-isha-bharti:filter_multi_format
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.
+1,078
−114
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b33636c
Adding initial changes for Filter Multi-Format Requests issue(2711)
pm-isha-bharti dc22137
Updating multiformat-supported to pointer
pm-isha-bharti 824c64d
Changing type of acount PreferredMediaType config
pm-isha-bharti 70a1fe0
Adding test cases and minor optimisations
pm-isha-bharti 3445b94
Addressing code review comments - Part 1
pm-isha-bharti 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -72,6 +72,16 @@ func (i *InfoAwareBidder) MakeRequests(request *openrtb2.BidRequest, reqInfo *Ex | |
request.Imp = filteredImps | ||
errs = append(errs, newErrs...) | ||
} | ||
|
||
//if bidder doesnt support multiformat, send only preferred media type in the request | ||
if !i.info.multiformat { | ||
var newErrs []error | ||
request.Imp, newErrs = FilterMultiformatImps(request, reqInfo.PreferredMediaType) | ||
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. Just wondering: do we need a new function to filter these imps altogether? There's already imp filtering happening a couple of lines below. Would it be hard to add that functionality above?
|
||
if newErrs != nil { | ||
errs = append(errs, newErrs...) | ||
} | ||
} | ||
|
||
reqs, delegateErrs := i.Bidder.MakeRequests(request, reqInfo) | ||
return reqs, append(errs, delegateErrs...) | ||
} | ||
|
@@ -140,9 +150,10 @@ func filterImps(imps []openrtb2.Imp, numToFilter int) ([]openrtb2.Imp, []error) | |
|
||
// Structs to handle parsed bidder info, so we aren't reparsing every request | ||
type parsedBidderInfo struct { | ||
app parsedSupports | ||
site parsedSupports | ||
dooh parsedSupports | ||
app parsedSupports | ||
site parsedSupports | ||
dooh parsedSupports | ||
multiformat bool | ||
} | ||
|
||
type parsedSupports struct { | ||
|
@@ -172,5 +183,101 @@ func parseBidderInfo(info config.BidderInfo) parsedBidderInfo { | |
parsedInfo.dooh.enabled = true | ||
parsedInfo.dooh.banner, parsedInfo.dooh.video, parsedInfo.dooh.audio, parsedInfo.dooh.native = parseAllowedTypes(info.Capabilities.DOOH.MediaTypes) | ||
} | ||
parsedInfo.multiformat = IsMultiFormatSupported(info) | ||
|
||
return parsedInfo | ||
} | ||
|
||
// FilterMultiformatImps filters impressions based on the preferred media type if the bidder does not support multiformat. | ||
// It returns the updated list of impressions and any errors encountered during the filtering process. | ||
func FilterMultiformatImps(bidRequest *openrtb2.BidRequest, preferredMediaType openrtb_ext.BidType) ([]openrtb2.Imp, []error) { | ||
var updatedImps []openrtb2.Imp | ||
var errs []error | ||
|
||
for _, imp := range bidRequest.Imp { | ||
if IsMultiFormat(imp) && preferredMediaType != "" { | ||
if err := AdjustImpForPreferredMediaType(&imp, preferredMediaType); err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
updatedImps = append(updatedImps, imp) | ||
} else { | ||
updatedImps = append(updatedImps, imp) | ||
} | ||
} | ||
|
||
if len(updatedImps) == 0 { | ||
errs = append(errs, &errortypes.BadInput{Message: "Bid request contains 0 impressions after filtering."}) | ||
} | ||
|
||
return updatedImps, errs | ||
} | ||
|
||
// AdjustImpForPreferredMediaType modifies the given impression to retain only the preferred media type. | ||
// It returns the updated impression and any error encountered during the adjustment process. | ||
func AdjustImpForPreferredMediaType(imp *openrtb2.Imp, preferredMediaType openrtb_ext.BidType) error { | ||
|
||
// Clear irrelevant media types based on the preferred media type. | ||
switch preferredMediaType { | ||
case openrtb_ext.BidTypeBanner: | ||
if imp.Banner != nil { | ||
imp.Video = nil | ||
imp.Audio = nil | ||
imp.Native = nil | ||
} else { | ||
return &errortypes.BadInput{Message: fmt.Sprintf("Imp %s does not have a valid BANNER media type.", imp.ID)} | ||
} | ||
case openrtb_ext.BidTypeVideo: | ||
if imp.Video != nil { | ||
imp.Banner = nil | ||
imp.Audio = nil | ||
imp.Native = nil | ||
} else { | ||
return &errortypes.BadInput{Message: fmt.Sprintf("Imp %s does not have a valid VIDEO media type.", imp.ID)} | ||
} | ||
case openrtb_ext.BidTypeAudio: | ||
if imp.Audio != nil { | ||
imp.Banner = nil | ||
imp.Video = nil | ||
imp.Native = nil | ||
} else { | ||
return &errortypes.BadInput{Message: fmt.Sprintf("Imp %s does not have a valid AUDIO media type.", imp.ID)} | ||
} | ||
case openrtb_ext.BidTypeNative: | ||
if imp.Native != nil { | ||
imp.Banner = nil | ||
imp.Video = nil | ||
imp.Audio = nil | ||
} else { | ||
return &errortypes.BadInput{Message: fmt.Sprintf("Imp %s does not have a valid NATIVE media type.", imp.ID)} | ||
} | ||
default: | ||
return &errortypes.BadInput{Message: fmt.Sprintf("Imp %s has an invalid preferred media type: %s.", imp.ID, preferredMediaType)} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func IsMultiFormatSupported(bidderInfo config.BidderInfo) bool { | ||
if bidderInfo.OpenRTB != nil && bidderInfo.OpenRTB.MultiformatSupported != nil { | ||
return *bidderInfo.OpenRTB.MultiformatSupported | ||
} | ||
return true | ||
} | ||
|
||
func IsMultiFormat(imp openrtb2.Imp) bool { | ||
count := 0 | ||
if imp.Banner != nil { | ||
count++ | ||
} | ||
if imp.Video != nil { | ||
count++ | ||
} | ||
if imp.Audio != nil { | ||
count++ | ||
} | ||
if imp.Native != nil { | ||
count++ | ||
} | ||
return count > 1 | ||
} |
Oops, something went wrong.
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.
Nitpick: can we rename
newErrs
toformatErrs
or something more descriptive?