Skip to content

Commit

Permalink
Remove Default Request Hardcoded Aliases (#4020)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxNode authored Oct 30, 2024
1 parent df58baf commit 3907f1a
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 490 deletions.
64 changes: 19 additions & 45 deletions endpoints/info/bidders.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var invalidEnabledOnlyMsg = []byte(`Invalid value for 'enabledonly' query param,
var invalidBaseAdaptersOnlyMsg = []byte(`Invalid value for 'baseadaptersonly' query param, must be of boolean type`)

// NewBiddersEndpoint builds a handler for the /info/bidders endpoint.
func NewBiddersEndpoint(bidders config.BidderInfos, aliases map[string]string) httprouter.Handle {
responseAll, err := prepareBiddersResponseAll(bidders, aliases)
func NewBiddersEndpoint(bidders config.BidderInfos) httprouter.Handle {
responseAll, err := prepareBiddersResponseAll(bidders)
if err != nil {
glog.Fatalf("error creating /info/bidders endpoint all bidders response: %v", err)
}
Expand All @@ -26,7 +26,7 @@ func NewBiddersEndpoint(bidders config.BidderInfos, aliases map[string]string) h
glog.Fatalf("error creating /info/bidders endpoint all bidders (base adapters only) response: %v", err)
}

responseEnabledOnly, err := prepareBiddersResponseEnabledOnly(bidders, aliases)
responseEnabledOnly, err := prepareBiddersResponseEnabledOnly(bidders)
if err != nil {
glog.Fatalf("error creating /info/bidders endpoint enabled only response: %v", err)
}
Expand Down Expand Up @@ -76,7 +76,6 @@ func readQueryFlag(r *http.Request, queryParam string) (flag, ok bool) {
q := r.URL.Query()

v, exists := q[queryParam]

if !exists || len(v) == 0 {
return false, true
}
Expand All @@ -91,26 +90,13 @@ func readQueryFlag(r *http.Request, queryParam string) (flag, ok bool) {
}
}

func prepareBiddersResponseAll(bidders config.BidderInfos, aliases map[string]string) ([]byte, error) {
bidderNames := make([]string, 0, len(bidders)+len(aliases))

for name := range bidders {
bidderNames = append(bidderNames, name)
}

for name := range aliases {
bidderNames = append(bidderNames, name)
}

sort.Strings(bidderNames)
return jsonutil.Marshal(bidderNames)
}
type bidderPredicate func(config.BidderInfo) bool

func prepareBiddersResponseAllBaseOnly(bidders config.BidderInfos) ([]byte, error) {
func prepareResponse(bidders config.BidderInfos, p bidderPredicate) ([]byte, error) {
bidderNames := make([]string, 0, len(bidders))

for name, info := range bidders {
if len(info.AliasOf) == 0 {
if p(info) {
bidderNames = append(bidderNames, name)
}
}
Expand All @@ -119,36 +105,24 @@ func prepareBiddersResponseAllBaseOnly(bidders config.BidderInfos) ([]byte, erro
return jsonutil.Marshal(bidderNames)
}

func prepareBiddersResponseEnabledOnly(bidders config.BidderInfos, aliases map[string]string) ([]byte, error) {
bidderNames := make([]string, 0, len(bidders)+len(aliases))

for name, info := range bidders {
if info.IsEnabled() {
bidderNames = append(bidderNames, name)
}
}
func prepareBiddersResponseAll(bidders config.BidderInfos) ([]byte, error) {
filterNone := func(_ config.BidderInfo) bool { return true }
return prepareResponse(bidders, filterNone)
}

for name, bidder := range aliases {
if info, ok := bidders[bidder]; ok && info.IsEnabled() {
bidderNames = append(bidderNames, name)
}
}
func prepareBiddersResponseAllBaseOnly(bidders config.BidderInfos) ([]byte, error) {
filterBaseOnly := func(info config.BidderInfo) bool { return len(info.AliasOf) == 0 }
return prepareResponse(bidders, filterBaseOnly)
}

sort.Strings(bidderNames)
return jsonutil.Marshal(bidderNames)
func prepareBiddersResponseEnabledOnly(bidders config.BidderInfos) ([]byte, error) {
filterEnabledOnly := func(info config.BidderInfo) bool { return info.IsEnabled() }
return prepareResponse(bidders, filterEnabledOnly)
}

func prepareBiddersResponseEnabledOnlyBaseOnly(bidders config.BidderInfos) ([]byte, error) {
bidderNames := make([]string, 0, len(bidders))

for name, info := range bidders {
if info.IsEnabled() && len(info.AliasOf) == 0 {
bidderNames = append(bidderNames, name)
}
}

sort.Strings(bidderNames)
return jsonutil.Marshal(bidderNames)
filterEnabledOnlyBaseOnly := func(info config.BidderInfo) bool { return info.IsEnabled() && len(info.AliasOf) == 0 }
return prepareResponse(bidders, filterEnabledOnlyBaseOnly)
}

func writeBadRequest(w http.ResponseWriter, data []byte) {
Expand Down
46 changes: 17 additions & 29 deletions endpoints/info/bidders_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ const (
)

// NewBiddersDetailEndpoint builds a handler for the /info/bidders/<bidder> endpoint.
func NewBiddersDetailEndpoint(bidders config.BidderInfos, aliases map[string]string) httprouter.Handle {
responses, err := prepareBiddersDetailResponse(bidders, aliases)
func NewBiddersDetailEndpoint(bidders config.BidderInfos) httprouter.Handle {
responses, err := prepareBiddersDetailResponse(bidders)
if err != nil {
glog.Fatalf("error creating /info/bidders/<bidder> endpoint response: %v", err)
}

return func(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
bidder := ps.ByName("bidderName")
bidderName, found := getNormalizedBidderName(bidder)

coreBidderName, found := getNormalisedBidderName(bidder, aliases)
if !found {
w.WriteHeader(http.StatusNotFound)
}
if response, ok := responses[coreBidderName]; ok {

if response, ok := responses[bidderName]; ok {
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(response); err != nil {
glog.Errorf("error writing response to /info/bidders/%s: %v", bidder, err)
Expand All @@ -43,25 +44,21 @@ func NewBiddersDetailEndpoint(bidders config.BidderInfos, aliases map[string]str
}
}

func getNormalisedBidderName(bidderName string, aliases map[string]string) (string, bool) {
func getNormalizedBidderName(bidderName string) (string, bool) {
if strings.ToLower(bidderName) == "all" {
return "all", true
}
coreBidderName, ok := openrtb_ext.NormalizeBidderName(bidderName)
if !ok { //check default aliases if not found in coreBidders
if _, isDefaultAlias := aliases[bidderName]; isDefaultAlias {
return bidderName, true
}

bidderNameNormalized, ok := openrtb_ext.NormalizeBidderName(bidderName)
if !ok {
return "", false
}
return coreBidderName.String(), true

return bidderNameNormalized.String(), true
}

func prepareBiddersDetailResponse(bidders config.BidderInfos, aliases map[string]string) (map[string][]byte, error) {
details, err := mapDetails(bidders, aliases)
if err != nil {
return nil, err
}
func prepareBiddersDetailResponse(bidders config.BidderInfos) (map[string][]byte, error) {
details := mapDetails(bidders)

responses, err := marshalDetailsResponse(details)
if err != nil {
Expand All @@ -77,25 +74,14 @@ func prepareBiddersDetailResponse(bidders config.BidderInfos, aliases map[string
return responses, nil
}

func mapDetails(bidders config.BidderInfos, aliases map[string]string) (map[string]bidderDetail, error) {
func mapDetails(bidders config.BidderInfos) map[string]bidderDetail {
details := map[string]bidderDetail{}

for bidderName, bidderInfo := range bidders {
details[bidderName] = mapDetailFromConfig(bidderInfo)
}

for aliasName, bidderName := range aliases {
aliasBaseInfo, aliasBaseInfoFound := details[bidderName]
if !aliasBaseInfoFound {
return nil, fmt.Errorf("base adapter %s for alias %s not found", bidderName, aliasName)
}

aliasInfo := aliasBaseInfo
aliasInfo.AliasOf = bidderName
details[aliasName] = aliasInfo
}

return details, nil
return details
}

func marshalDetailsResponse(details map[string]bidderDetail) (map[string][]byte, error) {
Expand Down Expand Up @@ -151,6 +137,8 @@ type platform struct {
func mapDetailFromConfig(c config.BidderInfo) bidderDetail {
var bidderDetail bidderDetail

bidderDetail.AliasOf = c.AliasOf

if c.Maintainer != nil {
bidderDetail.Maintainer = &maintainer{
Email: c.Maintainer.Email,
Expand Down
Loading

0 comments on commit 3907f1a

Please sign in to comment.