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: Vidazoo #3698

Merged
merged 7 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions adapters/vidazoo/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package vidazoo

import (
"encoding/json"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"testing"
)

func TestValidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json schema. %v", err)
}

for _, validParam := range validParams {
if err := validator.Validate(openrtb_ext.BidderVidazoo, json.RawMessage(validParam)); err != nil {
t.Errorf("Schema rejected valid params: %s", validParam)
}
}
}

func TestInvalidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json schema. %v", err)
}

for _, p := range invalidParams {
if err := validator.Validate(openrtb_ext.BidderVidazoo, json.RawMessage(p)); err == nil {
t.Errorf("Schema allowed invalid params: %s", p)
}
}
}

var validParams = []string{
`{"cId": "provided_cid_123"}`,
}

var invalidParams = []string{
`{"cId": 3898334}`,
}
133 changes: 133 additions & 0 deletions adapters/vidazoo/vidazoo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package vidazoo

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/prebid/openrtb/v20/openrtb2"
"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"
)

type adapter struct {
endpoint string
}

// Builder builds a new instance of the Vidazoo for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
bidder := &adapter{
endpoint: config.Endpoint,
}
return bidder, nil
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var requests []*adapters.RequestData
var errors []error

requestCopy := *request

for _, imp := range request.Imp {
requestCopy.Imp = []openrtb2.Imp{imp}

requestJSON, err := json.Marshal(&requestCopy)
if err != nil {
errors = append(errors, fmt.Errorf("marshal bidRequest: %w", err))
continue
}

cId, err := extractCid(&imp)
if err != nil {
errors = append(errors, fmt.Errorf("extract cId: %w", err))
continue
}

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")

requestData := &adapters.RequestData{
Method: "POST",
Uri: fmt.Sprintf("%s%s", a.endpoint, url.QueryEscape(cId)),
Body: requestJSON,
Headers: headers,
ImpIDs: []string{imp.ID},
}

requests = append(requests, requestData)
}

return requests, errors
}

func getMediaTypeForBid(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
}
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Could not define bid type for imp: %s", bid.ImpID),
}
}

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

if adapters.IsResponseStatusCodeNoContent(responseData) {
return nil, nil
}

if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", responseData.StatusCode),
}}
}

var response openrtb2.BidResponse
if err := json.Unmarshal(responseData.Body, &response); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("bad server response: %d. ", err),
}}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(response.SeatBid))

if response.Cur != "" {
bidResponse.Currency = response.Cur
}

for _, seatBid := range response.SeatBid {
for i, bid := range seatBid.Bid {
bidType, err := getMediaTypeForBid(bid)
if err != nil {
errs = append(errs, err)
continue
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: bidType,
})
}
}

return bidResponse, errs
}

func extractCid(imp *openrtb2.Imp) (string, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return "", fmt.Errorf("unmarshal bidderExt: %w", err)
}

var impExt openrtb_ext.ImpExtVidazoo
if err := json.Unmarshal(bidderExt.Bidder, &impExt); err != nil {
return "", fmt.Errorf("unmarshal ImpExtVidazoo: %w", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this effectively use the cid from the first impression only?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use imp splitting as suggested in the docs

return strings.TrimSpace(impExt.ConnectionId), nil
}
24 changes: 24 additions & 0 deletions adapters/vidazoo/vidazoo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package vidazoo

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderVidazoo, config.Adapter{
Endpoint: "http://prebid-server.cootlogix.com/openrtb/",
},
config.Server{
ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2",
})

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

adapterstest.RunJSONBidderTest(t, "vidazootest", bidder)
}
127 changes: 127 additions & 0 deletions adapters/vidazoo/vidazootest/exemplary/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"mockBidRequest": {
"id": "some-request-id",
"site": {
"page": "prebid.org"
},
"imp": [
{
"id": "some-impression-id",
"banner": {
"w": 240,
"h": 400
},
"ext": {
"bidder": {
"cId": "test_cid_123"
}
}
}
],
"tmax": 5000
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://prebid-server.cootlogix.com/openrtb/test_cid_123",
"body": {
"id": "some-request-id",
"site": {
"page": "prebid.org"
},
"imp": [
{
"id": "some-impression-id",
"banner": {
"w": 240,
"h": 400
},
"ext": {
"bidder": {
"cId": "test_cid_123"
}
}
}
],
"tmax": 5000
},
"impIDs": [
"some-impression-id"
]
},
"mockResponse": {
"status": 200,
"body": {
"id": "some-request-id",
"cur": "",
"bidid": "some-bid-id",
"seatbid": [
{
"bid": [
{
"exp": 60,
"adm": "<div>Some creative</div>",
"burl": "",
"iurl": "http://prebid-server.cootlogix.com/creative.jpg",
"lurl": "",
"nurl": "",
"id": "some-bid-id",
"impid": "some-impression-id",
"h": 400,
"w": 240,
"price": 1,
"dealid": "deal123",
"adomain": [
"test.com"
],
"adid": "some-ad-id",
"cid": "test",
"attr": [],
"cat": [],
"crid": "some-creative-id",
"ext": {},
"hratio": 0,
"language": "",
"protocol": 0,
"qagmediarating": 0,
"tactic": "",
"wratio": 0,
"mtype": 1
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "some-bid-id",
"impid": "some-impression-id",
"price": 1,
"adm": "<div>Some creative</div>",
"adid": "some-ad-id",
"cid": "test",
"iurl": "http://prebid-server.cootlogix.com/creative.jpg",
"crid": "some-creative-id",
"adomain": [
"test.com"
],
"dealid": "deal123",
"w": 240,
"h": 400,
"exp": 60,
"ext": {},
"mtype": 1
},
"type": "banner"
}
]
}
],
"expectedMakeBidsErrors": []
}
Loading
Loading