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.
Added new adapter for CPMStar ad network banners and video (prebid#1159)
- Loading branch information
1 parent
c62420f
commit b3b7869
Showing
26 changed files
with
1,061 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,161 @@ | ||
package cpmstar | ||
|
||
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 Adapter struct { | ||
endpoint string | ||
} | ||
|
||
func (a *Adapter) MakeRequests(request *openrtb.BidRequest, unused *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
var errs []error | ||
var adapterRequests []*adapters.RequestData | ||
|
||
if err := preprocess(request); err != nil { | ||
errs = append(errs, err) | ||
return nil, errs | ||
} | ||
|
||
adapterReq, err := a.makeRequest(request) | ||
if err != nil { | ||
errs = append(errs, err) | ||
return nil, errs | ||
} | ||
|
||
adapterRequests = append(adapterRequests, adapterReq) | ||
|
||
return adapterRequests, errs | ||
} | ||
|
||
func (a *Adapter) makeRequest(request *openrtb.BidRequest) (*adapters.RequestData, error) { | ||
var err error | ||
|
||
jsonBody, err := json.Marshal(request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
|
||
return &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: a.endpoint, | ||
Body: jsonBody, | ||
Headers: headers, | ||
}, nil | ||
} | ||
|
||
func preprocess(request *openrtb.BidRequest) error { | ||
if len(request.Imp) == 0 { | ||
return &errortypes.BadInput{ | ||
Message: "No Imps in Bid Request", | ||
} | ||
} | ||
for i := 0; i < len(request.Imp); i++ { | ||
var imp = &request.Imp[i] | ||
var bidderExt adapters.ExtImpBidder | ||
|
||
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { | ||
return &errortypes.BadInput{ | ||
Message: err.Error(), | ||
} | ||
} | ||
|
||
if err := validateImp(imp); err != nil { | ||
return err | ||
} | ||
|
||
var extImp openrtb_ext.ExtImpCpmstar | ||
if err := json.Unmarshal(bidderExt.Bidder, &extImp); err != nil { | ||
return &errortypes.BadInput{ | ||
Message: err.Error(), | ||
} | ||
} | ||
|
||
imp.Ext = bidderExt.Bidder | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateImp(imp *openrtb.Imp) error { | ||
if imp.Banner == nil && imp.Video == nil { | ||
return &errortypes.BadInput{ | ||
Message: "Only Banner and Video bid-types are supported at this time", | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// MakeBids based on cpmstar server response | ||
func (a *Adapter) MakeBids(bidRequest *openrtb.BidRequest, unused *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
if responseData.StatusCode == http.StatusNoContent { | ||
return nil, nil | ||
} | ||
|
||
if responseData.StatusCode != http.StatusOK { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Unexpected HTTP status code: %d. Run with request.debug = 1 for more info", responseData.StatusCode), | ||
}} | ||
} | ||
|
||
var bidResponse openrtb.BidResponse | ||
|
||
if err := json.Unmarshal(responseData.Body, &bidResponse); err != nil { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: err.Error(), | ||
}} | ||
} | ||
|
||
if len(bidResponse.SeatBid) == 0 { | ||
return nil, nil | ||
} | ||
|
||
rv := adapters.NewBidderResponseWithBidsCapacity(len(bidResponse.SeatBid[0].Bid)) | ||
var errors []error | ||
|
||
for _, seatbid := range bidResponse.SeatBid { | ||
for _, bid := range seatbid.Bid { | ||
foundMatchingBid := false | ||
bidType := openrtb_ext.BidTypeBanner | ||
for _, imp := range bidRequest.Imp { | ||
if imp.ID == bid.ImpID { | ||
foundMatchingBid = true | ||
if imp.Banner != nil { | ||
bidType = openrtb_ext.BidTypeBanner | ||
} else if imp.Video != nil { | ||
bidType = openrtb_ext.BidTypeVideo | ||
} | ||
break | ||
} | ||
} | ||
|
||
if foundMatchingBid { | ||
rv.Bids = append(rv.Bids, &adapters.TypedBid{ | ||
Bid: &bid, | ||
BidType: bidType, | ||
}) | ||
} else { | ||
errors = append(errors, &errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("bid id='%s' could not find valid impid='%s'", bid.ID, bid.ImpID), | ||
}) | ||
} | ||
} | ||
} | ||
return rv, errors | ||
} | ||
|
||
func NewCpmstarBidder(endpoint string) *Adapter { | ||
return &Adapter{ | ||
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,11 @@ | ||
package cpmstar | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/adapters/adapterstest" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
adapterstest.RunJSONBidderTest(t, "cpmstartest", NewCpmstarBidder("//host")) | ||
} |
154 changes: 154 additions & 0 deletions
154
adapters/cpmstar/cpmstartest/exemplary/banner-and-video.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,154 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-banner-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"placementId": 154, | ||
"subpoolId": 123 | ||
} | ||
} | ||
}, | ||
{ | ||
"id": "test-video-imp-id", | ||
"video": { | ||
"mimes": [ | ||
"video/mp4" | ||
], | ||
"protocols": [ | ||
2, | ||
5 | ||
], | ||
"w": 640, | ||
"h": 480 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"placementId": 154, | ||
"subpoolId": 654 | ||
} | ||
} | ||
} | ||
], | ||
"site": { | ||
"id": "fake-site-id" | ||
} | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "//host", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-banner-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"h": 250, | ||
"w": 300 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"placementId": 154, | ||
"subpoolId": 123 | ||
} | ||
}, | ||
{ | ||
"id": "test-video-imp-id", | ||
"video": { | ||
"h": 480, | ||
"mimes": [ | ||
"video/mp4" | ||
], | ||
"protocols": [ | ||
2, | ||
5 | ||
], | ||
"w": 640 | ||
}, | ||
"ext": { | ||
"placementId": 154, | ||
"subpoolId": 654 | ||
} | ||
} | ||
], | ||
"site": { | ||
"id": "fake-site-id" | ||
} | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [ | ||
{ | ||
"seat": "cpmstar", | ||
"bid": [ | ||
{ | ||
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800", | ||
"impid": "test-video-imp-id", | ||
"price": 0.5, | ||
"adm": "some-test-ad", | ||
"crid": "crid_10", | ||
"h": 250, | ||
"w": 300 | ||
} | ||
] | ||
} | ||
], | ||
"cur": "USD" | ||
} | ||
} | ||
} | ||
], | ||
"expectedBids": [ | ||
{ | ||
"bid": { | ||
"id": "7706636740145184841", | ||
"impid": "test-imp-id", | ||
"price": 0.5, | ||
"adm": "some-test-ad", | ||
"adid": "29681110", | ||
"adomain": [ | ||
"sample.com" | ||
], | ||
"cid": "958", | ||
"crid": "29681110", | ||
"w": 1024, | ||
"h": 576 | ||
}, | ||
"type": "banner" | ||
}, | ||
{ | ||
"bid": { | ||
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800", | ||
"impid": "test-imp-video-id", | ||
"price": 0.5, | ||
"adm": "some-test-ad", | ||
"adid": "29484110", | ||
"adomain": [ | ||
"sample.com" | ||
], | ||
"cid": "958", | ||
"crid": "29484110", | ||
"w": 1024, | ||
"h": 576 | ||
}, | ||
"type": "video" | ||
} | ||
] | ||
} |
Oops, something went wrong.