Skip to content

Commit

Permalink
New: InMobi Prebid Server Adapter (#1489)
Browse files Browse the repository at this point in the history
* Adding InMobi adapter

* code review feedback, also explicitly working with Imp[0], as we don't support multiple impressions

* less tolerant bidder params due to sneaky 1.13 -> 1.14+ change
  • Loading branch information
cormorinth authored Sep 15, 2020
1 parent d3ba8a9 commit 7b59a4b
Show file tree
Hide file tree
Showing 19 changed files with 701 additions and 0 deletions.
127 changes: 127 additions & 0 deletions adapters/inmobi/inmobi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package inmobi

import (
"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 InMobiAdapter struct {
endPoint string
}

func NewInMobiAdapter(endpoint string) *InMobiAdapter {
return &InMobiAdapter{
endPoint: endpoint,
}
}

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

if len(request.Imp) == 0 {
return nil, []error{&errortypes.BadInput{
Message: "No impression in the request",
}}
}

if err := preprocess(&request.Imp[0]); err != nil {
errs = append(errs, err)
return nil, errs
}

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,
Body: reqJson,
Headers: headers,
}}, errs
}

func (a *InMobiAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if response.StatusCode == http.StatusNoContent {
return nil, nil
}

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

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

bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)

for _, sb := range serverBidResponse.SeatBid {
for i := range sb.Bid {
mediaType := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: mediaType,
})
}
}

return bidResponse, nil
}

func preprocess(imp *openrtb.Imp) error {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return &errortypes.BadInput{
Message: err.Error(),
}
}

var inMobiExt openrtb_ext.ExtImpInMobi
if err := json.Unmarshal(bidderExt.Bidder, &inMobiExt); err != nil {
return &errortypes.BadInput{Message: "bad InMobi bidder ext"}
}

if len(inMobiExt.Plc) == 0 {
return &errortypes.BadInput{Message: "'plc' is a required attribute for InMobi's bidder ext"}
}

if imp.Banner != nil {
banner := *imp.Banner
imp.Banner = &banner
if (banner.W == nil || banner.H == nil || *banner.W == 0 || *banner.H == 0) && len(banner.Format) > 0 {
format := banner.Format[0]
banner.W = &format.W
banner.H = &format.H
}
}

return nil
}

func getMediaTypeForImp(impId string, imps []openrtb.Imp) openrtb_ext.BidType {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impId {
if imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
}
break
}
}
return mediaType
}
10 changes: 10 additions & 0 deletions adapters/inmobi/inmobi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package inmobi

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

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "inmobitest", NewInMobiAdapter("https://api.w.inmobi.com/showad/openrtb/bidder/prebid"))
}
107 changes: 107 additions & 0 deletions adapters/inmobi/inmobitest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"mockBidRequest": {
"app": {
"bundle": "com.example.app"
},
"id": "req-id",
"device": {
"ifa": "9d8fe0a9-c0dd-4482-b16b-5709b00c608d",
"ip": "1.1.1.1",
"ua": "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36"
},
"imp": [
{
"ext": {
"bidder": {
"plc": "1596825400965"
}
},
"banner": {
"w": 320,
"h": 50
},
"id": "imp-id"
}
]
},
"httpCalls": [{
"expectedRequest": {
"uri": "https://api.w.inmobi.com/showad/openrtb/bidder/prebid",
"body": {
"app": {
"bundle": "com.example.app"
},
"id": "req-id",
"device": {
"ifa": "9d8fe0a9-c0dd-4482-b16b-5709b00c608d",
"ip": "1.1.1.1",
"ua": "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36"
},
"imp": [
{
"ext": {
"bidder": {
"plc": "1596825400965"
}
},
"banner": {
"w": 320,
"h": 50
},
"id": "imp-id"
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "req-id",
"seatbid": [
{
"bid": [
{
"ext": {
"prebid": {
"meta": {
"networkName": "inmobi"
}
}
},
"nurl": "https://some.event.url/params",
"crid": "123456789",
"adomain": [],
"price": 2.0,
"id": "1234",
"adm": "bannerhtml",
"impid": "imp-id"
}
]
}
]
}
}
}],

"expectedBidResponses": [{
"currency": "USD",
"bids": [{
"bid": {
"id": "1234",
"impid": "imp-id",
"price": 2.0,
"adm": "bannerhtml",
"crid": "123456789",
"nurl": "https://some.event.url/params",
"ext": {
"prebid": {
"meta": {
"networkName": "inmobi"
}
}
}
},
"type": "banner"
}]
}]
}
109 changes: 109 additions & 0 deletions adapters/inmobi/inmobitest/exemplary/simple-video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"mockBidRequest": {
"app": {
"bundle": "com.example.app"
},
"id": "req-id",
"device": {
"ifa": "9d8fe0a9-c0dd-4482-b16b-5709b00c608d",
"ip": "1.1.1.1",
"ua": "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36"
},
"imp": [
{
"ext": {
"bidder": {
"plc": "1598991608990"
}
},
"video": {
"w": 640,
"h": 360,
"mimes": ["video/mp4"]
},
"id": "imp-id"
}
]
},
"httpCalls": [{
"expectedRequest": {
"uri": "https://api.w.inmobi.com/showad/openrtb/bidder/prebid",
"body": {
"app": {
"bundle": "com.example.app"
},
"id": "req-id",
"device": {
"ifa": "9d8fe0a9-c0dd-4482-b16b-5709b00c608d",
"ip": "1.1.1.1",
"ua": "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36"
},
"imp": [
{
"ext": {
"bidder": {
"plc": "1598991608990"
}
},
"video": {
"w": 640,
"h": 360,
"mimes": ["video/mp4"]
},
"id": "imp-id"
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "req-id",
"seatbid": [
{
"bid": [
{
"ext": {
"prebid": {
"meta": {
"networkName": "inmobi"
}
}
},
"nurl": "https://some.event.url/params",
"crid": "123456789",
"adomain": [],
"price": 2.0,
"id": "1234",
"adm": "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <VAST version=\"3.0\"></VAST>",
"impid": "imp-id"
}
]
}
]
}
}
}],

"expectedBidResponses": [{
"currency": "USD",
"bids": [{
"bid": {
"id": "1234",
"impid": "imp-id",
"price": 2.0,
"adm": "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <VAST version=\"3.0\"></VAST>",
"crid": "123456789",
"nurl": "https://some.event.url/params",
"ext": {
"prebid": {
"meta": {
"networkName": "inmobi"
}
}
}
},
"type": "video"
}]
}]
}
3 changes: 3 additions & 0 deletions adapters/inmobi/inmobitest/params/race/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plc": "1596825400965"
}
3 changes: 3 additions & 0 deletions adapters/inmobi/inmobitest/params/race/video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plc": "1598991608990"
}
Loading

0 comments on commit 7b59a4b

Please sign in to comment.