Skip to content

Commit

Permalink
New Adapter: TradPlus
Browse files Browse the repository at this point in the history
  • Loading branch information
tradplus committed Aug 2, 2024
1 parent 843a81c commit 5d43c55
Show file tree
Hide file tree
Showing 19 changed files with 955 additions and 0 deletions.
48 changes: 48 additions & 0 deletions adapters/tradplus/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tradplus

import (
"encoding/json"
"testing"

"github.com/prebid/prebid-server/v2/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-schemas. %v", err)
}

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

// TestInvalidParams makes sure that the tradplus schema rejects all the imp.ext fields we don't support.
func TestInvalidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json-schemas. %v", err)
}

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

var validParams = []string{
`{"accountId": "11233", "zoneId": ""}`,
`{"accountId": "aaa", "accountId": "us"}`,
`{"accountId": "aa", "accountId": "sin"}`,
}

var invalidParams = []string{
`{"accountId": ""}`,
`{"accountId": "", "zoneId": ""}`,
`{"accountId": "", "zoneId": "sin"}`,
`{"zoneId": "aaa"}`,
}
197 changes: 197 additions & 0 deletions adapters/tradplus/tradplus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package tradplus

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 {
endpoint *template.Template
}

// Builder builds a new instance of the tradplus adapter for the given bidder with the given config.
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)
}

return &adapter{
endpoint: template,
}, nil
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
adapterRequest, errs := a.makeRequest(request)
if errs != nil {
return nil, errs
}
return []*adapters.RequestData{adapterRequest}, nil
}

func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, []error) {
errs := validateTradPlusExt(request)
if errs != nil {
return nil, errs
}

tradplusExt, err := getImpressionExt(&request.Imp[0])
if err != nil {
return nil, []error{err}
}

request.Imp[0].Ext = nil

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

err = transform(request)
if err != nil {
return nil, []error{err}
}

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

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
return &adapters.RequestData{
Method: "POST",
Uri: url,
Body: reqBody,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
}, nil
}

func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtImpTradPlus, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Error parsing tradplusExt - " + err.Error(),
}
}

var tradplusExt openrtb_ext.ExtImpTradPlus
if err := json.Unmarshal(bidderExt.Bidder, &tradplusExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Error parsing bidderExt - " + err.Error(),
}
}

return &tradplusExt, nil
}

func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtImpTradPlus) (string, error) {
endpointParams := macros.EndpointTemplateParams{
AccountID: params.AccountID,
ZoneID: params.ZoneID,
}
return macros.ResolveMacros(a.endpoint, endpointParams)
}

func transform(request *openrtb2.BidRequest) error {
for i, imp := range request.Imp {
if imp.Native != nil {
var nativeRequest map[string]interface{}
nativeCopyRequest := make(map[string]interface{})
if err := json.Unmarshal([]byte(request.Imp[i].Native.Request), &nativeRequest); err != nil {
return err
}
_, exists := nativeRequest["native"]
if exists {
continue
}
nativeCopyRequest["native"] = nativeRequest
nativeReqByte, err := json.Marshal(nativeCopyRequest)
if err != nil {
return err
}
nativeCopy := *request.Imp[i].Native
nativeCopy.Request = string(nativeReqByte)
request.Imp[i].Native = &nativeCopy
}
}
return nil
}

func validateTradPlusExt(request *openrtb2.BidRequest) []error {
var extImpTradPlus openrtb_ext.ExtImpTradPlus
var errs []error
for _, imp := range request.Imp {
var extBidder adapters.ExtImpBidder
err := json.Unmarshal(imp.Ext, &extBidder)
if err != nil {
errs = append(errs, err)
continue
}
err = json.Unmarshal(extBidder.Bidder, &extImpTradPlus)
if err != nil {
errs = append(errs, err)
continue
}
if extImpTradPlus.AccountID == "" {
errs = append(errs, fmt.Errorf("imp.ext.accountId required"))
continue
}
}
return errs
}

// MakeBids make the bids for the bid response.
func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if adapters.IsResponseStatusCodeNoContent(response) {
return nil, nil
}
if err := adapters.CheckResponseStatusCodeForErrors(response); err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d.", response.StatusCode),
}}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
var errs []error
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(internalRequest.Imp))
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
mediaType, err := getMediaTypeForBid(sb.Bid[i])
if err != nil {
errs = append(errs, err)
continue
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: mediaType,
})
}
}
return bidResponse, errs
}

func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
switch bid.MType {
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
case openrtb2.MarkupNative:
return openrtb_ext.BidTypeNative, nil
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
default:
return "", fmt.Errorf("unrecognized bid type in response from tradplus for bid %s", bid.ImpID)
}
}
29 changes: 29 additions & 0 deletions adapters/tradplus/tradplus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tradplus

import (
"testing"

"github.com/stretchr/testify/assert"

"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.BidderTradPlus, config.Adapter{
Endpoint: "https://{{.ZoneID}}adx.tradplusad.com/{{.AccountID}}/pserver"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

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

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

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

assert.Error(t, buildErr)
}
53 changes: 53 additions & 0 deletions adapters/tradplus/tradplustest/exemplary/no-bid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 50
}
]
},
"ext": {
"bidder": {
"accountId": "fake-account-id",
"zoneId": ""
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://adx.tradplusad.com/fake-account-id/pserver",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 50
}
]
}
}
]
},
"impIDs":["test-imp-id"]
},
"mockResponse": {
"status": 204,
"body": {}
}
}
],
"expectedBidResponses": []
}
Loading

0 comments on commit 5d43c55

Please sign in to comment.