Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions adapters/viant/params_test.go
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}`,
}
264 changes: 264 additions & 0 deletions adapters/viant/viant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
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 {

Copy link
Copy Markdown
Collaborator

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.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider returning the original ext on unmarshal failure instead:

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
}

for _, seatBid := range bidResponse.SeatBid {
for i := range seatBid.Bid {
bid := &seatBid.Bid[i]
bidType, err := getMediaTypeForBid(bid, request.Imp)
if err != nil {
continue
}

bidderResponse.Bids = append(bidderResponse.Bids, &adapters.TypedBid{
Bid: bid,
BidType: bidType,
})
}
}

return bidderResponse, nil
}

func getMediaTypeForBid(bid *openrtb2.Bid, imps []openrtb2.Imp) (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
}

for _, imp := range imps {
if imp.ID == bid.ImpID {
if imp.Banner != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeBanner, nil. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression.

return openrtb_ext.BidTypeBanner, nil
}
if imp.Video != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeVideo, nil. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression.

return openrtb_ext.BidTypeVideo, nil
}
if imp.Native != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeNative, nil. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression.

return openrtb_ext.BidTypeNative, nil
}
if imp.Audio != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeAudio, nil. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression.

return openrtb_ext.BidTypeAudio, nil
}
}
}

return "", fmt.Errorf("unable to determine media type for bid %s", bid.ID)
}
21 changes: 21 additions & 0 deletions adapters/viant/viant_test.go
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)
}
Loading
Loading