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: freewheelssp #2392

Merged
merged 12 commits into from
Nov 15, 2022
103 changes: 103 additions & 0 deletions adapters/freewheelssp/freewheelssp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package freewheelssp

import (
"encoding/json"
"fmt"
"github.com/prebid/openrtb/v17/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"
"net/http"
)

type adapter struct {
endpoint string
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
for i := 0; i < len(request.Imp); i++ {
imp := &request.Imp[i]
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Invalid imp.ext for impression index %d. Error Infomation: %s", i, err.Error()),
}}
}

var impExt openrtb_ext.ImpExtFreewheelSSP
if err := json.Unmarshal(bidderExt.Bidder, &impExt); err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Invalid imp.ext for impression index %d. Error Infomation: %s", i, err.Error()),
}}
}

var err error
if imp.Ext, err = json.Marshal(impExt); err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unable to transfer requestImpExt to Json fomat, %s", err.Error()),
}}
}
}

requestJSON, err := json.Marshal(request)
if err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unable to transfer request to Json fomat, %s", err.Error()),
}}
}

headers := http.Header{}
headers.Add("Componentid", "prebid-go")

requestData := &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: requestJSON,
Headers: headers,
}
return []*adapters.RequestData{requestData}, 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.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to cover this error case with tests?

}}
}

var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}

cur := bidResp.Cur
bidResponse := &adapters.BidderResponse{
Currency: cur,
Bids: []*adapters.TypedBid{},
}

bidType := openrtb_ext.BidTypeVideo

for _, seatBid := range bidResp.SeatBid {
for i := range seatBid.Bid {
b := &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
return bidResponse, nil
}

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
bidder := &adapter{
config.Endpoint,
}
return bidder, nil
}
19 changes: 19 additions & 0 deletions adapters/freewheelssp/freewheelssp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package freewheelssp

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderFreewheelSSP, config.Adapter{
Endpoint: "https://testjsonsample.com"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}

adapterstest.RunJSONBidderTest(t, "freewheelssptest", bidder)
}
144 changes: 144 additions & 0 deletions adapters/freewheelssp/freewheelssptest/exemplary/multi-imp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"mockBidRequest": {
"id": "freewheelssp-test",
"site": {
"page": "prebid.org"
},
"imp": [
{
"id": "imp-1",
"video": {
"mimes": ["video/mp4"],
"w": 300,
"h": 250
},
"ext": {
"bidder": {
"zoneId": 12345
}
}
},
{
"id": "imp-2",
"video": {
"mimes": ["video/mp4"],
"w": 300,
"h": 250
},
"ext": {
"bidder": {
"zoneId": 12346
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://testjsonsample.com",
"body":{
"id": "freewheelssp-test",
"site": {
"page": "prebid.org"
},
"imp": [
{
"id": "imp-1",
"video": {
"mimes": ["video/mp4"],
"w": 300,
"h": 250
},
"ext": {
"zoneId": 12345
}
},
{
"id": "imp-2",
"video": {
"mimes": ["video/mp4"],
"w": 300,
"h": 250
},
"ext": {
"zoneId": 12346
}
}
]
},
"headers": {
"Componentid": [
"prebid-go"
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "freewheelssp-test",
"seatbid": [
{
"bid": [
{
"id": "12345_freewheelssp-test_1",
"impid": "imp-1",
"price": 1.0,
"adid": "7857",
"adm": "<?xml version='1.0' encoding='UTF-8'?><VAST version='2.0'></VAST>",
"cid": "4001",
"crid": "7857"
},
{
"id": "12346_freewheelssp-test_2",
"impid": "imp-2",
"price": 1.0,
"adid": "7933",
"adm": "<?xml version='1.0' encoding='UTF-8'?><VAST version='2.0'></VAST>",
"cid": "3476",
"crid": "7933"
}
],
"seat": "freewheelsspTv"
}
],
"bidid": "freewheelssp-test",
"cur": "EUR"
}
}
}
],

"expectedBidResponses": [
{
"currency": "CUR",
"bids": [
{
"bid": {
"id": "12345_freewheelssp-test_1",
"impid": "imp-1",
"price": 1.0,
"adid": "7857",
"adm": "<?xml version='1.0' encoding='UTF-8'?><VAST version='2.0'></VAST>",
"cid": "4001",
"crid": "7857"
},
"type": "video"
},
{
"bid": {
"id": "12346_freewheelssp-test_2",
"impid": "imp-2",
"price": 1.0,
"adid": "7933",
"adm": "<?xml version='1.0' encoding='UTF-8'?><VAST version='2.0'></VAST>",
"cid": "3476",
"crid": "7933"
},
"type": "video"
}
]
}
]
}
97 changes: 97 additions & 0 deletions adapters/freewheelssp/freewheelssptest/exemplary/single-imp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"mockBidRequest": {
"id": "freewheelssp-test",
"site": {
"page": "prebid.org"
},
"imp": [
{
"id": "imp-1",
"video": {
"mimes": ["video/mp4"],
"w": 300,
"h": 250
},
"ext": {
"bidder": {
"zoneId": 12345
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://testjsonsample.com",
"body":{
"id": "freewheelssp-test",
"site": {
"page": "prebid.org"
},
"imp": [{
"id": "imp-1",
"video": {
"mimes": ["video/mp4"],
"w": 300,
"h": 250
},
"ext": {
"zoneId": 12345
}
}]
},
"headers": {
"Componentid": [
"prebid-go"
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "freewheelssp-test",
"seatbid": [
{
"bid": [
{
"id": "12345_freewheelssp-test_1",
"impid": "imp-1",
"price": 1.0,
"adid": "7857",
"adm": "<?xml version='1.0' encoding='UTF-8'?><VAST version='2.0'></VAST>",
"cid": "4001",
"crid": "7857"
}
],
"seat": "freewheelsspTv"
}
],
"bidid": "freewheelssp-test",
"cur": "EUR"
}
}
}
],

"expectedBidResponses": [
{
"currency": "CUR",
"bids": [
{
"bid": {
"id": "12345_freewheelssp-test_1",
"impid": "imp-1",
"price": 1.0,
"adid": "7857",
"adm": "<?xml version='1.0' encoding='UTF-8'?><VAST version='2.0'></VAST>",
"cid": "4001",
"crid": "7857"
},
"type": "video"
}
]
}
]
}
Loading