Skip to content

New Adapter: Tagoras #4329

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
50 changes: 50 additions & 0 deletions adapters/tagoras/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tagoras

import (
"encoding/json"
"testing"

"github.com/prebid/prebid-server/v3/openrtb_ext"
)

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.BidderTagoras, 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.BidderTagoras, json.RawMessage(p)); err == nil {
t.Errorf("Schema allowed invalid params: %s", p)
}
}
}

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

var invalidParams = []string{
`{"cId": 123}`,
`{"cId": true}`,
`{"cId": ["array"]}`,
`{"cId": {}`,
`{"cId": ""}`,
`{"cId": null}`,
`{"cId": "provided_cid_123", "extra": "field"}`,
`{"cid": "valid_cid"}`,
`{"cId": "invalid_chars_!@#$%^&*()"}`,
}
132 changes: 132 additions & 0 deletions adapters/tagoras/tagoras.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package tagoras

import (
"encoding/json"
"fmt"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v3/adapters"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/errortypes"
"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/prebid/prebid-server/v3/util/jsonutil"
"net/http"
"net/url"
)

type adapter struct {
endpoint string
}

// Builder builds a new instance of the tagoras 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 (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 := jsonutil.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 := jsonutil.Unmarshal(imp.Ext, &bidderExt); err != nil {
return "", fmt.Errorf("unmarshal bidderExt: %w", err)
}

var impExt openrtb_ext.ImpExtTagoras
if err := jsonutil.Unmarshal(bidderExt.Bidder, &impExt); err != nil {
return "", fmt.Errorf("unmarshal ImpExtTagoras: %w", err)
}
return impExt.ConnectionId, nil
}

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),
}
}
24 changes: 24 additions & 0 deletions adapters/tagoras/tagoras_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tagoras

import (
"testing"

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

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

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

adapterstest.RunJSONBidderTest(t, "tagorastest", bidder)
}
127 changes: 127 additions & 0 deletions adapters/tagoras/tagorastest/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": "https://tagoras.io/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": "https://tagoras.io/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": "https://tagoras.io/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