Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
Add LockerDome bid adapter (prebid#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
margsliu authored and mansinahar committed Oct 1, 2019
1 parent c225350 commit a3a7a15
Show file tree
Hide file tree
Showing 26 changed files with 942 additions and 0 deletions.
153 changes: 153 additions & 0 deletions adapters/lockerdome/lockerdome.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package lockerdome

import (
"encoding/json"
"fmt"
"net/http"

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

const unexpectedStatusCodeMessage = "Unexpected status code: %d. Run with request.debug = 1 for more info"

// Implements Bidder interface.
type LockerDomeAdapter struct {
endpoint string
}

// MakeRequests makes the HTTP requests which should be made to fetch bids [from the bidder, in this case, LockerDome]
func (adapter *LockerDomeAdapter) MakeRequests(openRTBRequest *openrtb.BidRequest, extraReqInfo *adapters.ExtraRequestInfo) (requestsToBidder []*adapters.RequestData, errs []error) {

numberOfImps := len(openRTBRequest.Imp)

if openRTBRequest.Imp == nil || numberOfImps == 0 { // lockerdometest/supplemental/empty_imps.json
err := &errortypes.BadInput{
Message: "No valid impressions in the bid request.",
}
errs = append(errs, err)
return nil, errs
}

var indexesOfValidImps []int
for i := 0; i < numberOfImps; i++ {
// LockerDome currently only supports banner impressions, and requires data in the ext field.
if openRTBRequest.Imp[i].Banner == nil { // lockerdometest/supplemental/unsupported_imp_type.json
err := &errortypes.BadInput{
Message: "LockerDome does not currently support non-banner types.",
}
errs = append(errs, err)
continue
}
var bidderExt adapters.ExtImpBidder
err := json.Unmarshal(openRTBRequest.Imp[i].Ext, &bidderExt)
if err != nil { // lockerdometest/supplemental/no_ext.json
err = &errortypes.BadInput{
Message: "ext was not provided.",
}
errs = append(errs, err)
continue
}
var lockerdomeExt openrtb_ext.ExtImpLockerDome
err = json.Unmarshal(bidderExt.Bidder, &lockerdomeExt)
if err != nil { // lockerdometest/supplemental/no_adUnitId_param.json
err = &errortypes.BadInput{
Message: "ext.bidder.adUnitId was not provided.",
}
errs = append(errs, err)
continue
}
if lockerdomeExt.AdUnitId == "" { // lockerdometest/supplemental/empty_adUnitId_param.json
err := &errortypes.BadInput{
Message: "ext.bidder.adUnitId is empty.",
}
errs = append(errs, err)
continue
}
indexesOfValidImps = append(indexesOfValidImps, i)
}
if numberOfImps > len(indexesOfValidImps) {
var validImps []openrtb.Imp
for j := 0; j < len(indexesOfValidImps); j++ {
validImps = append(validImps, openRTBRequest.Imp[j])
}
if len(validImps) == 0 {
err := &errortypes.BadInput{
Message: "No valid or supported impressions in the bid request.",
}
errs = append(errs, err)
return nil, errs
} else {
openRTBRequest.Imp = validImps
}
}
openRTBRequestJSON, err := json.Marshal(openRTBRequest)
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{
Method: "POST",
Uri: adapter.endpoint,
Body: openRTBRequestJSON,
Headers: headers,
}

requestsToBidder = append(requestsToBidder, requestToBidder)

return requestsToBidder, nil

}

// MakeBids unpacks the server's response into Bids.
func (adapter *LockerDomeAdapter) MakeBids(openRTBRequest *openrtb.BidRequest, requestToBidder *adapters.RequestData, bidderRawResponse *adapters.ResponseData) (bidderResponse *adapters.BidderResponse, errs []error) {

if bidderRawResponse.StatusCode == http.StatusNoContent {
return nil, nil
}

if bidderRawResponse.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf(unexpectedStatusCodeMessage, bidderRawResponse.StatusCode),
}}
}

if bidderRawResponse.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf(unexpectedStatusCodeMessage, bidderRawResponse.StatusCode),
}}
}

var openRTBBidderResponse openrtb.BidResponse
if err := json.Unmarshal(bidderRawResponse.Body, &openRTBBidderResponse); err != nil {
return nil, []error{
fmt.Errorf("Error unmarshaling LockerDome bid response - %s", err.Error()),
}
}

if len(openRTBBidderResponse.SeatBid) == 0 {
return nil, nil
}

bidderResponse = adapters.NewBidderResponseWithBidsCapacity(len(openRTBBidderResponse.SeatBid[0].Bid))

for _, seatBid := range openRTBBidderResponse.SeatBid {
for i := range seatBid.Bid {
typedBid := adapters.TypedBid{Bid: &seatBid.Bid[i], BidType: openrtb_ext.BidTypeBanner}
bidderResponse.Bids = append(bidderResponse.Bids, &typedBid)
}
}
return bidderResponse, nil
}

func NewLockerDomeBidder(endpoint string) *LockerDomeAdapter {
return &LockerDomeAdapter{endpoint: endpoint}
}
10 changes: 10 additions & 0 deletions adapters/lockerdome/lockerdome_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package lockerdome

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

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "lockerdometest", NewLockerDomeBidder("https://lockerdome.com/ladbid/prebidserver/openrtb2"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {
"adUnitId": "LD9434769725128806"
}
}
},
{
"id": "test-imp-id-2",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {
"adUnitId": "LD9360401292656742"
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://lockerdome.com/ladbid/prebidserver/openrtb2",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {
"adUnitId": "LD9434769725128806"
}
}
},
{
"id": "test-imp-id-2",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {
"adUnitId": "LD9360401292656742"
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "lockerdome",
"bid": [
{
"id": "729c0d99-3b45-4225-9ad5-64aef2b46df3",
"impid": "test-imp-id",
"price": 0.23,
"adm": "<html>test ad markup #1</html>",
"crid": "LD12134555163033624",
"h": 250,
"w": 300
},
{
"id": "9euij6w8-sj89-6397-6er4-5we9t1a67wkj",
"impid": "test-imp-id-2",
"price": 0.5,
"adm": "<html>test ad markup #2</html>",
"crid": "LD12258417590730764",
"h": 250,
"w": 300
}
]
}
]
}
}
}
],

"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "729c0d99-3b45-4225-9ad5-64aef2b46df3",
"impid": "test-imp-id",
"price": 0.23,
"adm": "<html>test ad markup #1</html>",
"crid": "LD12134555163033624",
"w": 300,
"h": 250
},
"type": "banner"
},
{
"bid": {
"id": "9euij6w8-sj89-6397-6er4-5we9t1a67wkj",
"impid": "test-imp-id-2",
"price": 0.5,
"adm": "<html>test ad markup #2</html>",
"crid": "LD12258417590730764",
"w": 300,
"h": 250
},
"type": "banner"
}
]
}
]
}
Loading

0 comments on commit a3a7a15

Please sign in to comment.