-
Notifications
You must be signed in to change notification settings - Fork 832
New Adapter: MadSense #4169
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: MadSense #4169
Changes from 3 commits
c6dc0a9
33446bb
2db4f8f
c1696fb
ed25697
cc05a36
a2b2389
be43393
7f0eba2
fc8f41d
4b8ba04
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,126 @@ | ||||||||||||||||
package madsense | ||||||||||||||||
|
||||||||||||||||
import ( | ||||||||||||||||
"encoding/json" | ||||||||||||||||
"net/http" | ||||||||||||||||
"net/url" | ||||||||||||||||
|
||||||||||||||||
"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/openrtb_ext" | ||||||||||||||||
"github.com/prebid/prebid-server/v3/util/jsonutil" | ||||||||||||||||
) | ||||||||||||||||
|
||||||||||||||||
type adapter struct { | ||||||||||||||||
endpoint string | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// Builder builds a new instance of the MadSense adapter for the given bidder with the given config. | ||||||||||||||||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { | ||||||||||||||||
bidder := &adapter{ | ||||||||||||||||
endpoint: config.Endpoint, | ||||||||||||||||
} | ||||||||||||||||
return bidder, nil | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (requests []*adapters.RequestData, errors []error) { | ||||||||||||||||
reqs := make([]*adapters.RequestData, 0, len(request.Imp)) | ||||||||||||||||
var errs []error | ||||||||||||||||
|
||||||||||||||||
appendReq := func(imps []openrtb2.Imp) { | ||||||||||||||||
req, err := a.makeRequest(request, imps) | ||||||||||||||||
if err != nil { | ||||||||||||||||
errors = append(errors, err) | ||||||||||||||||
return | ||||||||||||||||
} | ||||||||||||||||
if req != nil { | ||||||||||||||||
reqs = append(reqs, req) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
var videoImps, bannerImps []openrtb2.Imp | ||||||||||||||||
for _, imp := range request.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. Here as well, you appear to make a copy of the slice, perhaps you can use a reference to avoid the copy?
Suggested change
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. done |
||||||||||||||||
if imp.Banner != nil { | ||||||||||||||||
bannerImps = append(bannerImps, imp) | ||||||||||||||||
} else if imp.Video != nil { | ||||||||||||||||
videoImps = append(videoImps, imp) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// we support video podding, so we want to send all video impressions in a single request | ||||||||||||||||
appendReq(videoImps) | ||||||||||||||||
for _, bannerImp := range bannerImps { | ||||||||||||||||
appendReq([]openrtb2.Imp{bannerImp}) | ||||||||||||||||
} | ||||||||||||||||
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. suggestion: if merely appending, perhaps you wanted to just take sub-slice?
Suggested change
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. done, moved to the loop before |
||||||||||||||||
|
||||||||||||||||
return reqs, errs | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
func (a *adapter) makeRequest(request *openrtb2.BidRequest, imps []openrtb2.Imp) (*adapters.RequestData, error) { | ||||||||||||||||
if len(imps) == 0 { | ||||||||||||||||
return nil, nil | ||||||||||||||||
} | ||||||||||||||||
ext, err := parseImpExt(&imps[0]) | ||||||||||||||||
if err != nil { | ||||||||||||||||
return nil, err | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
request.Imp = imps | ||||||||||||||||
body, err := json.Marshal(request) | ||||||||||||||||
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. Please use jsonutil for marshaling 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. Done! |
||||||||||||||||
if err != nil { | ||||||||||||||||
return nil, err | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if request.Test == 1 { | ||||||||||||||||
ext.CompanyId = "test" | ||||||||||||||||
bsardo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
} | ||||||||||||||||
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. When 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. Please see my earlier comment. 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. done, added a local variable |
||||||||||||||||
|
||||||||||||||||
return &adapters.RequestData{ | ||||||||||||||||
Method: http.MethodPost, | ||||||||||||||||
Uri: a.getEndpointURL(ext), | ||||||||||||||||
Body: body, | ||||||||||||||||
Headers: getHeaders(request), | ||||||||||||||||
ImpIDs: openrtb_ext.GetImpIDs(request.Imp), | ||||||||||||||||
}, nil | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
func (a *adapter) getEndpointURL(ext *openrtb_ext.ExtImpMadSense) string { | ||||||||||||||||
params := url.Values{} | ||||||||||||||||
params.Add("company_id", ext.CompanyId) | ||||||||||||||||
return a.endpoint + "?" + params.Encode() | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
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{err} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
var resp openrtb2.BidResponse | ||||||||||||||||
if err := jsonutil.Unmarshal(response.Body, &resp); err != nil { | ||||||||||||||||
return nil, []error{&errortypes.BadServerResponse{ | ||||||||||||||||
Message: "Bad Server Response", | ||||||||||||||||
}} | ||||||||||||||||
bsardo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
var bidErrors []error | ||||||||||||||||
bidderResponse := adapters.NewBidderResponseWithBidsCapacity(1) | ||||||||||||||||
for _, seatBid := range resp.SeatBid { | ||||||||||||||||
for i := range seatBid.Bid { | ||||||||||||||||
bid := &seatBid.Bid[i] | ||||||||||||||||
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. suggestion: Avoid copy of the seatbid as well
Suggested change
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. done |
||||||||||||||||
typedBid, err := getTypedBidFromBid(bid) | ||||||||||||||||
if err != nil { | ||||||||||||||||
bidErrors = append(bidErrors, err) | ||||||||||||||||
continue | ||||||||||||||||
} | ||||||||||||||||
bidderResponse.Bids = append(bidderResponse.Bids, typedBid) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
return bidderResponse, bidErrors | ||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||||||
package madsense | ||||||||||
|
||||||||||
import ( | ||||||||||
"github.com/prebid/prebid-server/v3/adapters/adapterstest" | ||||||||||
"github.com/prebid/prebid-server/v3/config" | ||||||||||
"github.com/prebid/prebid-server/v3/openrtb_ext" | ||||||||||
"testing" | ||||||||||
) | ||||||||||
|
||||||||||
func TestJsonSamples(t *testing.T) { | ||||||||||
bidder, buildErr := Builder(openrtb_ext.BidderMadSense, config.Adapter{ | ||||||||||
Endpoint: "https://ads.madsense.io/pbs"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||||||||||
bsardo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
if buildErr != nil { | ||||||||||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||||||||||
} | ||||||||||
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. suggestion: Prefer the testify assertions/requirements
Suggested change
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. Thank you, changed in this and params test |
||||||||||
adapterstest.RunJSONBidderTest(t, "madsensetest", bidder) | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"company_id": "9876543" | ||
} | ||
} | ||
} | ||
], | ||
"device": { | ||
"ua": "user-agent", | ||
"ip": "1.2.3.4" | ||
}, | ||
"app": { | ||
"bundle": "54321" | ||
} | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"method": "POST", | ||
"headers": { | ||
"Accept": [ | ||
"application/json" | ||
], | ||
"Content-Type": [ | ||
"application/json;charset=utf-8" | ||
], | ||
"X-Openrtb-Version": [ | ||
"2.6" | ||
], | ||
"User-Agent": [ | ||
"user-agent" | ||
], | ||
"X-Forwarded-For": [ | ||
"1.2.3.4" | ||
] | ||
}, | ||
"uri": "https://ads.madsense.io/pbs?company_id=9876543", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"company_id": "9876543" | ||
} | ||
} | ||
} | ||
], | ||
"device": { | ||
"ua": "user-agent", | ||
"ip": "1.2.3.4" | ||
}, | ||
"app": { | ||
"bundle": "54321" | ||
} | ||
}, | ||
"impIDs":["test-imp-id"] | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"cur": "USD", | ||
"seatbid": [ | ||
{ | ||
"bid": [ | ||
{ | ||
"id": "884f46aa-25ab-488a-bd7e-90e3a31a03e0", | ||
"crid": "b83e6e58-9342-4e8c-bf6a-4ad09b231547", | ||
"price": 10, | ||
"impid": "test-imp-id", | ||
"adm": "<img src=\"demo ad\" width=\"300\" height=\"250\">", | ||
"mtype": 1 | ||
} | ||
] | ||
} | ||
], | ||
"seat": "madsense" | ||
} | ||
} | ||
} | ||
], | ||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "884f46aa-25ab-488a-bd7e-90e3a31a03e0", | ||
"crid": "b83e6e58-9342-4e8c-bf6a-4ad09b231547", | ||
"price": 10, | ||
"impid": "test-imp-id", | ||
"adm": "<img src=\"demo ad\" width=\"300\" height=\"250\">", | ||
"mtype": 1 | ||
}, | ||
"type": "banner" | ||
} | ||
], | ||
"seat": "madsense" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"company_id": "9876543" | ||
} | ||
} | ||
} | ||
], | ||
"device": { | ||
"ua": "user-agent", | ||
"ip": "1.2.3.4" | ||
}, | ||
"site": { | ||
"domain": "domain.com", | ||
"page": "http://domain.com/page" | ||
} | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"method": "POST", | ||
"headers": { | ||
"Accept": [ | ||
"application/json" | ||
], | ||
"Content-Type": [ | ||
"application/json;charset=utf-8" | ||
], | ||
"Origin": [ | ||
"domain.com" | ||
], | ||
"X-Openrtb-Version": [ | ||
"2.6" | ||
], | ||
"User-Agent": [ | ||
"user-agent" | ||
], | ||
"X-Forwarded-For": [ | ||
"1.2.3.4" | ||
] | ||
}, | ||
"uri": "https://ads.madsense.io/pbs?company_id=9876543", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"company_id": "9876543" | ||
} | ||
} | ||
} | ||
], | ||
"device": { | ||
"ua": "user-agent", | ||
"ip": "1.2.3.4" | ||
}, | ||
"site": { | ||
"domain": "domain.com", | ||
"page": "http://domain.com/page" | ||
} | ||
}, | ||
"impIDs":["test-imp-id"] | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"cur": "USD", | ||
"seatbid": [ | ||
{ | ||
"bid": [ | ||
{ | ||
"id": "884f46aa-25ab-488a-bd7e-90e3a31a03e0", | ||
"crid": "b83e6e58-9342-4e8c-bf6a-4ad09b231547", | ||
"price": 10, | ||
"impid": "test-imp-id", | ||
"adm": "<iframe width=\"300\" height=\"250\"></iframe>", | ||
"mtype": 1 | ||
} | ||
] | ||
} | ||
], | ||
"seat": "madsense" | ||
} | ||
} | ||
} | ||
], | ||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "884f46aa-25ab-488a-bd7e-90e3a31a03e0", | ||
"crid": "b83e6e58-9342-4e8c-bf6a-4ad09b231547", | ||
"price": 10, | ||
"impid": "test-imp-id", | ||
"adm": "<iframe width=\"300\" height=\"250\"></iframe>", | ||
"mtype": 1 | ||
}, | ||
"type": "banner" | ||
} | ||
], | ||
"seat": "madsense" | ||
} | ||
] | ||
} |
Uh oh!
There was an error while loading. Please reload this page.