-
Notifications
You must be signed in to change notification settings - Fork 764
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 Adpone adapter #1033
Add Adpone adapter #1033
Changes from 17 commits
4f78ae6
a510e13
24ae016
951623b
8925084
8b2f277
37b905a
00b4876
2630c8a
c874e91
9f0cf2d
de01472
7991a07
9d35616
1cbc23d
93f1479
99368d4
30f3ae6
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,116 @@ | ||
package adpone | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
"net/http" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/prebid/prebid-server/errortypes" | ||
) | ||
|
||
func NewAdponeBidder(endpoint string) *adponeAdapter { | ||
return &adponeAdapter{endpoint: endpoint} | ||
} | ||
|
||
type adponeAdapter struct { | ||
endpoint string | ||
} | ||
|
||
func (adapter *adponeAdapter) MakeRequests( | ||
openRTBRequest *openrtb.BidRequest, | ||
reqInfo *adapters.ExtraRequestInfo, | ||
) ( | ||
requestsToBidder []*adapters.RequestData, | ||
errs []error, | ||
) { | ||
var imp = &openRTBRequest.Imp[0] | ||
var bidderExt adapters.ExtImpBidder | ||
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { | ||
errs = append(errs, newBadInputError(err.Error())) | ||
} | ||
var ttxExt openrtb_ext.ExtAdpone | ||
if err := json.Unmarshal(bidderExt.Bidder, &ttxExt); err != nil { | ||
errs = append(errs, newBadInputError(err.Error())) | ||
} | ||
if len(openRTBRequest.Imp) == 0 { | ||
errs = append(errs, newBadInputError("No impression in the bid request")) | ||
return nil, errs | ||
} | ||
openRTBRequestJSON, err := json.Marshal(openRTBRequest) | ||
guscarreon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
errs = append(errs, err) | ||
return nil, errs | ||
} | ||
|
||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
headers.Add("Accept", "application/json") | ||
headers.Add("x-openrtb-version", "2.5") | ||
|
||
requestToBidder := &adapters.RequestData{ | ||
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. Do you think more headers might be helpful? Other adapters include headers such as x-openrtb-version. Do you think it'll help to include more header info?
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. Absolutely yes; I will add more headers. Thanks a lot |
||
Method: "POST", | ||
Uri: adapter.endpoint, | ||
Body: openRTBRequestJSON, | ||
Headers: headers, | ||
} | ||
requestsToBidder = append(requestsToBidder, requestToBidder) | ||
|
||
return requestsToBidder, errs | ||
} | ||
|
||
const unexpectedStatusCodeFormat = "" + | ||
"Unexpected status code: %d. Run with request.debug = 1 for more info" | ||
|
||
func (adapter *adponeAdapter) MakeBids( | ||
openRTBRequest *openrtb.BidRequest, | ||
requestToBidder *adapters.RequestData, | ||
bidderRawResponse *adapters.ResponseData, | ||
) ( | ||
bidderResponse *adapters.BidderResponse, | ||
errs []error, | ||
) { | ||
switch bidderRawResponse.StatusCode { | ||
case http.StatusOK: | ||
break | ||
case http.StatusNoContent: | ||
return nil, nil | ||
case http.StatusBadRequest: | ||
err := &errortypes.BadInput{ | ||
Message: fmt.Sprintf(unexpectedStatusCodeFormat, bidderRawResponse.StatusCode), | ||
} | ||
return nil, []error{err} | ||
default: | ||
err := &errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf(unexpectedStatusCodeFormat, bidderRawResponse.StatusCode), | ||
} | ||
return nil, []error{err} | ||
} | ||
|
||
var openRTBBidderResponse openrtb.BidResponse | ||
if err := json.Unmarshal(bidderRawResponse.Body, &openRTBBidderResponse); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
bidsCapacity := len(openRTBBidderResponse.SeatBid[0].Bid) | ||
bidderResponse = adapters.NewBidderResponseWithBidsCapacity(bidsCapacity) | ||
var typedBid *adapters.TypedBid | ||
for _, seatBid := range openRTBBidderResponse.SeatBid { | ||
for _, bid := range seatBid.Bid { | ||
bid := bid | ||
typedBid = &adapters.TypedBid{Bid: &bid, BidType: "banner"} | ||
bidderResponse.Bids = append(bidderResponse.Bids, typedBid) | ||
} | ||
} | ||
|
||
return bidderResponse, nil | ||
|
||
} | ||
|
||
func newBadInputError(message string) error { | ||
return &errortypes.BadInput{ | ||
Message: message, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package adpone | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/adapters/adapterstest" | ||
) | ||
|
||
const testsDir = "adponetest" | ||
const testsBidderEndpoint = "http://localhost/prebid_server" | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
adapterstest.RunJSONBidderTest(t, testsDir, NewAdponeBidder(testsBidderEndpoint)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"site": { | ||
"page": "https://good.site/url" | ||
}, | ||
"imp": [{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [{ | ||
"w": 300, | ||
"h": 250 | ||
}] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"placementId": "fake-placement-id" | ||
} | ||
} | ||
}] | ||
}, | ||
|
||
"httpCalls": [{ | ||
"expectedRequest": { | ||
"uri": "http://localhost/prebid_server", | ||
"body": { | ||
"id": "test-request-id", | ||
"site": { | ||
"page": "https://good.site/url" | ||
}, | ||
"imp": [{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [{ | ||
"w": 300, | ||
"h": 250 | ||
}] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"placementId": "fake-placement-id" | ||
} | ||
} | ||
}] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [{ | ||
"seat": "adpone", | ||
"bid": [{ | ||
"id": "randomid", | ||
"impid": "test-imp-id", | ||
"price": 0.500000, | ||
"adid": "12345678", | ||
"adm": "some-test-ad", | ||
"cid": "987", | ||
"crid": "12345678", | ||
"h": 250, | ||
"w": 300 | ||
}] | ||
}], | ||
"cur": "EUR" | ||
} | ||
} | ||
}], | ||
|
||
"expectedBidResponses": [{ | ||
"currency": "EUR", | ||
"bids": [{ | ||
"bid": { | ||
"id": "randomid", | ||
"impid": "test-imp-id", | ||
"price": 0.5, | ||
"adm": "some-test-ad", | ||
"adid": "12345678", | ||
"cid": "987", | ||
"crid": "12345678", | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"type": "banner" | ||
}] | ||
}] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"placementId": "fake-placement-id" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
|
||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://localhost/prebid_server", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"placementId": "fake-placement-id" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": "{\"id\"data.lost" | ||
} | ||
} | ||
], | ||
|
||
"expectedMakeBidsErrors": [ | ||
"json: cannot unmarshal string into Go value of type openrtb.BidResponse" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
|
||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://localhost/prebid_server", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 204, | ||
"body": {} | ||
} | ||
} | ||
], | ||
|
||
"expectedBidResponses": [] | ||
} |
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.
When a request with an empty
openRTBRequest.Imp
array comes in, your adapter fails with an "index out of range" error in line 29. Can we check fornil
before dereferencing theImp
array and check its length before accessing any of its elements?