-
Notifications
You must be signed in to change notification settings - Fork 930
New Adapter: Viant #4854
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
Open
KEPlockr
wants to merge
2
commits into
prebid:master
Choose a base branch
from
loc-kr:VIANT-S2S-ADAPTER
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
New Adapter: Viant #4854
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,46 @@ | ||
| package viant | ||
|
|
||
| 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.BidderViant, json.RawMessage(validParam)); err != nil { | ||
| t.Errorf("Schema rejected viant 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.BidderViant, json.RawMessage(invalidParam)); err == nil { | ||
| t.Errorf("Schema allowed unexpected params: %s", invalidParam) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var validParams = []string{ | ||
| `{"publisherId": "prebid-test-pub-001"}`, | ||
| `{"publisherId": "viant-pub-12345"}`, | ||
| `{"publisherId": "any-string"}`, | ||
| } | ||
|
|
||
| var invalidParams = []string{ | ||
| `{}`, | ||
| `{"publisherId": ""}`, | ||
| `{"publisherId": 123}`, | ||
| } |
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,251 @@ | ||
| package viant | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "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/openrtb_ext" | ||
| "github.com/prebid/prebid-server/v4/util/jsonutil" | ||
| ) | ||
|
|
||
| // defaultCurrency is the fallback Viant bids in and the target we convert | ||
| // unsupported bid floor currencies into. | ||
| const defaultCurrency = "USD" | ||
|
|
||
| // supportedCurrencies are the bid floor currencies Viant can interpret directly. | ||
| // Floors already in one of these are passed through untouched so Viant applies | ||
| // its own conversion rates; anything else is converted to defaultCurrency first. | ||
| // Adding a newly supported currency here is the only change needed to stop | ||
| // converting it. | ||
| var supportedCurrencies = map[string]struct{}{ | ||
| "USD": {}, | ||
| "GBP": {}, | ||
| "CAD": {}, | ||
| "EUR": {}, | ||
| "AUD": {}, | ||
| "SAR": {}, | ||
| "AED": {}, | ||
| } | ||
|
|
||
| type adapter struct { | ||
| endpoint string | ||
| } | ||
|
|
||
| func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { | ||
| bidder := &adapter{ | ||
| endpoint: config.Endpoint, | ||
| } | ||
| return bidder, nil | ||
| } | ||
|
|
||
| func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
| var errs []error | ||
|
|
||
| reqCopy := *request | ||
| cleanImps := make([]openrtb2.Imp, 0, len(request.Imp)) | ||
|
|
||
| for i := range request.Imp { | ||
| var impExt adapters.ExtImpBidder | ||
| if err := jsonutil.Unmarshal(request.Imp[i].Ext, &impExt); err != nil { | ||
| errs = append(errs, &errortypes.BadInput{ | ||
| Message: fmt.Sprintf("invalid imp.ext for impression index %d. %s", i, err.Error()), | ||
| }) | ||
| continue | ||
| } | ||
|
|
||
| var bidderExt openrtb_ext.ImpExtViant | ||
| if err := jsonutil.Unmarshal(impExt.Bidder, &bidderExt); err != nil { | ||
| errs = append(errs, &errortypes.BadInput{ | ||
| Message: fmt.Sprintf("invalid imp.ext.bidder for impression index %d. %s", i, err.Error()), | ||
| }) | ||
| continue | ||
| } | ||
|
|
||
| if bidderExt.PublisherID == "" { | ||
| errs = append(errs, &errortypes.BadInput{ | ||
| Message: fmt.Sprintf("imp.ext.bidder.publisherId is required for impression index %d", i), | ||
| }) | ||
| continue | ||
| } | ||
|
|
||
| imp := request.Imp[i] | ||
| imp.Ext = stripBidderExt(imp.Ext) | ||
|
|
||
| if imp.BidFloor > 0 && imp.BidFloorCur != "" { | ||
| if _, ok := supportedCurrencies[strings.ToUpper(imp.BidFloorCur)]; !ok { | ||
| convertedValue, err := requestInfo.ConvertCurrency(imp.BidFloor, imp.BidFloorCur, defaultCurrency) | ||
| if err != nil { | ||
| // Viant accepts requests in currencies it doesn't support without | ||
| // erroring and simply bids in USD. Mirror that leniency: if we can't | ||
| // express the floor in USD, drop the floor rather than the impression | ||
| // so Viant can still return a (USD) bid. | ||
| errs = append(errs, &errortypes.Warning{ | ||
| Message: fmt.Sprintf("dropping unconvertible bid floor for impression index %d: %s", i, err.Error()), | ||
| }) | ||
| imp.BidFloor = 0 | ||
| imp.BidFloorCur = "" | ||
| } else { | ||
| imp.BidFloorCur = defaultCurrency | ||
| imp.BidFloor = convertedValue | ||
| } | ||
| } | ||
| } | ||
|
|
||
| cleanImps = append(cleanImps, imp) | ||
| } | ||
|
|
||
| if len(cleanImps) == 0 { | ||
| return nil, append(errs, &errortypes.BadInput{ | ||
| Message: "no valid impressions in the bid request", | ||
| }) | ||
| } | ||
|
|
||
| reqCopy.Imp = cleanImps | ||
| // Viant prices its bids in any currency it supports, so pass a supported | ||
| // requested currency straight through and let it respond in that currency. | ||
| // For unsupported currencies (or none) ask for USD and let Prebid core | ||
| // convert the USD bid into the publisher's requested currency downstream. | ||
| reqCopy.Cur = []string{resolveRequestCurrency(request.Cur)} | ||
|
|
||
| requestJSON, err := json.Marshal(reqCopy) | ||
| if err != nil { | ||
| return nil, append(errs, err) | ||
| } | ||
|
|
||
| headers := http.Header{} | ||
| headers.Add("Content-Type", "application/json;charset=utf-8") | ||
| headers.Add("Accept", "application/json") | ||
|
|
||
| return []*adapters.RequestData{{ | ||
| Method: "POST", | ||
| Uri: a.endpoint, | ||
| Body: requestJSON, | ||
| Headers: headers, | ||
| ImpIDs: openrtb_ext.GetImpIDs(reqCopy.Imp), | ||
| }}, errs | ||
| } | ||
|
|
||
| // resolveRequestCurrency returns the first requested currency Viant supports so | ||
| // it can bid in it directly. If none of the requested currencies are supported | ||
| // (or none were requested), it returns defaultCurrency. | ||
| func resolveRequestCurrency(requestCurrencies []string) string { | ||
| for _, cur := range requestCurrencies { | ||
| if _, ok := supportedCurrencies[strings.ToUpper(cur)]; ok { | ||
| return cur | ||
| } | ||
| } | ||
| return defaultCurrency | ||
| } | ||
|
|
||
| // stripBidderExt removes the "bidder" and "prebid" keys from imp.ext, | ||
| // returning nil if nothing else remains. | ||
| func stripBidderExt(ext json.RawMessage) json.RawMessage { | ||
| if ext == nil { | ||
| return nil | ||
| } | ||
|
|
||
| var extMap map[string]json.RawMessage | ||
| if err := jsonutil.Unmarshal(ext, &extMap); err != nil { | ||
| return nil | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider returning the original if err := jsonutil.Unmarshal(ext, &extMap); err != nil {
return ext
}
Could you also add a supplemental fixture where imp.ext is not a JSON object, to document and test the current behavior? |
||
| } | ||
|
|
||
| delete(extMap, openrtb_ext.PrebidExtBidderKey) | ||
| delete(extMap, openrtb_ext.PrebidExtKey) | ||
|
|
||
| if len(extMap) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| cleaned, err := json.Marshal(extMap) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| return cleaned | ||
| } | ||
|
|
||
| func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
|
|
||
| if response.StatusCode == http.StatusNoContent { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if response.StatusCode == http.StatusBadRequest { | ||
| return nil, []error{&errortypes.BadInput{ | ||
| Message: fmt.Sprintf("unexpected status code: %d. Run with request.debug = 1 for more info.", response.StatusCode), | ||
| }} | ||
| } | ||
|
|
||
| if response.StatusCode == http.StatusServiceUnavailable { | ||
| return nil, []error{&errortypes.BadServerResponse{ | ||
| Message: fmt.Sprintf("service unavailable: HTTP status %d", response.StatusCode), | ||
| }} | ||
| } | ||
|
|
||
| if response.StatusCode != http.StatusOK { | ||
| return nil, []error{&errortypes.BadServerResponse{ | ||
| Message: fmt.Sprintf("unexpected status code: %d. Run with request.debug = 1 for more info.", response.StatusCode), | ||
| }} | ||
| } | ||
|
|
||
| if len(response.Body) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| var bidResponse openrtb2.BidResponse | ||
| if err := jsonutil.Unmarshal(response.Body, &bidResponse); err != nil { | ||
| return nil, []error{&errortypes.BadServerResponse{ | ||
| Message: fmt.Sprintf("JSON parsing error: %s", err.Error()), | ||
| }} | ||
| } | ||
|
|
||
| bidderResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) | ||
| // Viant bids in the requested currency when it supports it, so trust the | ||
| // currency it reports. Fall back to USD if the response omits it. | ||
| if bidResponse.Cur != "" { | ||
| bidderResponse.Currency = bidResponse.Cur | ||
| } else { | ||
| bidderResponse.Currency = defaultCurrency | ||
| } | ||
|
|
||
| var errs []error | ||
| for _, seatBid := range bidResponse.SeatBid { | ||
| for i := range seatBid.Bid { | ||
| bid := &seatBid.Bid[i] | ||
| bidType, err := getMediaTypeForBid(bid) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| continue | ||
| } | ||
|
|
||
| bidderResponse.Bids = append(bidderResponse.Bids, &adapters.TypedBid{ | ||
| Bid: bid, | ||
| BidType: bidType, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return bidderResponse, 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.MarkupNative: | ||
| return openrtb_ext.BidTypeNative, nil | ||
| case openrtb2.MarkupAudio: | ||
| return openrtb_ext.BidTypeAudio, nil | ||
| default: | ||
| return "", &errortypes.BadServerResponse{ | ||
| Message: fmt.Sprintf("unsupported MType %d for bid %s", bid.MType, bid.ImpID), | ||
| } | ||
| } | ||
| } | ||
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,21 @@ | ||
| package viant | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/prebid/prebid-server/v4/adapters/adapterstest" | ||
| "github.com/prebid/prebid-server/v4/config" | ||
| "github.com/prebid/prebid-server/v4/openrtb_ext" | ||
| ) | ||
|
|
||
| func TestJsonSamples(t *testing.T) { | ||
| bidder, buildErr := Builder(openrtb_ext.BidderViant, config.Adapter{ | ||
| Endpoint: "https://bidders-us.adelphic.net/rtb/v25/viant-prebid-server/bidder", | ||
| }, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||
|
|
||
| if buildErr != nil { | ||
| t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
| } | ||
|
|
||
| adapterstest.RunJSONBidderTest(t, "vianttest", bidder) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function reduces request.cur from a list to a single supported value. The behavior is non-obvious — OpenRTB allows multiple currencies, but here the request always goes out with exactly one. Please add a comment explaining why Viant requires a single currency in the outgoing request.
Also, when all currencies in request.cur are unsupported (e.g. ["JPY", "CNY"]), the function silently falls back to "USD". A supplemental fixture for this case would make the fallback behavior explicit and tested.