-
Notifications
You must be signed in to change notification settings - Fork 901
New Adapter: TRUSTX (remove Grid alias) #4614
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
Changes from all commits
2b6b83f
058ffe5
5cc01b6
8d6c85b
40489ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package trustx | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/prebid/prebid-server/v3/openrtb_ext" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // This file actually intends to test static/bidder-params/trustx.json | ||
| // | ||
| // These also validate the format of the external API: request.imp[i].ext.prebid.bidder.trustx | ||
|
|
||
| // TestValidParams makes sure that the trustx schema accepts all imp.ext fields which we intend to support. | ||
| func TestValidParams(t *testing.T) { | ||
| validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params") | ||
| require.NoError(t, err, "Failed to fetch the json-schemas") | ||
|
|
||
| for _, validParam := range validParams { | ||
| err := validator.Validate(openrtb_ext.BidderTrustX, json.RawMessage(validParam)) | ||
| require.NoError(t, err, "Schema rejected trustx params: %s", validParam) | ||
| } | ||
| } | ||
|
|
||
| // TestInvalidParams makes sure that the trustx schema rejects all the imp.ext fields we don't support. | ||
| func TestInvalidParams(t *testing.T) { | ||
| validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params") | ||
| require.NoError(t, err, "Failed to fetch the json-schemas") | ||
|
|
||
| for _, invalidParam := range invalidParams { | ||
| err := validator.Validate(openrtb_ext.BidderTrustX, json.RawMessage(invalidParam)) | ||
| require.Error(t, err, "Schema allowed unexpected params: %s", invalidParam) | ||
| } | ||
| } | ||
|
|
||
| var validParams = []string{ | ||
| `{}`, | ||
| `{"uid": 1234}`, | ||
| `{"uid": 1234, "keywords":{"site": {}, "user": {}}}`, | ||
| } | ||
| var invalidParams = []string{ | ||
|
Contributor
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. Can we add other test cases where we also check data types of Uid and Keywords?
Contributor
Author
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. Added 2 test cases. |
||
| ``, | ||
| `null`, | ||
| `true`, | ||
| `5`, | ||
| `4.2`, | ||
| `[]`, | ||
| `{"uid": "invalid_type"}`, | ||
| `{"keywords": "invalid_type"}`, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| package trustx | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/prebid/openrtb/v20/openrtb2" | ||
| "github.com/prebid/prebid-server/v3/adapters" | ||
| "github.com/prebid/prebid-server/v3/config" | ||
| "github.com/prebid/prebid-server/v3/errortypes" | ||
| "github.com/prebid/prebid-server/v3/openrtb_ext" | ||
| "github.com/prebid/prebid-server/v3/util/jsonutil" | ||
| ) | ||
|
|
||
| type impExt struct { | ||
| Prebid *openrtb_ext.ExtImpPrebid `json:"prebid,omitempty"` | ||
| Bidder json.RawMessage `json:"bidder"` | ||
| Data *impExtData `json:"data,omitempty"` | ||
| Gpid string `json:"gpid,omitempty"` | ||
| } | ||
|
|
||
| type impExtData struct { | ||
| PbAdslot string `json:"pbadslot,omitempty"` | ||
| AdServer *impExtDataAdServer `json:"adserver,omitempty"` | ||
| } | ||
|
|
||
| type impExtDataAdServer struct { | ||
| Name string `json:"name,omitempty"` | ||
| AdSlot string `json:"adslot,omitempty"` | ||
| } | ||
|
|
||
| type bidExt struct { | ||
| Bidder bidExtBidder `json:"bidder,omitempty"` | ||
| } | ||
|
|
||
| type bidExtBidder struct { | ||
| TrustX bidExtBidderTrustX `json:"trustx,omitempty"` | ||
| } | ||
|
|
||
| type bidExtBidderTrustX struct { | ||
| NetworkName string `json:"networkName,omitempty"` | ||
| } | ||
|
|
||
| type adapter struct { | ||
| endpoint string | ||
| } | ||
|
|
||
| // Builder builds a new instance of the TRUSTX adapter for the given bidder with the given config. | ||
| 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, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
| var errs []error | ||
| for i := range request.Imp { | ||
| err := setImpExtData(&request.Imp[i]) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| return nil, errs | ||
| } | ||
| } | ||
|
|
||
| body, err := jsonutil.Marshal(request) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| return nil, errs | ||
| } | ||
|
|
||
| reqs := make([]*adapters.RequestData, 0, 1) | ||
| requestData := &adapters.RequestData{ | ||
| Method: http.MethodPost, | ||
| Uri: a.endpoint, | ||
| Body: body, | ||
| Headers: getHeaders(request), | ||
| ImpIDs: openrtb_ext.GetImpIDs(request.Imp), | ||
| } | ||
| reqs = append(reqs, requestData) | ||
|
|
||
| return reqs, errs | ||
| } | ||
|
|
||
| func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
| if adapters.IsResponseStatusCodeNoContent(response) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if err := adapters.CheckResponseStatusCodeForErrors(response); err != nil { | ||
| return nil, []error{err} | ||
| } | ||
|
|
||
| var resp openrtb2.BidResponse | ||
| if err := jsonutil.Unmarshal(response.Body, &resp); err != nil { | ||
| return nil, []error{&errortypes.BadServerResponse{ | ||
| Message: "Bad Server Response", | ||
| }} | ||
| } | ||
|
|
||
| var bidErrors []error | ||
| bidderResponse := adapters.NewBidderResponseWithBidsCapacity(1) | ||
| for i := range resp.SeatBid { | ||
| seatBid := &resp.SeatBid[i] | ||
| for j := range seatBid.Bid { | ||
| bid := &seatBid.Bid[j] | ||
| typedBid, err := getTypedBid(bid) | ||
| if err != nil { | ||
| bidErrors = append(bidErrors, err) | ||
| continue | ||
| } | ||
| bidderResponse.Bids = append(bidderResponse.Bids, typedBid) | ||
| } | ||
| } | ||
|
|
||
| return bidderResponse, bidErrors | ||
| } | ||
|
|
||
| func setImpExtData(imp *openrtb2.Imp) error { | ||
| var ext impExt | ||
| if err := jsonutil.Unmarshal(imp.Ext, &ext); err != nil { | ||
| //if unmarshalling fails, proceed with the request | ||
| return nil | ||
| } | ||
| if ext.Data != nil && ext.Data.AdServer != nil && ext.Data.AdServer.AdSlot != "" { | ||
| ext.Gpid = ext.Data.AdServer.AdSlot | ||
| extJSON, err := jsonutil.Marshal(ext) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
Comment on lines
+128
to
+131
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. We are ok with swallowing the unmarshalling error above in this function but please do not swallow marshaling errors. We've seen instances in production where shared memory corruption results in a marshalling error causing an adapter panic that tips us off that there is a real problem.
Contributor
Author
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. Changed marshalling error handling |
||
| imp.Ext = extJSON | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func getHeaders(request *openrtb2.BidRequest) http.Header { | ||
| headers := http.Header{} | ||
| headers.Set("Content-Type", "application/json;charset=utf-8") | ||
| headers.Set("Accept", "application/json") | ||
| headers.Set("X-Openrtb-Version", "2.6") | ||
|
|
||
| if request.Site != nil { | ||
| if request.Site.Ref != "" { | ||
| headers.Set("Referer", request.Site.Ref) | ||
| } | ||
| if request.Site.Domain != "" { | ||
| headers.Set("Origin", request.Site.Domain) | ||
| } | ||
| } | ||
|
|
||
| if request.Device != nil { | ||
| if len(request.Device.IP) > 0 { | ||
| headers.Set("X-Forwarded-For", request.Device.IP) | ||
| } | ||
|
|
||
| if len(request.Device.IPv6) > 0 { | ||
| headers.Set("X-Forwarded-For", request.Device.IPv6) | ||
| } | ||
|
|
||
| if len(request.Device.UA) > 0 { | ||
| headers.Set("User-Agent", request.Device.UA) | ||
| } | ||
| } | ||
|
|
||
| return headers | ||
| } | ||
|
|
||
| func getTypedBid(bid *openrtb2.Bid) (*adapters.TypedBid, error) { | ||
| var bidType openrtb_ext.BidType | ||
| switch bid.MType { | ||
| case openrtb2.MarkupBanner: | ||
| bidType = openrtb_ext.BidTypeBanner | ||
| case openrtb2.MarkupVideo: | ||
| bidType = openrtb_ext.BidTypeVideo | ||
| default: | ||
| return nil, &errortypes.BadServerResponse{ | ||
| Message: fmt.Sprintf("Unsupported MType: %v", bid.MType), | ||
| } | ||
| } | ||
|
|
||
| var extBidPrebidVideo *openrtb_ext.ExtBidPrebidVideo | ||
| if bidType == openrtb_ext.BidTypeVideo { | ||
| extBidPrebidVideo = &openrtb_ext.ExtBidPrebidVideo{} | ||
| if len(bid.Cat) > 0 { | ||
| extBidPrebidVideo.PrimaryCategory = bid.Cat[0] | ||
| } | ||
| if bid.Dur > 0 { | ||
| extBidPrebidVideo.Duration = int(bid.Dur) | ||
| } | ||
| } | ||
| return &adapters.TypedBid{ | ||
| Bid: bid, | ||
| BidType: bidType, | ||
| BidVideo: extBidPrebidVideo, | ||
| BidMeta: getBidMeta(bid.Ext), | ||
| }, nil | ||
| } | ||
|
|
||
| func getBidMeta(ext json.RawMessage) *openrtb_ext.ExtBidPrebidMeta { | ||
| if ext == nil { | ||
| return nil | ||
| } | ||
| var be bidExt | ||
|
|
||
| if err := jsonutil.Unmarshal(ext, &be); err != nil { | ||
| return nil | ||
| } | ||
| var bidMeta *openrtb_ext.ExtBidPrebidMeta | ||
| if be.Bidder.TrustX.NetworkName != "" { | ||
|
Contributor
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. Can there be a case where TrustX is empty? Won't this logic break in such cases?
Contributor
Author
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. Yes, there can be a case where we don't return the network name, but |
||
| bidMeta = &openrtb_ext.ExtBidPrebidMeta{ | ||
| NetworkName: be.Bidder.TrustX.NetworkName, | ||
| } | ||
| } | ||
| return bidMeta | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package trustx | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/prebid/prebid-server/v3/adapters/adapterstest" | ||
| "github.com/prebid/prebid-server/v3/config" | ||
| "github.com/prebid/prebid-server/v3/openrtb_ext" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestJsonSamples(t *testing.T) { | ||
| bidder, buildErr := Builder(openrtb_ext.BidderTrustX, config.Adapter{ | ||
| Endpoint: "https://test.localhost.com"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||
|
|
||
| require.NoError(t, buildErr, "Builder returned unexpected error") | ||
| adapterstest.RunJSONBidderTest(t, "trustxtest", bidder) | ||
| } |
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.
Question: is a default Uid being 0 acceptable here? Is there a reason why we are not making Uid required?
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.
Hi, 0 is acceptable, as publishers can also pass the value in other fields (e.g.
imp.tagid)