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: TradPlus #3987

Open
wants to merge 14 commits 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
54 changes: 54 additions & 0 deletions adapters/tradplus/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tradplus

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-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"}`,
`{"accountId": 123}`,
`{"accountId": {"test":1}}`,
`{"accountId": true}`,
`{"accountId": null}`,
`{"zoneId": "aaa"}`,
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
`{"zoneId": "aaa"}`,
`{"zoneId": null}`,
}
147 changes: 147 additions & 0 deletions adapters/tradplus/tradplus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package tradplus

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

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

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) {

tradplusExt, err := getImpressionExt(&request.Imp[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please link to your prebid.github.io PR and make it clear in that PR that the TradPlus only looks at the ext for the first impression and ignored the rest. Some other adapters do it, but its not a publisher expected behavior.

Copy link
Author

@tradplus tradplus Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,I've added to the first comment if anything else is needed ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. New adapters must also open a PR against the Prebid Docs repository here: https://github.com/prebid/prebid.github.io/

Please see the User Documentation section of our new adapter developer guide.

if err != nil {
return nil, []error{err}
}

request.Imp[0].Ext = nil

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

reqBody, err := jsonutil.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 := jsonutil.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Error parsing tradplusExt - " + err.Error(),
}
}

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

if tradplusExt.AccountID == "" {
return nil, &errortypes.BadInput{
Message: "imp.ext.accountId required",
}
}

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)
}

// 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/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.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": []
}
88 changes: 88 additions & 0 deletions adapters/tradplus/tradplustest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 50
}
]
},
"ext": {
"bidder": {
"accountId": "fake-account-id",
"zoneId": "us-"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://us-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": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "ttx",
"bid": [
{
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 1.2,
"adm": "some-ads",
"crid": "crid_testid",
"mtype": 1
}
]
}
],
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 1.2,
"adm": "some-ads",
"crid": "crid_testid",
"mtype": 1
},
"type": "banner"
}
]
}
]
}
Loading
Loading