Skip to content

Commit

Permalink
Add AdTonos Exchange bidder adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaw92 committed Aug 8, 2024
1 parent 7c92e10 commit c3cca60
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 0 deletions.
128 changes: 128 additions & 0 deletions adapters/adtonos/adtonos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package adtonos

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

"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/macros"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)

type adapter struct {
endpointTemplate *template.Template
}

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (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 *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(request.Imp[0].Ext, &bidderExt); err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Invalid imp.ext for impression index %d. Error Infomation: %s", 0, err.Error()),
}}
}
var impExt openrtb_ext.ImpExtAdTonos
if err := json.Unmarshal(bidderExt.Bidder, &impExt); err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Invalid imp.ext.bidder for impression index %d. Error Infomation: %s", 0, err.Error()),
}}
}

endpoint, err := a.buildEndpointURL(&impExt)
if err != nil {
return nil, []error{err}
}

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

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

requestData := &adapters.RequestData{
Method: "POST",
Uri: endpoint,
Body: requestJson,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
}

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

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

func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if adapters.IsResponseStatusCodeNoContent(responseData) {
return nil, nil
}

if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
return nil, []error{err}
}

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

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
bidResponse.Currency = response.Cur
var errors []error
for _, seatBid := range response.SeatBid {
for i := range seatBid.Bid {
bidType, err := getMediaTypeForImp(seatBid.Bid[i].ImpID, request.Imp)
if err != nil {
errors = append(errors, err)
continue
}
b := &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, errors
}

func getMediaTypeForImp(responseImpId string, requestImps []openrtb2.Imp) (openrtb_ext.BidType, error) {
for _, requestImp := range requestImps {
if requestImp.ID == responseImpId {
if requestImp.Audio != nil {
return openrtb_ext.BidTypeAudio, nil
} else if requestImp.Video != nil {
return openrtb_ext.BidTypeVideo, nil
} else {
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Unsupported bidtype for bid: \"%s\"", responseImpId),
}
}
}
}
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find impression: \"%s\"", responseImpId),
}
}
30 changes: 30 additions & 0 deletions adapters/adtonos/adtonos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package adtonos

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"
"github.com/stretchr/testify/assert"
)

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

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

adapterstest.RunJSONBidderTest(t, "adtonostest", bidder)
}

func TestEndpointTemplateMalformed(t *testing.T) {
_, buildErr := Builder(openrtb_ext.BidderAdTonos, config.Adapter{
Endpoint: "{{Malformed}}"},
config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

assert.Error(t, buildErr)
}
113 changes: 113 additions & 0 deletions adapters/adtonos/adtonostest/exemplary/simple-audio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"mockBidRequest": {
"id": "some-request-id",
"tmax": 1000,
"imp": [
{
"id": "some-impression-id",
"bidfloor": 4.2,
"ext": {
"bidder": {
"supplierId": "777XYZ123"
}
},
"audio": {
"mimes": [
"audio/mpeg"
]
}
}
],
"test": 1,
"site": {
"publisher": {
"id": "1"
},
"page": "http://www.example.com",
"domain": "www.example.com"
},
"device": {}
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://exchange.example.com/bid/777XYZ123",
"headers": {
"Content-Type": [
"application/json;charset=utf-8"
],
"Accept": [
"application/json"
]
},
"body": {
"id": "some-request-id",
"tmax": 1000,
"imp": [
{
"id": "some-impression-id",
"bidfloor": 4.2,
"ext": {
"bidder": {
"supplierId": "777XYZ123"
}
},
"audio": {
"mimes": [
"audio/mpeg"
]
}
}
],
"site": {
"publisher": {
"id": "1"
},
"page": "http://www.example.com",
"domain": "www.example.com"
},
"device": {},
"test": 1
},
"impIDs":["some-impression-id"]
},
"mockResponse": {
"status": 200,
"body": {
"id": "some-request-id",
"cur": "USD",
"seatbid": [
{
"bid": [
{
"id": "1",
"impid": "some-impression-id",
"crid": "some-creative-id",
"adm": "<VAST>TAG</VAST>",
"price": 6.5
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "1",
"impid": "some-impression-id",
"crid": "some-creative-id",
"adm": "<VAST>TAG</VAST>",
"price": 6.5
},
"type": "audio"
}
]
}
]
}
113 changes: 113 additions & 0 deletions adapters/adtonos/adtonostest/exemplary/simple-video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"mockBidRequest": {
"id": "video-request-id",
"tmax": 1000,
"imp": [
{
"id": "some-impression-id",
"bidfloor": 4.2,
"ext": {
"bidder": {
"supplierId": "777XYZ123"
}
},
"video": {
"mimes": [
"video/mp4"
]
}
}
],
"test": 1,
"site": {
"publisher": {
"id": "1"
},
"page": "http://www.example.com",
"domain": "www.example.com"
},
"device": {}
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://exchange.example.com/bid/777XYZ123",
"headers": {
"Content-Type": [
"application/json;charset=utf-8"
],
"Accept": [
"application/json"
]
},
"body": {
"id": "video-request-id",
"tmax": 1000,
"imp": [
{
"id": "some-impression-id",
"bidfloor": 4.2,
"ext": {
"bidder": {
"supplierId": "777XYZ123"
}
},
"video": {
"mimes": [
"video/mp4"
]
}
}
],
"site": {
"publisher": {
"id": "1"
},
"page": "http://www.example.com",
"domain": "www.example.com"
},
"device": {},
"test": 1
},
"impIDs":["some-impression-id"]
},
"mockResponse": {
"status": 200,
"body": {
"id": "video-request-id",
"cur": "USD",
"seatbid": [
{
"bid": [
{
"id": "1",
"impid": "some-impression-id",
"crid": "some-creative-id",
"adm": "<VAST>TAG</VAST>",
"price": 6.5
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "1",
"impid": "some-impression-id",
"crid": "some-creative-id",
"adm": "<VAST>TAG</VAST>",
"price": 6.5
},
"type": "video"
}
]
}
]
}
Loading

0 comments on commit c3cca60

Please sign in to comment.