github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/deal_tier.go (about)

     1  package openrtb_ext
     2  
     3  import (
     4  	"github.com/prebid/openrtb/v20/openrtb2"
     5  	"github.com/prebid/prebid-server/v2/util/jsonutil"
     6  )
     7  
     8  // DealTier defines the configuration of a deal tier.
     9  type DealTier struct {
    10  	// Prefix specifies the beginning of the hb_pb_cat_dur targeting key value. Must be non-empty.
    11  	Prefix string `json:"prefix"`
    12  
    13  	// MinDealTier specifies the minimum deal priority value (inclusive) that must be met for the targeting
    14  	// key value to be modified. Must be greater than 0.
    15  	MinDealTier int `json:"minDealTier"`
    16  }
    17  
    18  // DealTierBidderMap defines a correlation between bidders and deal tiers.
    19  type DealTierBidderMap map[BidderName]DealTier
    20  
    21  // ReadDealTiersFromImp returns a map of bidder deal tiers read from the impression of an original request (not split / cleaned).
    22  func ReadDealTiersFromImp(imp openrtb2.Imp) (DealTierBidderMap, error) {
    23  	dealTiers := make(DealTierBidderMap)
    24  
    25  	if len(imp.Ext) == 0 {
    26  		return dealTiers, nil
    27  	}
    28  
    29  	var impPrebidExt struct {
    30  		Prebid struct {
    31  			Bidders map[string]struct {
    32  				DealTier *DealTier `json:"dealTier"`
    33  			} `json:"bidder"`
    34  		} `json:"prebid"`
    35  	}
    36  	if err := jsonutil.Unmarshal(imp.Ext, &impPrebidExt); err != nil {
    37  		return nil, err
    38  	}
    39  	for bidder, param := range impPrebidExt.Prebid.Bidders {
    40  		if param.DealTier != nil {
    41  			if bidderNormalized, bidderFound := NormalizeBidderName(bidder); bidderFound {
    42  				dealTiers[bidderNormalized] = *param.DealTier
    43  			} else {
    44  				dealTiers[BidderName(bidder)] = *param.DealTier
    45  			}
    46  		}
    47  	}
    48  
    49  	return dealTiers, nil
    50  }