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: Onetag #1695

Merged
merged 5 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
162 changes: 162 additions & 0 deletions adapters/onetag/onetag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package onetag

import (
"encoding/json"
"fmt"
"net/http"
"text/template"

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

type adapter struct {
endpointTemplate template.Template
}

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
template, err := template.New("endpointTemplate").Parse(config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
}

bidder := &adapter{
endpointTemplate: *template,
}

return bidder, nil
}

func (a *adapter) MakeRequests(request *openrtb.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
pubID := ""
for idx, imp := range request.Imp {
onetagExt, err := getImpressionExt(imp)
if err != nil {
return nil, []error{err}
}
if onetagExt.PubId != "" {
if pubID == "" {
pubID = onetagExt.PubId
}
if pubID != onetagExt.PubId {
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
return nil, []error{&errortypes.BadInput{
Message: "There must be only one publisher ID",
}}
}
}
request.Imp[idx].Ext = onetagExt.Ext
}

if pubID == "" {
return nil, []error{&errortypes.BadInput{
Message: "Missing Publisher ID",
}}
}

url, err := a.buildEndpointURL(pubID)
if err != nil {
return nil, []error{err}
}

requestJSON, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
}

requestData := &adapters.RequestData{
Method: "POST",
Uri: url,
Body: requestJSON,
}

return []*adapters.RequestData{requestData}, nil
}

func getImpressionExt(imp openrtb.Imp) (*openrtb_ext.ExtImpOnetag, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Bidder extension not provided or can't be unmarshalled",
}
}

var onetagExt openrtb_ext.ExtImpOnetag
if err := json.Unmarshal(bidderExt.Bidder, &onetagExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Error while unmarshaling bidder extension",
}
}

return &onetagExt, nil
}

func (a *adapter) buildEndpointURL(pubID string) (string, error) {
endpointParams := macros.EndpointTemplateParams{PublisherID: pubID}
return macros.ResolveMacros(a.endpointTemplate, endpointParams)
}

func (a *adapter) MakeBids(request *openrtb.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
}

