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

Filter Multi-Format Requests (2711) #4162

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions adapters/bidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type ExtraRequestInfo struct {
PbsEntryPoint metrics.RequestType
GlobalPrivacyControlHeader string
CurrencyConversions currency.Conversions
PreferredMediaType openrtb_ext.BidType
}

func NewExtraRequestInfo(c currency.Conversions) ExtraRequestInfo {
Expand Down
113 changes: 110 additions & 3 deletions adapters/infoawarebidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
if newErrs != nil {
errs = append(errs, newErrs...)
}
}

reqs, delegateErrs := i.Bidder.MakeRequests(request, reqInfo)
return reqs, append(errs, delegateErrs...)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Loading
Loading