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 Adpone adapter #1033

Merged
merged 18 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
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
116 changes: 116 additions & 0 deletions adapters/adpone/adpone.go
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]
Copy link
Contributor

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 for nil before dereferencing the Imp array and check its length before accessing any of its elements?

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{
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

headers.Add("Accept", "application/json")
headers.Add("x-openrtb-version", "2.5")

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
}
}
14 changes: 14 additions & 0 deletions adapters/adpone/adpone_test.go
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))
}
87 changes: 87 additions & 0 deletions adapters/adpone/adponetest/exemplary/simple-banner.json
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"
}]
}]
}
1 change: 1 addition & 0 deletions adapters/adpone/adponetest/params/race/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
60 changes: 60 additions & 0 deletions adapters/adpone/adponetest/supplemental/bad_response.json
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"
]
}
56 changes: 56 additions & 0 deletions adapters/adpone/adponetest/supplemental/status_204.json
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": []
}
Loading