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: adf (adformOpenRTB) #1815

Merged
merged 4 commits into from
Apr 23, 2021
Merged
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
106 changes: 106 additions & 0 deletions adapters/adf/adf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package adf

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

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

type adapter struct {
endpoint string
}

// Builder builds a new instance of the Adf adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &adapter{
endpoint: config.Endpoint,
}
return bidder, nil
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errors []error
var validImps = make([]openrtb2.Imp, 0, len(request.Imp))

for _, imp := range request.Imp {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
errors = append(errors, &errortypes.BadInput{
Message: err.Error(),
})
continue
}

var adfImpExt openrtb_ext.ExtImpAdf
if err := json.Unmarshal(bidderExt.Bidder, &adfImpExt); err != nil {
errors = append(errors, &errortypes.BadInput{
Message: err.Error(),
})
continue
}

imp.TagID = adfImpExt.MasterTagID.String()
validImps = append(validImps, imp)
}

request.Imp = validImps

requestJSON, err := json.Marshal(request)
if err != nil {
errors = append(errors, err)
return nil, errors
}

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

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

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

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

if responseData.StatusCode != http.StatusOK {
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d.", responseData.StatusCode),
}
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

for _, seatBid := range response.SeatBid {
for i := range seatBid.Bid {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: openrtb_ext.BidTypeNative,
})
}
}

return bidResponse, nil
}
20 changes: 20 additions & 0 deletions adapters/adf/adf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package adf

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderAdf, config.Adapter{
Endpoint: "https://adx.adform.net/adx/openrtb"})

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

adapterstest.RunJSONBidderTest(t, "adftest", bidder)
}
108 changes: 108 additions & 0 deletions adapters/adf/adftest/exemplary/multi-native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [{
"id": "test-imp-id-1",
"ext": {
"bidder": {
"mid": "828782"
}
},
"native": {
"request": "{json string 1}",
"ver": "1.2"
}
}, {
"id": "test-imp-id-2",
"ext": {
"bidder": {
"mid": "828783"
}
},
"native": {
"request": "{json string 2}",
"ver": "1.2"
}
}]
},
"httpCalls": [{
"expectedRequest": {
"uri": "https://adx.adform.net/adx/openrtb",
"body": {
"id": "test-request-id",
"imp": [{
"id": "test-imp-id-1",
"ext": {
"bidder": {
"mid": "828782"
}
},
"native": {
"request": "{json string 1}",
"ver": "1.2"
},
"tagid": "828782"
}, {
"id": "test-imp-id-2",
"ext": {
"bidder": {
"mid": "828783"
}
},
"native": {
"request": "{json string 2}",
"ver": "1.2"
},
"tagid": "828783"
}]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [{
"bid": [{
"id": "test-bid-id-1",
"impid": "test-imp-id-1",
"price": 10,
"adm": "{json response string 1}",
"adomain": [],
"crid": "test-creative-id-1"
}, {
"id": "test-bid-id-2",
"impid": "test-imp-id-2",
"price": 2,
"adm": "{json response string 2}",
"adomain": [ "ad-domain" ],
"crid": "test-creative-id-2"
}]
}],
"cur": "EUR"
}
}
}],
"expectedBidResponses": [{
"currency": "EUR",
"bids": [{
"bid": {
"id": "test-bid-id-1",
"impid": "test-imp-id-1",
"price": 10,
"adm": "{json response string 1}",
"crid": "test-creative-id-1"
},
"type": "native"
}, {
"bid": {
"id": "test-bid-id-2",
"impid": "test-imp-id-2",
"price": 2,
"adm": "{json response string 2}",
"adomain": [ "ad-domain" ],
"crid": "test-creative-id-2"
},
"type": "native"
}]
}]
}
90 changes: 90 additions & 0 deletions adapters/adf/adftest/exemplary/single-native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [{
"id": "test-imp-id",
"ext": {
"bidder": {
"mid": "828782"
}
},
"native": {
"request": "{json string}",
"ver": "1.2"
}
}],
"site": {
"publisher": {
"id": "1"
},
"page": "some-page-url"
},
"device": {
"w": 1920,
"h": 800
}
},
"httpCalls": [{
"expectedRequest": {
"uri": "https://adx.adform.net/adx/openrtb",
"body": {
"id": "test-request-id",
"imp": [{
"id": "test-imp-id",
"ext": {
"bidder": {
"mid": "828782"
}
},
"native": {
"request": "{json string}",
"ver": "1.2"
},
"tagid": "828782"
}],
"site": {
"publisher": {
"id": "1"
},
"page": "some-page-url"
},
"device": {
"w": 1920,
"h": 800
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [{
"bid": [{
"id": "test-bid-id",
"impid": "test-imp-id",
"price": 10,
"adm": "{json response string}",
"adomain": [],
"crid": "test-creative-id"
}]
}],
"cur": "USD"
}
}
}],
"expectedBidResponses": [{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-bid-id",
"impid": "test-imp-id",
"price": 10,
"adm": "{json response string}",
"crid": "test-creative-id"
},
"type": "native"
}
]
}]
}
3 changes: 3 additions & 0 deletions adapters/adf/adftest/params/race/native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mid": "828782"
}
48 changes: 48 additions & 0 deletions adapters/adf/adftest/supplemental/bad-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [{
"id": "test-imp-id",
"native": {
"request": ""
},
"ext": {
"bidder": {
"mid": 12345
}
}
}]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://adx.adform.net/adx/openrtb",
"body": {
"id": "test-request-id",
"imp": [{
"ext": {
"bidder": {
"mid": 12345
}
},
"id": "test-imp-id",
"native": {
"request": ""
},
"tagid": "12345"
}]
}
},
"mockResponse": {
"status": 400
}
}
],
"expectedBidResponses": [],
"expectedMakeBidsErrors": [
{
"value": "Unexpected status code: 400. Bad request from publisher.",
"comparison": "literal"
}
]
}
Loading