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

Add Pubnative Adapter #1134

Merged
merged 5 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
170 changes: 170 additions & 0 deletions adapters/pubnative/pubnative.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package pubnative

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

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

type PubnativeAdapter struct {
URI string
}

func (a *PubnativeAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
impCount := len(request.Imp)
requestData := make([]*adapters.RequestData, 0, impCount)
errs := []error{}

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

err := checkRequest(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}

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

var pubnativeExt openrtb_ext.ExtImpPubnative
if err := json.Unmarshal(bidderExt.Bidder, &pubnativeExt); err != nil {
errs = append(errs, err)
continue
}

err := convertImpression(&imp)
if err != nil {
errs = append(errs, err)
continue
}

reqJSON, err := json.Marshal(request)
fhartwig marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
errs = append(errs, err)
return nil, errs
}

reqData := &adapters.RequestData{
Method: "POST",
Uri: fmt.Sprintf("%s?apptoken=%s&zoneid=%d", a.URI, pubnativeExt.AppAuthToken, pubnativeExt.ZoneID),
fhartwig marked this conversation as resolved.
Show resolved Hide resolved
Body: reqJSON,
Headers: headers,
}

requestData = append(requestData, reqData)
}
return requestData, errs
}

func checkRequest(request *openrtb.BidRequest) error {
if request.Device == nil || len(request.Device.OS) == 0 {
return &errortypes.BadInput{
Message: "Impression is missing device OS information",
}
}

request.Test = 0 // don't forward test flag to PN adserver
return nil
}

func convertImpression(imp *openrtb.Imp) error {
if imp.Banner == nil && imp.Video == nil && imp.Native == nil {
return &errortypes.BadInput{
Message: "Pubnative only supports banner, video or native ads.",
}
}
if imp.Banner != nil {
err := convertBanner(imp.Banner)
if err != nil {
return err
}
}

return nil
}

// make sure that banner has openrtb 2.3-compatible size information
func convertBanner(banner *openrtb.Banner) error {
if banner.W == nil || banner.H == nil || *banner.W == 0 || *banner.H == 0 {
if len(banner.Format) > 0 {
f := banner.Format[0]
banner.W = &f.W
banner.H = &f.H
} else {
return &errortypes.BadInput{
Message: "Size information missing for banner",
}
}
}
return nil
}

func (a *PubnativeAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if response.StatusCode == http.StatusNoContent {
return nil, nil
}

if response.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}

if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}

var parsedResponse openrtb.BidResponse
if err := json.Unmarshal(response.Body, &parsedResponse); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: err.Error(),
}}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)

for _, sb := range parsedResponse.SeatBid {
for i := 0; i < len(sb.Bid); i++ {
bid := sb.Bid[i]
if bid.Price != 0 {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: getMediaTypeForImp(bid.ImpID, internalRequest.Imp),
})
}
}
}
return bidResponse, nil
}

func NewPubnativeBidder(uri string) *PubnativeAdapter {
return &PubnativeAdapter{URI: uri}
}

