-
Notifications
You must be signed in to change notification settings - Fork 758
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
engagebdr adapter #966
Changes from 11 commits
49229ce
0289413
9cd0251
8f56e29
0b7abc7
e8aeb8d
6128fef
852ce0c
a79774f
722678e
129b3ac
fcd3a54
146defe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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), | ||
}) | ||
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), | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as line 35 above. Seems like a |
||
continue | ||
} | ||
if impExt.Sspid == "" { | ||
errors = append(errors, &errortypes.BadInput{ | ||
Message: fmt.Sprintf("Ignoring imp id=%s, no sspid present.", imp.ID), | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't put a |
||
continue | ||
} | ||
sspidImps[impExt.Sspid] = append(sspidImps[impExt.Sspid], imp) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
} |
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")) | ||
} |
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" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
There was a problem hiding this comment.
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 followingif
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.