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

New Adapter: BrightMountainMedia #1855

Merged
merged 12 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
160 changes: 160 additions & 0 deletions adapters/bmtm/brightmountainmedia.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package bmtm

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

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

type BrightMountainMediaAdapter struct {
BrightMountainMediaInc marked this conversation as resolved.
Show resolved Hide resolved
endpoint string
}

// Builder builds a new instance of the BrightMountainMedia adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &BrightMountainMediaAdapter{
endpoint: config.Endpoint,
}
return bidder, nil
}

func (a *BrightMountainMediaAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var extRequests []*adapters.RequestData
var errs []error

for _, imp := range request.Imp {
extRequest, err := a.makeRequest(*request, imp)
if err != nil {
errs = append(errs, err)
} else {
extRequests = append(extRequests, extRequest)
}
}
return extRequests, errs
}

func (a *BrightMountainMediaAdapter) makeRequest(ortbRequest openrtb2.BidRequest, ortbImp openrtb2.Imp) (*adapters.RequestData, error) {
copiedImp, err := processImp(ortbImp)
if err != nil {
return nil, err
}

ortbRequest.Imp = []openrtb2.Imp{*copiedImp}

requestJSON, err := json.Marshal(ortbRequest)
if err != nil {
return nil, err
}

requestData := &adapters.RequestData{
Method: http.MethodPost,
Uri: a.endpoint,
Body: requestJSON,
Headers: setHeaders(ortbRequest),
}
return requestData, nil
}

func (a *BrightMountainMediaAdapter) MakeBids(internalRequest *openrtb2.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("Unknown status code: %d.", response.StatusCode),
}}
}

if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unknown status code: %d.", response.StatusCode),
}}
}

var bidResp openrtb2.BidResponse

if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)

for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: getMediaTypeForBid(sb.Bid[i].ImpID, internalRequest.Imp),
})
}
}
return bidResponse, nil
}

func setHeaders(request openrtb2.BidRequest) http.Header {
headers := http.Header{}

headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")

if request.Device != nil {
headers.Add("User-Agent", request.Device.UA)
BrightMountainMediaInc marked this conversation as resolved.
Show resolved Hide resolved

if request.Device.IP != "" {
headers.Add("X-Forwarded-For", request.Device.IP)
} else if request.Device.IPv6 != "" {
headers.Add("X-Forwarded-For", request.Device.IPv6)
}
}

if request.Site != nil {
headers.Add("Referer", request.Site.Page)
}
return headers
}

func processImp(imp openrtb2.Imp) (*openrtb2.Imp, error) {
if imp.Banner == nil && imp.Video == nil && imp.Native == nil {
BrightMountainMediaInc marked this conversation as resolved.
Show resolved Hide resolved
return nil, &errortypes.BadInput{
Message: fmt.Sprintf("For Imp ID %s Banner or Video is undefined", imp.ID),
}
}

var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Unknown BrightMountainMedia ExtImpBidder",
BrightMountainMediaInc marked this conversation as resolved.
Show resolved Hide resolved
}
}

var bmtmExt openrtb_ext.ImpExtBmtm
if err := json.Unmarshal(bidderExt.Bidder, &bmtmExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Unknown BrightMountainMedia bidder ext",
BrightMountainMediaInc marked this conversation as resolved.
Show resolved Hide resolved
}
}

imp.TagID = strconv.Itoa(bmtmExt.PlacementID)
imp.Ext = nil
return &imp, nil
BrightMountainMediaInc marked this conversation as resolved.
Show resolved Hide resolved
}

func getMediaTypeForBid(impID string, imps []openrtb2.Imp) openrtb_ext.BidType {
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner
} else if imp.Video != nil {
return openrtb_ext.BidTypeVideo
}
}
}
return openrtb_ext.BidTypeBanner
}
20 changes: 20 additions & 0 deletions adapters/bmtm/brightmountainmedia_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package bmtm

import (
"testing"

"github.com/prebid/prebid-server/adapters/adapterstest"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
)

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderBmtm, config.Adapter{
Endpoint: "https://one.elitebidder.com/api/pbs"})
BrightMountainMediaInc marked this conversation as resolved.
Show resolved Hide resolved

if buildErr != nil {
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
BrightMountainMediaInc marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("Builder returned unexpected error %v", buildErr)
}

adapterstest.RunJSONBidderTest(t, "brightmountainmediatest", bidder)
}
95 changes: 95 additions & 0 deletions adapters/bmtm/brightmountainmediatest/exemplary/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"mockBidRequest": {
"id": "test-request-id",
"site": {
"page": "prebid.org"
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 728,
"h": 90
}
]
},
"ext": {
"bidder": {
"placement_id": 329
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://one.elitebidder.com/api/pbs",
"body": {
"id": "test-request-id",
"site": {
"page": "prebid.org"
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 728,
"h": 90
}
]
},
"tagid": "329"
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "bmtm",
"bid": [
{
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.500000,
"adm": "some-test-ad",
"crid": "crid_10",
"h": 90,
"w": 728
}
]
}
],
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-test-ad",
"crid": "crid_10",
"w": 728,
"h": 90
},
"type": "banner"
}
]
}
]
}
Loading