Skip to content
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

New Adapter: BidsCube #1843

Merged
merged 6 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
124 changes: 124 additions & 0 deletions adapters/bidscube/bidscube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package bidscube

import (
"encoding/json"
"errors"
"net/http"
"strconv"

"github.com/mxmCherry/openrtb/v15/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)

type adapter struct {
endpoint string
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
impressions := request.Imp
result := make([]*adapters.RequestData, 0, len(impressions))
var errs []error

for _, impression := range impressions {
var impExt map[string]json.RawMessage
if err := json.Unmarshal(impression.Ext, &impExt); err != nil {
errs = append(errs, err)
continue
}

bidderExt, bidderExtExists := impExt["bidder"]
if !bidderExtExists || len(bidderExt) == 0 {
errs = append(errs, errors.New("bidder parameters required"))
continue
}

impression.Ext = bidderExt
request.Imp = []openrtb2.Imp{impression}
body, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
continue
}
result = append(result, &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: body,
Headers: headers,
})
}

request.Imp = impressions
return result, errs
}

func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var errs []error

switch responseData.StatusCode {
case http.StatusNoContent:
return nil, nil
case http.StatusBadRequest:
return nil, []error{&errortypes.BadInput{
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode),
}}
case http.StatusOK:
break
default:
return nil, []error{&errortypes.BadServerResponse{
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode),
}}
}

var bidResponse openrtb2.BidResponse
err := json.Unmarshal(responseData.Body, &bidResponse)
if err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: err.Error(),
}}
}

response := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))

for _, seatBid := range bidResponse.SeatBid {
for i := range seatBid.Bid {
var bidExt map[string]interface{}
if err := json.Unmarshal(seatBid.Bid[i].Ext, &bidExt); err != nil {
errs = append(errs, err)
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
continue
}
bidType := bidExt["prebid"].(map[string]interface{})["type"].(string)
response.Bids = append(response.Bids, &adapters.TypedBid{
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
Bid: &seatBid.Bid[i],
BidType: getMediaTypeForImp(bidType),
})
}
}

return response, errs
}

func getMediaTypeForImp(bidType string) openrtb_ext.BidType {
switch bidType {
case "banner":
return openrtb_ext.BidTypeBanner
case "video":
return openrtb_ext.BidTypeVideo
case "native":
return openrtb_ext.BidTypeNative
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
}
return openrtb_ext.BidTypeBanner
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
}

// Builder builds a new instance of the BidsCube adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &adapter{
endpoint: config.Endpoint,
}
return bidder, nil
}
18 changes: 18 additions & 0 deletions adapters/bidscube/bidscube_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package bidscube

import (
"testing"

"github.com/prebid/prebid-server/adapters/adapterstest"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
)

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderBidsCube, config.Adapter{
Endpoint: "http://example.com/?c=o&m=ortb"})
if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}
adapterstest.RunJSONBidderTest(t, "bidscubetest", bidder)
}
128 changes: 128 additions & 0 deletions adapters/bidscube/bidscubetest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"placementId": "3"
}
}
}
],
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"device": {
"ip": "123.123.123.123",
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx"
}
},

"httpCalls": [
{
"expectedRequest": {
"uri": "http://example.com/?c=o&m=ortb",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"placementId": "3"
}
}
],
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"device": {
"ip": "123.123.123.123",
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx"
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://supply.bidscube.com/?c=o&m=adm&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
}
]
}
]
}
}
}
],

"expectedBidResponses": [
{
"bids":[
{
"bid": {
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://supply.bidscube.com/?c=o&m=adm&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
},
"type": "banner"
}
]
}
]
}
116 changes: 116 additions & 0 deletions adapters/bidscube/bidscubetest/exemplary/simple-video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"mockBidRequest": {
"id": "test-request-id",
"device": {
"ip": "123.123.123.123",
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx"
},
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"imp": [
{
"id": "test-imp-id",
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1024,
"h": 576
},
"ext": {
"bidder": {
"placementId": "3"
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "http://example.com/?c=o&m=ortb",
"body": {
"id": "test-request-id",
"device": {
"ip": "123.123.123.123",
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx"
},
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"imp": [
{
"id": "test-imp-id",
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1024,
"h": 576
},
"ext": {
"placementId": "3"
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<VAST version=\"3.0\"><Ad id=\"20001\" sequence=\"1\"><InLine><AdSystem version=\"4.0\"><![CDATA[iabtechlab]]></AdSystem><AdTitle><![CDATA[Inline Simple Ad]]></AdTitle><Impression><![CDATA[]]></Impression><Creatives><Creative id=\"5480\" sequence=\"1\"><Linear><Duration>00:01:00</Duration><MediaFiles><MediaFile id=\"5246\" delivery=\"progressive\" type=\"video/mp4\" bitrate=\"600\" width=\"640\" height=\"360\" minBitrate=\"500\" maxBitrate=\"700\" scalable=\"1\" maintainAspectRatio=\"1\" codec=\"0\"><![CDATA[https://s0.2mdn.net/4253510/google_ddm_animation_480P.mp4]]></MediaFile></MediaFiles><VideoClicks><ClickThrough id=\"blog\"><![CDATA[https://example.com]]></ClickThrough></VideoClicks></Linear></Creative></Creatives></InLine></Ad></VAST>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"ext": {
"prebid": {
"type": "video"
}
}
}
],
"seat": "bidscube"
}
],
"cur": "USD"
}
}
}
],


"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<VAST version=\"3.0\"><Ad id=\"20001\" sequence=\"1\"><InLine><AdSystem version=\"4.0\"><![CDATA[iabtechlab]]></AdSystem><AdTitle><![CDATA[Inline Simple Ad]]></AdTitle><Impression><![CDATA[]]></Impression><Creatives><Creative id=\"5480\" sequence=\"1\"><Linear><Duration>00:01:00</Duration><MediaFiles><MediaFile id=\"5246\" delivery=\"progressive\" type=\"video/mp4\" bitrate=\"600\" width=\"640\" height=\"360\" minBitrate=\"500\" maxBitrate=\"700\" scalable=\"1\" maintainAspectRatio=\"1\" codec=\"0\"><![CDATA[https://s0.2mdn.net/4253510/google_ddm_animation_480P.mp4]]></MediaFile></MediaFiles><VideoClicks><ClickThrough id=\"blog\"><![CDATA[https://example.com]]></ClickThrough></VideoClicks></Linear></Creative></Creatives></InLine></Ad></VAST>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"ext": {
"prebid": {
"type": "video"
}
}
},
"type": "video"
}
]
}
]
}
Loading