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

Commit

Permalink
[Mgid] Add new Mgid Adapter (prebid#907)
Browse files Browse the repository at this point in the history
* new mgid adapter

* increase coverage

* remove extra imports
  • Loading branch information
Gaudeamus authored and hhhjort committed Jun 5, 2019
1 parent afc9ffe commit 6e3388c
Show file tree
Hide file tree
Showing 21 changed files with 1,025 additions and 0 deletions.
1 change: 1 addition & 0 deletions adapters/mgid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Please contact <[email protected]> if you would like to build and deploy Prebid server and use it with Mgid.
169 changes: 169 additions & 0 deletions adapters/mgid/mgid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package mgid

import (
"bytes"
"encoding/json"
"fmt"
"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
"net/http"
)

type MgidAdapter struct {
endpoint string
}

type ReqExt struct {
PlacementId string `json:"placementId"`
AccountId string `json:"accountId"`
}

type RespBidExt struct {
CreativeType openrtb_ext.BidType `json:"crtype"`
}

func (a *MgidAdapter) MakeRequests(request *openrtb.BidRequest) (adapterRequests []*adapters.RequestData, errs []error) {

adapterReq, errs := a.makeRequest(request)
if adapterReq != nil && len(errs) == 0 {
adapterRequests = append(adapterRequests, adapterReq)
}

return
}

func (a *MgidAdapter) makeRequest(request *openrtb.BidRequest) (*adapters.RequestData, []error) {
var errs []error

path, err := preprocess(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}

// Last Step
reqJSON, err := json.Marshal(request)
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")

return &adapters.RequestData{
Method: "POST",
Uri: a.endpoint + path,
Body: reqJSON,
Headers: headers,
}, errs
}

// Mutate the request to get it ready to send to yieldmo.
func preprocess(request *openrtb.BidRequest) (path string, err error) {
if request.TMax == 0 {
request.TMax = 200
}
for i := 0; i < len(request.Imp); i++ {
var imp = request.Imp[i]
var bidderExt adapters.ExtImpBidder

if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return "", &errortypes.BadInput{
Message: err.Error(),
}
}

var mgidExt openrtb_ext.ExtImpMgid

if err := json.Unmarshal(bidderExt.Bidder, &mgidExt); err != nil {
return "", &errortypes.BadInput{
Message: err.Error(),
}
}

if path == "" {
path = mgidExt.AccountId
}
request.Imp[i].TagID = mgidExt.PlacementId

cur := ""
if mgidExt.Currency != "" && mgidExt.Currency != "USD" {
cur = mgidExt.Currency
}
if cur == "" && mgidExt.Cur != "" && mgidExt.Cur != "USD" {
cur = mgidExt.Cur
}
bidfloor := mgidExt.BidFloor
if bidfloor <= 0 {
bidfloor = mgidExt.BidFloor2
}
if bidfloor > 0 {
request.Imp[i].BidFloor = bidfloor
}
if cur != "" {
request.Imp[i].BidFloorCur = cur
}
}
if path == "" {
return "", &errortypes.BadInput{
Message: "accountId is not set",
}
}

return
}

func (a *MgidAdapter) MakeBids(bidReq *openrtb.BidRequest, unused *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("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}

if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}

var bidResp openrtb.BidResponse

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

bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)

bidResponse.Currency = bidResp.Cur

for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidType := openrtb_ext.BidTypeBanner
if len(sb.Bid[i].Ext) > 0 && bytes.Contains(sb.Bid[i].Ext, []byte("crtype")) {
ext := RespBidExt{}
if err := json.Unmarshal(sb.Bid[i].Ext, &ext); err == nil && len(ext.CreativeType) > 0 {
bidType = ext.CreativeType
}
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
})
}
}
return bidResponse, nil
}

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

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

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "mgidtest", NewMgidBidder("https://prebid.mgid.com/prebid/"))
}
135 changes: 135 additions & 0 deletions adapters/mgid/mgidtest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"accountId": "123",
"placementId": "456"
}
}
}
],
"device": {
"ua": "test-user-agent",
"ip": "123.123.123.123",
"language": "en",
"dnt": 0
},
"site": {
"domain": "www.publisher.com",
"page": "http://www.publisher.com/awesome/site"
},
"user": {
"buyeruid": "test_reader_id"
}
},

"httpCalls": [
{
"expectedRequest": {
"headers": {
"Accept": ["application/json"],
"Content-Type": ["application/json;charset=utf-8"]
},
"uri": "https://prebid.mgid.com/prebid/123",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"tagid": "456",
"ext": {
"bidder": {
"accountId": "123",
"placementId": "456"
}
}
}
],
"site": {
"domain": "www.publisher.com",
"page": "http://www.publisher.com/awesome/site"
},
"tmax": 200,
"user": {
"buyeruid": "test_reader_id"
},
"device": {
"ua": "test-user-agent",
"ip": "123.123.123.123",
"dnt": 0,
"language": "en"
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "test-bid-id",
"impid": "test-imp-id",
"price": 3.5,
"nurl": "nurl",
"adm": "some-test-ad",
"w": 300,
"h": 250
}
]
}
]
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-bid-id",
"impid": "test-imp-id",
"price": 3.5,
"adm": "some-test-ad",
"nurl": "nurl",
"w": 300,
"h": 250
},
"type": "banner"
}
]
}
]
}
Loading

0 comments on commit 6e3388c

Please sign in to comment.