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

engagebdr adapter #966

Merged
merged 13 commits into from
Sep 11, 2019
Merged
150 changes: 150 additions & 0 deletions adapters/engagebdr/engagebdr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package engagebdr

import (
"encoding/json"
"github.com/prebid/prebid-server/openrtb_ext"
"net/http"

"fmt"

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

type EngageBDRAdapter struct {
http *adapters.HTTPAdapter
URI string
}

func (adapter *EngageBDRAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {

errors := make([]error, 0, len(request.Imp))

if request.Imp == nil || len(request.Imp) == 0 {
errors = append(errors, &errortypes.BadInput{
Message: fmt.Sprintf("Invalid BidRequest. No valid imp."),
})
return nil, errors
}

// EngageBDR uses different sspid parameters for banner and video.
sspidImps := make(map[string][]openrtb.Imp)
for _, imp := range request.Imp {

if imp.Audio != nil {
errors = append(errors, &errortypes.BadInput{
Message: fmt.Sprintf("Ignoring imp id=%s, invalid MediaType. EngageBDR only supports Banner, Video and Native.", imp.ID),
})
continue
}

var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
errors = append(errors, &errortypes.BadInput{
Message: fmt.Sprintf("Ignoring imp id=%s, error while decoding extImpBidder, err: %s.", imp.ID, err),
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Would a continue statement make sense after this line? If we have an error for some reason, I believe the following if statement found in line 39 will most likely also print an error (if not an NULL pointer de-reference in line 38) and we'll end up with a stack of errors that have one common source.

Breakpoint 1 set at 0x1626558 for github.com/prebid/prebid-server/adapters/engagebdr.(*EngageBDRAdapter).MakeRequests() ./engagebdr.go:32
(dlv) c
> github.com/prebid/prebid-server/adapters/engagebdr.(*EngageBDRAdapter).MakeRequests() ./engagebdr.go:32 (hits goroutine(21):1 total:1) (PC: 0x1626558)
    27:                 if imp.Native != nil {
    28:                         // filter native imps from bid request
    29:                         continue
    30:                 }
    31:                 var bidderExt adapters.ExtImpBidder
=>  32:                 if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
    33:                         errors = append(errors, &errortypes.BadInput{
    34:                                 Message: fmt.Sprintf("Ignoring imp id=%s, error while decoding extImpBidder, err: %s", imp.ID, err),
    35:                         })
    36:                 }
    37:                 impExt := openrtb_ext.ExtImpEngageBDR{}
    38:					err := json.Unmarshal(bidderExt.Bidder, &impExt)
    39:					if err != nil {
(dlv) p bidderExt.Bidder
encoding/json.RawMessage len: 0, cap: 0, nil

continue
}
impExt := openrtb_ext.ExtImpEngageBDR{}
err := json.Unmarshal(bidderExt.Bidder, &impExt)
if err != nil {
errors = append(errors, &errortypes.BadInput{
Message: fmt.Sprintf("Ignoring imp id=%s, error while decoding impExt, err: %s.", imp.ID, err),
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as line 35 above. Seems like a continue statement would be useful because we'll de-reference impExt in line 44 and there's a possibility that the Unmarshal didn't go well by then.

continue
}
if impExt.Sspid == "" {
errors = append(errors, &errortypes.BadInput{
Message: fmt.Sprintf("Ignoring imp id=%s, no sspid present.", imp.ID),
})
Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't put a continue statement here, we'd be mapping an empty impExt.Sspid value into sspidImps

continue
}
sspidImps[impExt.Sspid] = append(sspidImps[impExt.Sspid], imp)
}

Copy link
Contributor

@mansinahar mansinahar Jul 19, 2019

Choose a reason for hiding this comment

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

Just a suggestion: Since you're adapter only supports banner and video ads, might be a good idea to add a check somewhere here in this function to discard imps of any other type. I am sure this must be something that you'd be already handling on your end but it's always better to check and fail early.

var adapterRequests []*adapters.RequestData

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

for sspid, imps := range sspidImps {
if len(imps) > 0 {
// Make a copy as we don't want to change the original request
reqCopy := *request
reqCopy.Imp = imps
reqJSON, err := json.Marshal(reqCopy)
if err != nil {
errors = append(errors, err)
return nil, errors
}
adapterReq := adapters.RequestData{
Method: "POST",
Uri: adapter.URI + "?zoneid=" + sspid,
Body: reqJSON,
Headers: headers,
}
adapterRequests = append(adapterRequests, &adapterReq)
}
}

return adapterRequests, errors
}

func (adapter *EngageBDRAdapter) 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 bidResp openrtb.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)

for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp),
})
}
}
return bidResponse, nil
}

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
}

func NewEngageBDRBidder(client *http.Client, endpoint string) *EngageBDRAdapter {
adapter := &adapters.HTTPAdapter{Client: client}
return &EngageBDRAdapter{
http: adapter,
URI: endpoint,
}
}
11 changes: 11 additions & 0 deletions adapters/engagebdr/engagebdr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package engagebdr

import (
"github.com/prebid/prebid-server/adapters/adapterstest"
"net/http"
"testing"
)

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "engagebdrtest", NewEngageBDRBidder(new(http.Client), "http://dsp.bnmla.com/hb"))
}
97 changes: 97 additions & 0 deletions adapters/engagebdr/engagebdrtest/exemplary/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"w": 300,
"h": 250
},
"ext": {
"bidder": {
"sspid": "99999"
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "http://dsp.bnmla.com/hb?zoneid=99999",
"body":{
"id": "test-request-id",
"imp": [{
"id": "test-imp-id",
"banner": {
"w": 300,
"h": 250
},
"ext": {
"bidder": {
"sspid":"99999"
}
}
}]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id" : "test-imp-id",
"impid": "test-imp-id",
"price": 9.81,
"adid": "abcde-12345",
"adm": "<div><img src=\"https://cdn0.bnmla.com/0b1c6e85e9376e3092df8c9fc8ab9095.gif\" width=350 height=250 /></div>",
"adomain": [
"advertiserdomain.com"
],
"iurl": "http://match.bnmla.com/usersync?sspid=59&redir=",
"cid": "campaign1",
"crid": "abcde-12345",
"w": 300,
"h": 250
}
],
"seat": "test-request-id"
}
],
"bidid": "test-request-id",
"cur": "USD"
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-imp-id",
"impid": "test-imp-id",
"price": 9.81,
"adid": "abcde-12345",
"adm": "<div><img src=\"https://cdn0.bnmla.com/0b1c6e85e9376e3092df8c9fc8ab9095.gif\" width=350 height=250 /></div>",
"adomain": ["advertiserdomain.com"],
"iurl": "http://match.bnmla.com/usersync?sspid=59&redir=",
"cid": "campaign1",
"crid": "abcde-12345",
"w": 300,
"h": 250
},
"type": "banner"
}
]
}
]
}

Loading