-
Notifications
You must be signed in to change notification settings - Fork 765
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32fdbc4
commit 0250830
Showing
19 changed files
with
1,819 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,65 @@ | ||
package thetradedesk | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/v2/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.BidderTheTradeDesk, json.RawMessage(validParam)); err != nil { | ||
t.Errorf("Schema rejected TheTradeDesk params: %s", validParam) | ||
} | ||
} | ||
} | ||
|
||
// TestInvalidParams makes sure that the TheTradeDesk schema rejects all the imp.ext fields we don't support. | ||
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.BidderTheTradeDesk, json.RawMessage(invalidParam)); err == nil { | ||
t.Errorf("Schema allowed unexpected params: %s", invalidParam) | ||
} | ||
} | ||
} | ||
|
||
var validParams = []string{ | ||
`{"zoneid": 123456}`, | ||
`{"zoneId": 123456}`, | ||
`{"networkid": 78910}`, | ||
`{"networkId": 78910}`, | ||
`{"zoneid": 123456, "networkid": 78910}`, | ||
`{"zoneId": 123456, "networkId": 78910}`, | ||
`{"zoneid": 0, "networkid": 0}`, | ||
`{"zoneId": 0, "networkId": 0}`, | ||
} | ||
|
||
var invalidParams = []string{ | ||
``, | ||
`null`, | ||
`true`, | ||
`5`, | ||
`4.2`, | ||
`[]`, | ||
`{}`, | ||
`{"zoneid": -123}`, | ||
`{"zoneId": -123}`, | ||
`{"networkid": -321}`, | ||
`{"networkId": -321}`, | ||
`{"zoneid": -123, "networkid": -321}`, | ||
`{"zoneId": -123, "networkId": -321}`, | ||
`{"zoneid": -1}`, | ||
`{"networkid": -1}`, | ||
`{"zoneid": -1, "networkid": -1}`, | ||
} |
This file contains 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,157 @@ | ||
package thetradedesk | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/prebid/prebid-server/v2/adapters" | ||
"github.com/prebid/prebid-server/v2/config" | ||
"github.com/prebid/prebid-server/v2/errortypes" | ||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
|
||
"github.com/prebid/openrtb/v20/openrtb2" | ||
) | ||
|
||
type adapter struct { | ||
URI string | ||
bidderName string | ||
} | ||
|
||
type ExtImpBidderTheTradeDesk struct { | ||
adapters.ExtImpBidder | ||
} | ||
|
||
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
pubID, err := getPublisherId(request.Imp) | ||
|
||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
if request.Site != nil { | ||
if request.Site.Publisher != nil { | ||
request.Site.Publisher.ID = pubID | ||
} else { | ||
request.Site.Publisher = &openrtb2.Publisher{ID: pubID} | ||
} | ||
} else if request.App != nil { | ||
if request.App.Publisher != nil { | ||
request.App.Publisher.ID = pubID | ||
} else { | ||
request.App.Publisher = &openrtb2.Publisher{ID: pubID} | ||
} | ||
} | ||
|
||
var bidderEndpoint = a.URI + "/" + a.bidderName | ||
|
||
errs := make([]error, 0, len(request.Imp)) | ||
reqJSON, err := json.Marshal(request) | ||
if err != nil { | ||
errs = append(errs, err) | ||
return nil, errs | ||
} | ||
|
||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
headers.Add("Accept", "application/json") | ||
return []*adapters.RequestData{{ | ||
Method: "POST", | ||
Uri: bidderEndpoint, | ||
Body: reqJSON, | ||
Headers: headers, | ||
ImpIDs: openrtb_ext.GetImpIDs(request.Imp), | ||
}}, errs | ||
} | ||
|
||
func getPublisherId(impressions []openrtb2.Imp) (string, error) { | ||
pubID := "" | ||
for i := 0; i < len(impressions); i++ { | ||
var imp = &impressions[i] | ||
|
||
var bidderExt ExtImpBidderTheTradeDesk | ||
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { | ||
return "", err | ||
} | ||
|
||
var ttdExt openrtb_ext.ExtImpTheTradeDesk | ||
if err := json.Unmarshal(bidderExt.Bidder, &ttdExt); err != nil { | ||
return "", err | ||
} | ||
|
||
if ttdExt.PublisherId != "" { | ||
pubID = ttdExt.PublisherId | ||
} | ||
} | ||
return pubID, nil | ||
} | ||
|
||
func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
if response.StatusCode == http.StatusNoContent { | ||
return nil, nil | ||
} | ||
|
||
if response.StatusCode == http.StatusBadRequest { | ||
err := &errortypes.BadInput{ | ||
Message: "Unexpected status code: 400. Run with request.debug = 1 for more info.", | ||
} | ||
return nil, []error{err} | ||
} | ||
|
||
if response.StatusCode != http.StatusOK { | ||
err := &errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", response.StatusCode), | ||
} | ||
return nil, []error{err} | ||
} | ||
|
||
var bidResponse openrtb2.BidResponse | ||
if err := json.Unmarshal(response.Body, &bidResponse); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
bidderResponse := adapters.NewBidderResponse() | ||
bidderResponse.Currency = bidResponse.Cur | ||
|
||
for _, seatBid := range bidResponse.SeatBid { | ||
for _, bid := range seatBid.Bid { | ||
bid := bid | ||
|
||
bidType, err := getBidType(bid) | ||
|
||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
b := &adapters.TypedBid{ | ||
Bid: &bid, | ||
BidType: bidType, | ||
} | ||
bidderResponse.Bids = append(bidderResponse.Bids, b) | ||
} | ||
} | ||
|
||
return bidderResponse, nil | ||
} | ||
|
||
func getBidType(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 "", fmt.Errorf("unsupported mtype: %d", bid.MType) | ||
} | ||
} | ||
|
||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { | ||
return &adapter{ | ||
URI: config.Endpoint, | ||
bidderName: string(bidderName), | ||
}, nil | ||
} |
131 changes: 131 additions & 0 deletions
131
adapters/thetradedesk/thetradedesk/exemplary/simple-banner-cookie-uid.json
This file contains 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,131 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"site": { | ||
"id": "site-id", | ||
"page": "ttd.com" | ||
}, | ||
"device": { | ||
"os": "android", | ||
"ip": "91.199.242.236", | ||
"ua": "random user agent" | ||
}, | ||
"user": { | ||
"buyeruid": "ttd-user-id" | ||
}, | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"zoneid": 123456, | ||
"networkid": 78910 | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "https://direct.adsrvr.org/bid/bidder/ttd", | ||
"headers": {}, | ||
"body": { | ||
"id": "test-request-id", | ||
"site": { | ||
"id": "site-id", | ||
"page": "ttd.com" | ||
}, | ||
"device": { | ||
"os": "android", | ||
"ip": "91.199.242.236", | ||
"ua": "random user agent" | ||
}, | ||
"user": { | ||
"buyeruid": "ttd-user-id" | ||
}, | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"zoneid": 123456, | ||
"networkid": 78910 | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"impIDs":["test-imp-id"] | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"currency": "USD", | ||
"seatbid": [ | ||
{ | ||
"bid": [ | ||
{ | ||
"id": "test-slot-id", | ||
"impid": "test-imp-id", | ||
"price": 0.1, | ||
"crid": "creative-123", | ||
"adm": "<iframe id='789abc' name='789abc' src='http://creative-url.ttd.com'></iframe>", | ||
"w": 300, | ||
"h": 250, | ||
"ext": { | ||
"prebid": { | ||
"type": "banner" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "test-slot-id", | ||
"impid": "test-imp-id", | ||
"price": 0.1, | ||
"crid": "creative-123", | ||
"adm": "<iframe id='789abc' name='789abc' src='http://creative-url.ttd.com'></iframe>", | ||
"w": 300, | ||
"h": 250, | ||
"ext": { | ||
"prebid": { | ||
"type": "banner" | ||
} | ||
} | ||
}, | ||
"type": "banner" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
Oops, something went wrong.