-
Notifications
You must be signed in to change notification settings - Fork 944
New Adapter: StackAdapt #4724
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
Merged
Merged
New Adapter: StackAdapt #4724
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2e8133c
New Adapter: StackAdapter
franklong-sa b4d9dfd
Fix bid memory address issue
franklong-sa 31ef553
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa eddb6ea
add supplemental tests, clean up checks
franklong-sa b6cf5d7
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa cc99357
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa b7e9b81
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa 8034cd6
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa 614b5c0
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa d381023
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa 3af52dd
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa c995966
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa fc88f23
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa abb600c
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa df84272
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa f40fb9f
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa 9d9cd21
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa 5b66f22
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa 1703abf
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa b305f21
address comments - use jsonutil, query escape params, pub/supply reso…
franklong-sa 79e6c67
fix tests
franklong-sa 9d493da
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa 2393fee
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa 4cda345
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa ccd4ad3
Merge branch 'master' into stackadapt-bidder-adapter
franklong-sa 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package stackadapt | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/prebid/prebid-server/v4/openrtb_ext" | ||
| ) | ||
|
|
||
| func TestValidParams(t *testing.T) { | ||
| validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params") | ||
| if err != nil { | ||
| t.Fatalf("Failed to fetch the json-schemas. %v", err) | ||
| } | ||
|
|
||
| for _, validParam := range validParams { | ||
| if err := validator.Validate(openrtb_ext.BidderStackAdapt, json.RawMessage(validParam)); err != nil { | ||
| t.Errorf("Schema rejected stackadapt params: %s", validParam) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestInvalidParams(t *testing.T) { | ||
| validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params") | ||
| if err != nil { | ||
| t.Fatalf("Failed to fetch the json-schemas. %v", err) | ||
| } | ||
|
|
||
| for _, invalidParam := range invalidParams { | ||
| if err := validator.Validate(openrtb_ext.BidderStackAdapt, json.RawMessage(invalidParam)); err == nil { | ||
| t.Errorf("Schema allowed unexpected params: %s", invalidParam) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var validParams = []string{ | ||
| `{"publisherId":"pub-123","supplyId":"ssp-1"}`, | ||
| `{"publisherId":"pub-123","supplyId":"ssp-1","placementId":"placement-456"}`, | ||
| `{"publisherId":"pub-123","supplyId":"ssp-1","banner":{"expdir":[1,3]}}`, | ||
| `{"publisherId":"pub-123","supplyId":"ssp-1","bidfloor":1.5}`, | ||
| `{"publisherId":"pub-123","supplyId":"ssp-1","placementId":"pl-1","banner":{"expdir":[1,2,3]},"bidfloor":0.5}`, | ||
| } | ||
|
|
||
| var invalidParams = []string{ | ||
| ``, | ||
| `null`, | ||
| `true`, | ||
| `5`, | ||
| `[]`, | ||
| `{}`, | ||
| `{"placementId":"placement-456"}`, | ||
| `{"publisherId":"pub-123"}`, | ||
| `{"supplyId":"ssp-1"}`, | ||
| `{"publisherId":""}`, | ||
| `{"publisherId":"pub-123","supplyId":""}`, | ||
| `{"publisherId":"pub-123","supplyId":"ssp-1","bidfloor":-1}`, | ||
| `{"publisherId":"pub-123","supplyId":"ssp-1","banner":"invalid"}`, | ||
| `{"publisherId":"pub-123","supplyId":"ssp-1","bidfloor":"high"}`, | ||
| `{"publisherId":"pub-123","supplyId":"ssp-1","banner":{"expdir":["a"]}}`, | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| package stackadapt | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| "strings" | ||
| "text/template" | ||
|
|
||
| "github.com/buger/jsonparser" | ||
| "github.com/prebid/openrtb/v20/adcom1" | ||
| "github.com/prebid/openrtb/v20/openrtb2" | ||
|
|
||
| "github.com/prebid/prebid-server/v4/adapters" | ||
| "github.com/prebid/prebid-server/v4/config" | ||
| "github.com/prebid/prebid-server/v4/errortypes" | ||
| "github.com/prebid/prebid-server/v4/macros" | ||
| "github.com/prebid/prebid-server/v4/openrtb_ext" | ||
| "github.com/prebid/prebid-server/v4/util/jsonutil" | ||
| ) | ||
|
|
||
| type adapter struct { | ||
| endpoint *template.Template | ||
| } | ||
|
|
||
| func Builder(_ openrtb_ext.BidderName, cfg config.Adapter, _ config.Server) (adapters.Bidder, error) { | ||
| endpointTemplate, err := template.New("endpointTemplate").Parse(cfg.Endpoint) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to parse endpoint url template: %w", err) | ||
| } | ||
| return &adapter{ | ||
| endpoint: endpointTemplate, | ||
| }, nil | ||
| } | ||
|
|
||
| func (a *adapter) MakeRequests(request *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
| // Imp level | ||
| publisherID, supplyID, err := setImpsAndGetEndpointParams(request) | ||
| if err != nil { | ||
| return nil, []error{err} | ||
| } | ||
|
|
||
| // Request level | ||
| setPublisherID(request, publisherID) | ||
|
|
||
| endpointURL, err := a.buildEndpointURL(publisherID, supplyID) | ||
| if err != nil { | ||
| return nil, []error{err} | ||
| } | ||
|
|
||
| body, err := jsonutil.Marshal(request) | ||
| if err != nil { | ||
| return nil, []error{fmt.Errorf("marshal bidRequest: %w", err)} | ||
| } | ||
|
|
||
| return []*adapters.RequestData{{ | ||
| Method: http.MethodPost, | ||
| Uri: endpointURL, | ||
| Body: body, | ||
| Headers: http.Header{ | ||
| "Content-Type": {"application/json;charset=utf-8"}, | ||
| "Accept": {"application/json"}, | ||
| }, | ||
| ImpIDs: openrtb_ext.GetImpIDs(request.Imp), | ||
| }}, nil | ||
| } | ||
|
|
||
| func (a *adapter) buildEndpointURL(publisherID, supplyID string) (string, error) { | ||
| params := macros.EndpointTemplateParams{ | ||
| PublisherID: url.QueryEscape(publisherID), | ||
| SupplyId: url.QueryEscape(supplyID), | ||
| } | ||
| return macros.ResolveMacros(a.endpoint, params) | ||
| } | ||
|
|
||
| func setImpsAndGetEndpointParams(request *openrtb2.BidRequest) (string, string, error) { | ||
| var publisherID, supplyID string | ||
| for i, imp := range request.Imp { | ||
| var bidderExt adapters.ExtImpBidder | ||
| if err := jsonutil.Unmarshal(imp.Ext, &bidderExt); err != nil { | ||
| return "", "", &errortypes.BadInput{Message: fmt.Sprintf("imp[%d]: unable to unmarshal ext: %s", i, err.Error())} | ||
| } | ||
|
|
||
| var saExt openrtb_ext.ExtImpStackAdapt | ||
| if err := jsonutil.Unmarshal(bidderExt.Bidder, &saExt); err != nil { | ||
| return "", "", &errortypes.BadInput{Message: fmt.Sprintf("imp[%d]: unable to unmarshal bidder ext: %s", i, err.Error())} | ||
| } | ||
|
|
||
| if i == 0 { | ||
| publisherID = saExt.PublisherId | ||
| supplyID = saExt.SupplyId | ||
| } else if saExt.PublisherId != publisherID || saExt.SupplyId != supplyID { | ||
| return "", "", &errortypes.BadInput{Message: fmt.Sprintf("imp[%d]: all imps must have the same publisherId and supplyId", i)} | ||
| } | ||
|
|
||
| if saExt.PlacementId != nil { | ||
| request.Imp[i].TagID = *saExt.PlacementId | ||
| } | ||
|
|
||
| if saExt.BidFloor > 0 { | ||
| request.Imp[i].BidFloor = saExt.BidFloor | ||
| request.Imp[i].BidFloorCur = "USD" | ||
| } | ||
|
|
||
| if saExt.Banner != nil && len(saExt.Banner.ExpDir) > 0 && request.Imp[i].Banner != nil { | ||
| bannerCopy := *request.Imp[i].Banner | ||
| bannerCopy.ExpDir = make([]adcom1.ExpandableDirection, len(saExt.Banner.ExpDir)) | ||
| for j, dir := range saExt.Banner.ExpDir { | ||
| bannerCopy.ExpDir[j] = adcom1.ExpandableDirection(dir) | ||
| } | ||
| request.Imp[i].Banner = &bannerCopy | ||
| } | ||
| } | ||
|
|
||
| return publisherID, supplyID, nil | ||
| } | ||
|
|
||
| func setPublisherID(request *openrtb2.BidRequest, publisherID string) { | ||
| if request.Site != nil { | ||
| siteCopy := *request.Site | ||
| if siteCopy.Publisher != nil { | ||
| publisherCopy := *siteCopy.Publisher | ||
| publisherCopy.ID = publisherID | ||
| siteCopy.Publisher = &publisherCopy | ||
| } else { | ||
| siteCopy.Publisher = &openrtb2.Publisher{ID: publisherID} | ||
| } | ||
| request.Site = &siteCopy | ||
| } else if request.App != nil { | ||
| appCopy := *request.App | ||
| if appCopy.Publisher != nil { | ||
| publisherCopy := *appCopy.Publisher | ||
| publisherCopy.ID = publisherID | ||
| appCopy.Publisher = &publisherCopy | ||
|
franklong-sa marked this conversation as resolved.
|
||
| } else { | ||
| appCopy.Publisher = &openrtb2.Publisher{ID: publisherID} | ||
| } | ||
| request.App = &appCopy | ||
| } | ||
| } | ||
|
|
||
| func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
| if adapters.IsResponseStatusCodeNoContent(responseData) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { | ||
| return nil, []error{err} | ||
|
franklong-sa marked this conversation as resolved.
|
||
| } | ||
|
|
||
| var response openrtb2.BidResponse | ||
| if err := jsonutil.Unmarshal(responseData.Body, &response); err != nil { | ||
| return nil, []error{err} | ||
| } | ||
|
|
||
| bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) | ||
| bidResponse.Currency = response.Cur | ||
|
|
||
| var errs []error | ||
| for _, seatBid := range response.SeatBid { | ||
| for i := range seatBid.Bid { | ||
| bidType, err := getMediaTypeForBid(seatBid.Bid[i]) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| continue | ||
| } | ||
|
|
||
| if bidType == openrtb_ext.BidTypeNative { | ||
| seatBid.Bid[i].AdM, err = getNativeAdm(seatBid.Bid[i].AdM) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| continue | ||
| } | ||
| } | ||
| resolveMacros(&seatBid.Bid[i]) | ||
|
|
||
| bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
| Bid: &seatBid.Bid[i], | ||
| BidType: bidType, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return bidResponse, errs | ||
| } | ||
|
|
||
| func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) { | ||
| switch bid.MType { | ||
| case openrtb2.MarkupBanner: | ||
| return openrtb_ext.BidTypeBanner, nil | ||
| case openrtb2.MarkupVideo: | ||
| return openrtb_ext.BidTypeVideo, nil | ||
| case openrtb2.MarkupAudio: | ||
| return openrtb_ext.BidTypeAudio, nil | ||
| case openrtb2.MarkupNative: | ||
| return openrtb_ext.BidTypeNative, nil | ||
| default: | ||
| return "", &errortypes.BadServerResponse{ | ||
| Message: fmt.Sprintf("unsupported MType %d", bid.MType), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func resolveMacros(bid *openrtb2.Bid) { | ||
| if bid == nil { | ||
| return | ||
| } | ||
| price := strconv.FormatFloat(bid.Price, 'f', -1, 64) | ||
| bid.AdM = strings.ReplaceAll(bid.AdM, "${AUCTION_PRICE}", price) | ||
| bid.NURL = strings.ReplaceAll(bid.NURL, "${AUCTION_PRICE}", price) | ||
| bid.BURL = strings.ReplaceAll(bid.BURL, "${AUCTION_PRICE}", price) | ||
| } | ||
|
|
||
| func getNativeAdm(adm string) (string, error) { | ||
| value, dataType, _, err := jsonparser.Get([]byte(adm), string(openrtb_ext.BidTypeNative)) | ||
| if err != nil { | ||
| if errors.Is(err, jsonparser.KeyPathNotFoundError) { | ||
| return adm, nil | ||
| } | ||
| return adm, errors.New("unable to unmarshal native adm") | ||
| } | ||
| if dataType != jsonparser.Object { | ||
| return adm, errors.New("unable to get native adm") | ||
| } | ||
| return string(value), nil | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.