Skip to content

Commit

Permalink
Mobfox: Add rout to adexcange (prebid#1702)
Browse files Browse the repository at this point in the history
Co-authored-by: mobfox <artem.iholkin@smartyads.com>
  • Loading branch information
2 people authored and Dan Barnett committed May 11, 2021
1 parent 5c13863 commit d96f2b2
Show file tree
Hide file tree
Showing 28 changed files with 1,343 additions and 813 deletions.
53 changes: 39 additions & 14 deletions adapters/mobfoxpb/mobfoxpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/buger/jsonparser"
"github.com/mxmCherry/openrtb"
Expand All @@ -13,6 +14,16 @@ import (
"github.com/prebid/prebid-server/openrtb_ext"
)

const (
ROUTE_NATIVE = "o"
ROUTE_RTB = "rtb"
METHOD_NATIVE = "ortb"
METHOD_RTB = "req"
MACROS_ROUTE = "__route__"
MACROS_METHOD = "__method__"
MACROS_KEY = "__key__"
)

type adapter struct {
URI string
}
Expand All @@ -28,21 +39,36 @@ func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters
// MakeRequests create bid request for mobfoxpb demand
func (a *adapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errs []error
var err error
var tagID string

var route string
var method string
var adapterRequests []*adapters.RequestData

requestURI := a.URI
reqCopy := *request
imp := request.Imp[0]
tagID, err = jsonparser.GetString(imp.Ext, "bidder", "TagID")
if err != nil {
errs = append(errs, err)
tagID, errTag := jsonparser.GetString(imp.Ext, "bidder", "TagID")
key, errKey := jsonparser.GetString(imp.Ext, "bidder", "key")
if errTag != nil && errKey != nil {
errs = append(errs, &errortypes.BadInput{
Message: fmt.Sprintf("Invalid or non existing key and tagId, atleast one should be present"),
})
return nil, errs
}
imp.TagID = tagID

if key != "" {
route = ROUTE_RTB
method = METHOD_RTB
requestURI = strings.Replace(requestURI, MACROS_KEY, key, 1)
} else if tagID != "" {
method = METHOD_NATIVE
route = ROUTE_NATIVE
}

requestURI = strings.Replace(requestURI, MACROS_ROUTE, route, 1)
requestURI = strings.Replace(requestURI, MACROS_METHOD, method, 1)

reqCopy.Imp = []openrtb.Imp{imp}
adapterReq, err := a.makeRequest(&reqCopy)
adapterReq, err := a.makeRequest(&reqCopy, requestURI)
if err != nil {
errs = append(errs, err)
}
Expand All @@ -52,7 +78,7 @@ func (a *adapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.Ex
return adapterRequests, errs
}

func (a *adapter) makeRequest(request *openrtb.BidRequest) (*adapters.RequestData, error) {
func (a *adapter) makeRequest(request *openrtb.BidRequest, requestURI string) (*adapters.RequestData, error) {
reqJSON, err := json.Marshal(request)

if err != nil {
Expand All @@ -64,7 +90,7 @@ func (a *adapter) makeRequest(request *openrtb.BidRequest) (*adapters.RequestDat
headers.Add("Accept", "application/json")
return &adapters.RequestData{
Method: "POST",
Uri: a.URI,
Uri: requestURI,
Body: reqJSON,
Headers: headers,
}, nil
Expand Down Expand Up @@ -98,11 +124,10 @@ func (a *adapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest
if err != nil {
errs = append(errs, err)
} else {
b := &adapters.TypedBid{
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
})
}
}
}
Expand All @@ -126,6 +151,6 @@ func getMediaTypeForImp(impID string, imps []openrtb.Imp) (openrtb_ext.BidType,

// This shouldnt happen. Lets handle it just incase by returning an error.
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Failed to find impression \"%s\" ", impID),
Message: fmt.Sprintf("Failed to find impression \"%s\"", impID),
}
}
2 changes: 1 addition & 1 deletion adapters/mobfoxpb/mobfoxpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderMobfoxpb, config.Adapter{
Endpoint: "http://example.com/?c=o&m=ortb"})
Endpoint: "http://example.com/?c=__route__&m=__method__&key=__key__"})
if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"key": "6"
}
}
}
],
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"device": {
"ip": "123.123.123.123",
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx"
}
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://example.com/?c=rtb&m=req&key=6",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"key": "6"
}
}
}
],
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"device": {
"ip": "123.123.123.123",
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx"
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"https://bes.mobfox.com/?c=o&m=adm&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"https://bes.mobfox.com/?c=o&m=adm&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
},
"type": "banner"
}
]
}
]
}
128 changes: 128 additions & 0 deletions adapters/mobfoxpb/mobfoxpbtest/exemplary/simple-banner-rtb-route.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"TagID": "6"
}
}
}
],
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"device": {
"ip": "123.123.123.123",
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx"
}
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://example.com/?c=o&m=ortb&key=__key__",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"TagID": "6"
}
}
}
],
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"device": {
"ip": "123.123.123.123",
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx"
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"https://bes.mobfox.com/?c=o&m=adm&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"https://bes.mobfox.com/?c=o&m=adm&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
},
"type": "banner"
}
]
}
]
}
Loading

0 comments on commit d96f2b2

Please sign in to comment.