if responseData.StatusCode == http.StatusBadRequest {
err := &errortypes.BadInput{
Message: "Unexpected status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.",
}
return nil, []error{err}
}

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

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

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
bidResponse.Currency = response.Cur
for _, seatBid := range response.SeatBid {
for _, bid := range seatBid.Bid {
bidMediaType, err := getMediaTypeForBid(request.Imp, bid)
if err != nil {
return nil, []error{err}
}
b := &adapters.TypedBid{
Bid: &bid,
BidType: bidMediaType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, nil
}

func getMediaTypeForBid(impressions []openrtb.Imp, bid openrtb.Bid) (openrtb_ext.BidType, error) {
for _, impression := range impressions {
if impression.ID == bid.ImpID {
if impression.Banner != nil {
return openrtb_ext.BidTypeBanner, nil
}
if impression.Video != nil {
return openrtb_ext.BidTypeVideo, nil
}
if impression.Native != nil {
return openrtb_ext.BidTypeNative, nil
}
}
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
}

return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("The impression with ID %s is not present into the request", bid.ImpID),
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
}
}
26 changes: 26 additions & 0 deletions adapters/onetag/onetag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package onetag

import (
"testing"

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

func TestEndpointTemplateMalformed(t *testing.T) {
_, buildErr := Builder(openrtb_ext.BidderOneTag, config.Adapter{
Endpoint: "{{Malformed}}"})

assert.Error(t, buildErr)
}

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderOneTag, config.Adapter{
Endpoint: "https://example.com/prebid-server/{{.PublisherID}}"})

assert.NoError(t, buildErr, "Builder returned unexpected error %v", buildErr)

adapterstest.RunJSONBidderTest(t, "onetagtest", bidder)
}
201 changes: 201 additions & 0 deletions adapters/onetag/onetagtest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"mockBidRequest": {
"id": "8652a8680db33faabbf3fa76150f35df50a67060",
"imp": [
{
"id": "121-dt1",
"banner": {
"h": 250,
"w": 300,
"pos": 1
},
"bidfloor": 0.05,
"ext": {
"bidder": {
"pubId": "386276e072"
}
}
},
{
"id": "121-dt2",
"banner": {
"h": 728,
"w": 90,
"pos": 0
},
"bidfloor": 0.12,
"ext": {
"bidder": {
"pubId": "386276e072",
"ext": {
"key1": "value1",
"key2": "value2"
}
}
}
}
],
"site": {
"id": "15047",
"domain": "dailymotion.com",
"cat": ["IAB1"],
"page": "http://www.dailymotion.com/video/xxeauj_www-dramacafe-tv-hd-yyyy-yy-yyyyyyy-2012-yyyy_shortfilms",
"publisher": {
"id": "8796",
"name": "dailymotion",
"cat": ["IAB3-1"],
"domain": "dailymotion.com"
}
},
"user": {
"id": "518c3da3717203f34019b038"
},
"device": {
"ua": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; (R1 1.6); SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)",
"ip": "123.145.167.189"
},
"at": 1,
"cur": [
"USD"
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://example.com/prebid-server/386276e072",
"headers": {},
"body": {
"id": "8652a8680db33faabbf3fa76150f35df50a67060",
"imp": [
{
"id": "121-dt1",
"banner": {
"h": 250,
"w": 300,
"pos": 1
},
"bidfloor": 0.05
},
{
"id": "121-dt2",
"banner": {
"h": 728,
"w": 90,
"pos": 0
},
"bidfloor": 0.12,
"ext": {
"key1": "value1",
"key2": "value2"
}
}
],
"site": {
"id": "15047",
"domain": "dailymotion.com",
"cat": ["IAB1"],
"page": "http://www.dailymotion.com/video/xxeauj_www-dramacafe-tv-hd-yyyy-yy-yyyyyyy-2012-yyyy_shortfilms",
"publisher": {
"id": "8796",
"name": "dailymotion",
"cat": ["IAB3-1"],
"domain": "dailymotion.com"
}
},
"user": {
"id": "518c3da3717203f34019b038"
},
"device": {
"ua": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; (R1 1.6); SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)",
"ip": "123.145.167.189"
},
"at": 1,
"cur": [
"USD"
]
}
},
"mockResponse": {
"status": 200,
"headers": {},
"body": {
"id": "BID-4-ZIMP-4b309eae-504a-4252-a8a8-4c8ceee9791a",
"seatbid": [
{
"bid": [
{
"adid": "52a12b5955314b7194a4c9ff",
"adm": "<iframe src=\"http://ads.com/render/52a12b5955314b7194a4c9ff?won=${AUCTION_PRICE}\" width=\"728\" height=\"90\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" topmargin=\"0\" leftmargin=\"0\"></iframe>",
"adomain": ["ads.com"],
"cid": "52a12b5955314b7194a4c9ff",
"crid": "52a12b5955314b7194a4c9ff_1386294105",
"dealid": "DX-1985-010A",
"id": "24195efda36066ee21f967bc1de14c82db841f07",
"impid": "121-dt1",
"nurl": "http://ads.com/win/52a12b5955314b7194a4c9ff?won=${AUCTION_PRICE}",
"price": 1.028428,
"attr": []
}
],
"seat": "42"
},
{
"bid": [
{
"adid": "527c9fdd55314ba06815f25e",
"adm": "<iframe src=\"http://ads.com/render/527c9fdd55314ba06815f25e?won=${AUCTION_PRICE}\" width=\"300\" height=\"250\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" topmargin=\"0\" leftmargin=\"0\"></iframe>",
"adomain": ["ads.com"],
"cid": "527c9fdd55314ba06815f25e",
"crid": "527c9fdd55314ba06815f25e_1383899102",
"id": "24195efda36066ee21f967bc1de14c82db841f08",
"impid": "121-dt2",
"nurl": "http://ads.com/win/527c9fdd55314ba06815f25e?won=${AUCTION_PRICE}",
"price": 0.04958,
"attr": []
}
],
"seat": "772"
}
]
},
"cur": "USD"
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"type": "banner",
"bid": {
"adid": "52a12b5955314b7194a4c9ff",
"adm": "<iframe src=\"http://ads.com/render/52a12b5955314b7194a4c9ff?won=${AUCTION_PRICE}\" width=\"728\" height=\"90\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" topmargin=\"0\" leftmargin=\"0\"></iframe>",
"adomain": ["ads.com"],
"cid": "52a12b5955314b7194a4c9ff",
"crid": "52a12b5955314b7194a4c9ff_1386294105",
"dealid": "DX-1985-010A",
"id": "24195efda36066ee21f967bc1de14c82db841f07",
"impid": "121-dt1",
"nurl": "http://ads.com/win/52a12b5955314b7194a4c9ff?won=${AUCTION_PRICE}",
"price": 1.028428
}
},
{
"type": "banner",
"bid": {
"adid": "527c9fdd55314ba06815f25e",
"adm": "<iframe src=\"http://ads.com/render/527c9fdd55314ba06815f25e?won=${AUCTION_PRICE}\" width=\"300\" height=\"250\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" topmargin=\"0\" leftmargin=\"0\"></iframe>",
"adomain": ["ads.com"],
"cid": "527c9fdd55314ba06815f25e",
"crid": "527c9fdd55314ba06815f25e_1383899102",
"id": "24195efda36066ee21f967bc1de14c82db841f08",
"impid": "121-dt2",
"nurl": "http://ads.com/win/527c9fdd55314ba06815f25e?won=${AUCTION_PRICE}",
"price": 0.04958
}
}
]
}
]
}
Loading