func getMediaTypeForImp(impId string, imps []openrtb.Imp) openrtb_ext.BidType {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impId {
if imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
} else if imp.Native != nil {
mediaType = openrtb_ext.BidTypeNative
}
return mediaType
}
}
return mediaType
}
11 changes: 11 additions & 0 deletions adapters/pubnative/pubnative_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pubnative

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "pubnativetest", NewPubnativeBidder("http://example.com/prebid"))
}
130 changes: 130 additions & 0 deletions adapters/pubnative/pubnativetest/exemplary/native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"mockBidRequest": {
"id": "some-req-id",
"imp": [
{
"id": "some-imp-id",
"native": {
"request": "{\"ver\":\"1.1\",\"context\":1,\"contextsubtype\":11,\"plcmttype\":4,\"plcmtcnt\":1,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":500}},{\"id\":2,\"required\":1,\"img\":{\"type\":3,\"wmin\":1,\"hmin\":1}},{\"id\":3,\"required\":0,\"data\":{\"type\":1,\"len\":200}},{\"id\":4,\"required\":0,\"data\":{\"type\":2,\"len\":15000}},{\"id\":5,\"required\":0,\"data\":{\"type\":6,\"len\":40}},{\"id\":6,\"required\":0,\"data\":{\"type\":500}}]}",
"ver": "1.1"
},
"ext": {
"bidder": {
"zone_id": 1,
"app_auth_token": "4fd53a12b78af4b39835de9e449c3082"
}
}
}
],
"site": {
"domain": "example.com",
"page": "example.com"
},
"device": {
"ip": "152.193.6.74",
"os": "android"
},
"user": {
"id": "db089de9-a62e-4861-a881-0ff15e052516",
"buyeruid": "8299345306627569435"
},
"tmax": 500
},
"httpcalls": [
{
"expectedRequest": {
"uri": "http://example.com/prebid?apptoken=4fd53a12b78af4b39835de9e449c3082&zoneid=1",
"body": {
"id": "some-req-id",
"imp": [
{
"id": "some-imp-id",
"ext": {
"bidder": {
"zone_id": 1,
"app_auth_token": "4fd53a12b78af4b39835de9e449c3082"
}
},
"native": {
"request": "{\"ver\":\"1.1\",\"context\":1,\"contextsubtype\":11,\"plcmttype\":4,\"plcmtcnt\":1,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":500}},{\"id\":2,\"required\":1,\"img\":{\"type\":3,\"wmin\":1,\"hmin\":1}},{\"id\":3,\"required\":0,\"data\":{\"type\":1,\"len\":200}},{\"id\":4,\"required\":0,\"data\":{\"type\":2,\"len\":15000}},{\"id\":5,\"required\":0,\"data\":{\"type\":6,\"len\":40}},{\"id\":6,\"required\":0,\"data\":{\"type\":500}}]}",
"ver": "1.1"
}
}
],
"site": {
"domain": "example.com",
"page": "example.com"
},
"device": {
"ip": "152.193.6.74",
"os": "android"
},
"user": {
"id": "db089de9-a62e-4861-a881-0ff15e052516",
"buyeruid": "8299345306627569435"
},
"tmax": 500
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "some-req-id",
"seatbid": [
{
"bid": [
{
"id": "928185755156387460",
"impid": "some-imp-id",
"price": 1,
"adid": "69595837",
"adm": "{\"assets\":[{\"id\": 2,\"img\":{\"url\":\"http://example.com/p/creative-image/5e/b6/de/c3/5eb6dec3-4854-4dcd-980a-347f36ab502e.jpg\",\"w\": 3000,\"h\": 2250}},{\"id\": 1,\"title\":{\"text\":\"This is an example Prebid Native creative\"}},{\"id\": 3,\"data\":{\"value\":\"Prebid.org\"}},{\"id\": 4,\"data\":{\"value\":\"This is a Prebid Native Creative. There are many like it, but this one is mine.\"}}],\"link\":{\"url\":\"http://example.com/click?AAAAAAAA8D8AAAAAAADwPwAAAAAAAAAAAAAAAAAA8D8AAAAAAADwPwhdYz3ZyNF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hEKBUNQARPwmQgxNDg0NzIzOIADAYgDAZADAJgDFKADAaoDAMADkBzIAwDYAwDgAwDoAwD4AwOABACSBAkvb3BlbnJ0YjKYBACiBAwxNTIuMTkzLjYuNzSoBJrMI7IEDAgAEAAYACAAMAA4ALgEAMAEAMgEANIEEWRlZmF1bHQjTllNMjo0MDIz2gQCCADgBADwBL3llyGIBQGYBQCgBf____8FA1ABqgULc29tZS1yZXEtaWTABQDJBQAFARTwP9IFCQkFC2QAAADYBQHgBQHwBd4C-gUECAAQAJAGAZgGAA..&s=08b1535744639c904684afe46e3c6c0e4786089f&test=1&referrer=prebid.org&pp=${AUCTION_PRICE}\"],\"jstracker\":\"<script src=\\\"http://www.dummyurl.js\\\"></script>\"}",
"adomain": [
"example.com"
],
"iurl": "http://example.com/cr?id=69595837",
"cid": "958",
"crid": "69595837",
"cat": [
"IAB3-1"
],
"ext": {}
}
],
"seat": "958"
}
],
"bidid": "8141327771600527856",
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "928185755156387460",
"impid": "some-imp-id",
"price": 1,
"adm": "{\"assets\":[{\"id\": 2,\"img\":{\"url\":\"http://example.com/p/creative-image/5e/b6/de/c3/5eb6dec3-4854-4dcd-980a-347f36ab502e.jpg\",\"w\": 3000,\"h\": 2250}},{\"id\": 1,\"title\":{\"text\":\"This is an example Prebid Native creative\"}},{\"id\": 3,\"data\":{\"value\":\"Prebid.org\"}},{\"id\": 4,\"data\":{\"value\":\"This is a Prebid Native Creative. There are many like it, but this one is mine.\"}}],\"link\":{\"url\":\"http://example.com/click?AAAAAAAA8D8AAAAAAADwPwAAAAAAAAAAAAAAAAAA8D8AAAAAAADwPwhdYz3ZyNFNG3fXpZUyLXNZ0o5aAAAAACrElgC-AwAAvgMAAAIAAAC98iUEeP4QAAAAAABVU0QAVVNEAAEAAQARIAAAAAABAgQCAAAAAAEAhBaSXgAAAAA./pp=${AUCTION_PRICE}/cnd=%21OwwGAQ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AA4ALgEAMAEAMgEANIEEWRlZmF1bHQjTllNMjo0MDIz2gQCCADgBADwBL3llyGIBQGYBQCgBf____8FA1ABqgULc29tZS1yZXEtaWTABQDJBQAFARTwP9IFCQkFC2QAAADYBQHgBQHwBd4C-gUECAAQAJAGAZgGAA..&s=08b1535744639c904684afe46e3c6c0e4786089f&test=1&referrer=prebid.org&pp=${AUCTION_PRICE}\"],\"jstracker\":\"<script src=\\\"http://www.dummyurl.js\\\"></script>\"}",
"adid": "69595837",
"adomain": [
"example.com"
],
"iurl": "http://example.com/cr?id=69595837",
"cid": "958",
"crid": "69595837",
"cat": [
"IAB3-1"
],
"ext": {}
},
"type": "native"
}
]
}
]
}
Loading