This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TripleLift Prebid S2S Adapter (prebid#954)
* ignore swp files * start small * start really small * add a user sync * justify * triplelift adapter * add our endpoint * fix syntax * config stuff * compiler fixes * more config * add params * making progress * make our ext more exty * start making responses * more logic * fix compilation errors * can we just nil this out? * augment our json * radically simplify our json * fix errs * infer the bid type * fix syntax * fix comilation errors * rename * fix compilation error * config stuff * simplify params * more config stuff * fixes * revert this * fix up the extension * getting closer * add a test * update config * update bidder params * add the floor here, too * add a usersync test * validation, ws, and a test * update tests * fix test * update email * why not * change email * preprocess requests * do some parsing * take care of some errors * floor is optional * ws * remove native * everything is either banner or video * this should be a float * floor to floor * fix compilation errors * add some tests * more tests * more tests * simplify * more progress * format * ws * rm * don't need this * fix test * fix test * don't ignore swap * change line back * report an error if there are no valid impressions for triplelift * check for either a Banner or Video object on the impression * more tests * mv * more tests
- Loading branch information
1 parent
5e29d2c
commit 4a12478
Showing
21 changed files
with
908 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,147 @@ | ||
package triplelift | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/prebid/prebid-server/errortypes" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
) | ||
|
||
type TripleliftAdapter struct { | ||
endpoint string | ||
} | ||
|
||
type TripleliftInnerExt struct { | ||
Format int `json:"format"` | ||
} | ||
|
||
type TripleliftRespExt struct { | ||
Triplelift TripleliftInnerExt `json:"triplelift_pb"` | ||
} | ||
|
||
func getBidType(ext TripleliftRespExt) openrtb_ext.BidType { | ||
t := ext.Triplelift.Format | ||
if t == 11 { | ||
return openrtb_ext.BidTypeVideo | ||
} | ||
return openrtb_ext.BidTypeBanner | ||
} | ||
|
||
func processImp(imp *openrtb.Imp) error { | ||
// get the triplelift extension | ||
var ext adapters.ExtImpBidder | ||
var tlext openrtb_ext.ExtImpTriplelift | ||
if err := json.Unmarshal(imp.Ext, &ext); err != nil { | ||
return err | ||
} | ||
if err := json.Unmarshal(ext.Bidder, &tlext); err != nil { | ||
return err | ||
} | ||
if imp.Banner == nil && imp.Video == nil { | ||
return fmt.Errorf("neither Banner nor Video object specified") | ||
} | ||
imp.TagID = tlext.InvCode | ||
// floor is optional | ||
if tlext.Floor == nil { | ||
return nil | ||
} else { | ||
imp.BidFloor = *tlext.Floor | ||
} | ||
// no error | ||
return nil | ||
} | ||
|
||
func (a *TripleliftAdapter) MakeRequests(request *openrtb.BidRequest, extra *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
errs := make([]error, 0, len(request.Imp)+1) | ||
reqs := make([]*adapters.RequestData, 0, 1) | ||
// copy the request, because we are going to mutate it | ||
tlRequest := *request | ||
// this will contain all the valid impressions | ||
var validImps []openrtb.Imp | ||
// pre-process the imps | ||
for _, imp := range tlRequest.Imp { | ||
if err := processImp(&imp); err == nil { | ||
validImps = append(validImps, imp) | ||
} else { | ||
errs = append(errs, err) | ||
} | ||
} | ||
if len(validImps) == 0 { | ||
err := fmt.Errorf("No valid impressions for triplelift") | ||
errs = append(errs, err) | ||
return nil, errs | ||
} | ||
tlRequest.Imp = validImps | ||
reqJSON, err := json.Marshal(tlRequest) | ||
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") | ||
ad := a.endpoint | ||
reqs = append(reqs, &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: ad, | ||
Body: reqJSON, | ||
Headers: headers}) | ||
return reqs, errs | ||
} | ||
|
||
func getBidCount(bidResponse openrtb.BidResponse) int { | ||
c := 0 | ||
for _, sb := range bidResponse.SeatBid { | ||
c = c + len(sb.Bid) | ||
} | ||
return c | ||
} | ||
|
||
func (a *TripleliftAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *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.StatusOK { | ||
return nil, []error{fmt.Errorf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode)} | ||
} | ||
var bidResp openrtb.BidResponse | ||
if err := json.Unmarshal(response.Body, &bidResp); err != nil { | ||
return nil, []error{err} | ||
} | ||
var errs []error | ||
count := getBidCount(bidResp) | ||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(count) | ||
|
||
for _, sb := range bidResp.SeatBid { | ||
for i := 0; i < len(sb.Bid); i++ { | ||
bid := sb.Bid[i] | ||
var bidExt TripleliftRespExt | ||
if err := json.Unmarshal(bid.Ext, &bidExt); err != nil { | ||
errs = append(errs, err) | ||
} else { | ||
bidType := getBidType(bidExt) | ||
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
Bid: &bid, | ||
BidType: bidType, | ||
}) | ||
} | ||
} | ||
} | ||
return bidResponse, errs | ||
} | ||
|
||
func NewTripleliftBidder(client *http.Client, endpoint string) *TripleliftAdapter { | ||
return &TripleliftAdapter{ | ||
endpoint: endpoint} | ||
} |
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,10 @@ | ||
package triplelift | ||
|
||
import ( | ||
"github.com/prebid/prebid-server/adapters/adapterstest" | ||
"testing" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
adapterstest.RunJSONBidderTest(t, "triplelifttest", NewTripleliftBidder(nil, "http://tlx.3lift.net/s2s/auction?supplier_id=19")) | ||
} |
66 changes: 66 additions & 0 deletions
66
adapters/triplelift/triplelifttest/exemplary/optional-params.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,66 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
{ | ||
"w": 300, | ||
"h": 600 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"inventoryCode": "foo", | ||
"floor" : 20 | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://tlx.3lift.net/s2s/auction?supplier_id=19", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
{ | ||
"w": 300, | ||
"h": 600 | ||
} | ||
] | ||
}, | ||
"tagid": "foo", | ||
"bidfloor": 20, | ||
"ext": { | ||
"bidder": { | ||
"inventoryCode": "foo", | ||
"floor" : 20 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 204 | ||
} | ||
} | ||
] | ||
} |
125 changes: 125 additions & 0 deletions
125
adapters/triplelift/triplelifttest/exemplary/simple-banner.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,125 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
{ | ||
"w": 300, | ||
"h": 600 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"inventoryCode": "aa" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://tlx.3lift.net/s2s/auction?supplier_id=19", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
{ | ||
"w": 300, | ||
"h": 600 | ||
} | ||
] | ||
}, | ||
"tagid" : "aa", | ||
"ext": { | ||
"bidder": { | ||
"inventoryCode": "aa" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [ | ||
{ | ||
"seat": "958", | ||
"bid": [ | ||
{ | ||
"id": "7706636740145184841", | ||
"impid": "test-imp-id", | ||
"price": 0.5, | ||
"adid": "29681110", | ||
"adm": "some-test-ad", | ||
"adomain": [ | ||
"triplelift.com" | ||
], | ||
"iurl": "http://nym1-ib.adnxs.com/cr?id=29681110", | ||
"cid": "958", | ||
"crid": "29681110", | ||
"h": 250, | ||
"w": 300, | ||
"ext": { | ||
"triplelift_pb": { | ||
"format" : 2 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"bidid": "5778926625248726496", | ||
"cur": "USD" | ||
} | ||
} | ||
} | ||
], | ||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "7706636740145184841", | ||
"impid": "test-imp-id", | ||
"price": 0.5, | ||
"adm": "some-test-ad", | ||
"adid": "29681110", | ||
"adomain": [ | ||
"triplelift.com" | ||
], | ||
"iurl": "http://nym1-ib.adnxs.com/cr?id=29681110", | ||
"cid": "958", | ||
"crid": "29681110", | ||
"w": 300, | ||
"h": 250, | ||
"ext": { | ||
"triplelift_pb": { | ||
"format" : 2 | ||
} | ||
} | ||
}, | ||
"type": "banner" | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.