Skip to content

New: InMobi Prebid Server Adapter #1489

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

Merged
merged 3 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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