Skip to content

Commit

Permalink
Bidder Info Endpoint: Base Adapter Only Filter (#3088)
Browse files Browse the repository at this point in the history
* Base Adapters Only

* Fix Tpyo
  • Loading branch information
SyntaxNode authored Sep 18, 2023
1 parent 8cc0b09 commit f54d374
Show file tree
Hide file tree
Showing 2 changed files with 433 additions and 128 deletions.
113 changes: 93 additions & 20 deletions endpoints/info/bidders.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
"github.com/prebid/prebid-server/config"
)

var invalidEnabledOnly = []byte(`Invalid value for 'enabledonly' query param, must be of boolean type`)
var invalidEnabledOnlyMsg = []byte(`Invalid value for 'enabledonly' query param, must be of boolean type`)
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 {
Expand All @@ -20,43 +21,74 @@ func NewBiddersEndpoint(bidders config.BidderInfos, aliases map[string]string) h
glog.Fatalf("error creating /info/bidders endpoint all bidders response: %v", err)
}

responseAllBaseOnly, err := prepareBiddersResponseAllBaseOnly(bidders)
if err != nil {
glog.Fatalf("error creating /info/bidders endpoint all bidders (base adapters only) response: %v", err)
}

responseEnabledOnly, err := prepareBiddersResponseEnabledOnly(bidders, aliases)
if err != nil {
glog.Fatalf("error creating /info/bidders endpoint enabled only response: %v", err)
}

responseEnabledOnlyBaseOnly, err := prepareBiddersResponseEnabledOnlyBaseOnly(bidders)
if err != nil {
glog.Fatalf("error creating /info/bidders endpoint enabled only (base adapters only) response: %v", err)
}

return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var writeErr error
switch readEnabledOnly(r) {
case "true":
w.Header().Set("Content-Type", "application/json")
_, writeErr = w.Write(responseEnabledOnly)
case "false":
w.Header().Set("Content-Type", "application/json")
_, writeErr = w.Write(responseAll)
default:
w.WriteHeader(http.StatusBadRequest)
_, writeErr = w.Write(invalidEnabledOnly)
enabledOnly, baseAdaptersOnly, errMsg := readQueryFlags(r)
if errMsg != nil {
writeBadRequest(w, errMsg)
return
}

if writeErr != nil {
glog.Errorf("error writing response to /info/bidders: %v", writeErr)
var response []byte
switch {
case !enabledOnly && !baseAdaptersOnly:
response = responseAll
case !enabledOnly && baseAdaptersOnly:
response = responseAllBaseOnly
case enabledOnly && !baseAdaptersOnly:
response = responseEnabledOnly
case enabledOnly && baseAdaptersOnly:
response = responseEnabledOnlyBaseOnly
}
writeResponse(w, response)
}
}

func readQueryFlags(r *http.Request) (enabledOnly, baseAdaptersOnly bool, errMsg []byte) {
enabledOnly, ok := readQueryFlag(r, "enabledonly")
if !ok {
return false, false, invalidEnabledOnlyMsg
}

baseAdapterOnly, ok := readQueryFlag(r, "baseadaptersonly")
if !ok {
return false, false, invalidBaseAdaptersOnlyMsg
}

return enabledOnly, baseAdapterOnly, nil
}

func readEnabledOnly(r *http.Request) string {
func readQueryFlag(r *http.Request, queryParam string) (flag, ok bool) {
q := r.URL.Query()

v, exists := q["enabledonly"]
v, exists := q[queryParam]

if !exists || len(v) == 0 {
// if the enabledOnly query parameter is not specified, default to false to match
// previous behavior of returning all adapters regardless of their enabled status.
return "false"
return false, true
}

return strings.ToLower(v[0])
switch strings.ToLower(v[0]) {
case "true":
return true, true
case "false":
return false, true
default:
return false, false
}
}

func prepareBiddersResponseAll(bidders config.BidderInfos, aliases map[string]string) ([]byte, error) {
Expand All @@ -71,7 +103,19 @@ func prepareBiddersResponseAll(bidders config.BidderInfos, aliases map[string]st
}

sort.Strings(bidderNames)
return json.Marshal(bidderNames)
}

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

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

sort.Strings(bidderNames)
return json.Marshal(bidderNames)
}

Expand All @@ -94,3 +138,32 @@ func prepareBiddersResponseEnabledOnly(bidders config.BidderInfos, aliases map[s

return json.Marshal(bidderNames)
}

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 json.Marshal(bidderNames)
}

func writeBadRequest(w http.ResponseWriter, data []byte) {
w.WriteHeader(http.StatusBadRequest)
writeWithErrorHandling(w, data)
}

func writeResponse(w http.ResponseWriter, data []byte) {
w.Header().Set("Content-Type", "application/json")
writeWithErrorHandling(w, data)
}

func writeWithErrorHandling(w http.ResponseWriter, data []byte) {
if _, err := w.Write(data); err != nil {
glog.Errorf("error writing response to /info/bidders: %v", err)
}
}
Loading

0 comments on commit f54d374

Please sign in to comment.