Skip to content

Commit

Permalink
New Adapter: AdTonos (prebid#3853)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaw92 authored Sep 12, 2024
1 parent c02ee8c commit ffdd75f
Show file tree
Hide file tree
Showing 12 changed files with 706 additions and 0 deletions.
142 changes: 142 additions & 0 deletions adapters/adtonos/adtonos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
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 := getMediaTypeForBid(seatBid.Bid[i], 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 getMediaTypeForBid(bid openrtb2.Bid, requestImps []openrtb2.Imp) (openrtb_ext.BidType, error) {
if bid.MType != 0 {
// If present, use explicit markup type annotation from the bidder:
switch bid.MType {
case openrtb2.MarkupAudio:
return openrtb_ext.BidTypeAudio, nil
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
case openrtb2.MarkupNative:
return openrtb_ext.BidTypeNative, nil
}
}
// As a fallback, guess markup type based on requested type - AdTonos is an audio company so we prioritize that.
for _, requestImp := range requestImps {
if requestImp.ID == bid.ImpID {
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\"", bid.ImpID),
}
}
}
}
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find impression: \"%s\"", bid.ImpID),
}
}
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)
}
115 changes: 115 additions & 0 deletions adapters/adtonos/adtonostest/exemplary/simple-audio-with-mtype.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"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,
"mtype": 3
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "1",
"impid": "some-impression-id",
"crid": "some-creative-id",
"adm": "<VAST>TAG</VAST>",
"price": 6.5,
"mtype": 3
},
"type": "audio"
}
]
}
]
}
Loading

0 comments on commit ffdd75f

Please sign in to comment.