Skip to content

Commit

Permalink
Deepintent adapter (#1524)
Browse files Browse the repository at this point in the history
Co-authored-by: Sourabh Gandhe <sourabh@Sourabhs-MacBook-Pro.local>
  • Loading branch information
sourabhg and Sourabh Gandhe authored Dec 3, 2020
1 parent 840c15d commit 0f76d20
Show file tree
Hide file tree
Showing 27 changed files with 1,203 additions and 0 deletions.
191 changes: 191 additions & 0 deletions adapters/deepintent/deepintent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package deepintent

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

"github.com/mxmCherry/openrtb"
"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"
)

const displayManager string = "di_prebid"
const displayManagerVer string = "2.0.0"

// DeepintentAdapter struct
type DeepintentAdapter struct {
URI string
}

type deepintentParams struct {
TagID string `json:"tagId"`
}

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

//MakeRequests which creates request object for Deepintent DSP
func (d *DeepintentAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errs []error
var deepintentExt openrtb_ext.ExtImpDeepintent
var err error

var adapterRequests []*adapters.RequestData

reqCopy := *request
for _, imp := range request.Imp {
reqCopy.Imp = []openrtb.Imp{imp}

var bidderExt adapters.ExtImpBidder
if err = json.Unmarshal(reqCopy.Imp[0].Ext, &bidderExt); err != nil {
errs = append(errs, &errortypes.BadInput{
Message: fmt.Sprintf("Impression id=%s has an Error: %s", imp.ID, err.Error()),
})
continue
}

if err = json.Unmarshal(bidderExt.Bidder, &deepintentExt); err != nil {
errs = append(errs, &errortypes.BadInput{
Message: fmt.Sprintf("Impression id=%s, has invalid Ext", imp.ID),
})
continue
}

reqCopy.Imp[0].TagID = deepintentExt.TagID
reqCopy.Imp[0].DisplayManager = displayManager
reqCopy.Imp[0].DisplayManagerVer = displayManagerVer

adapterReq, errors := d.preprocess(reqCopy)
if errors != nil {
errs = append(errs, errors...)
}
if adapterReq != nil {
adapterRequests = append(adapterRequests, adapterReq)
}

}
return adapterRequests, errs
}

// MakeBids makes the bids
func (d *DeepintentAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var errs []error

if response.StatusCode == http.StatusNoContent {
return nil, nil
}

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)

for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
if err != nil {
errs = append(errs, err)
} else {
b := &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
}
return bidResponse, errs
}

func (d *DeepintentAdapter) preprocess(request openrtb.BidRequest) (*adapters.RequestData, []error) {

var errs []error
impsCount := len(request.Imp)
resImps := make([]openrtb.Imp, 0, impsCount)

for _, imp := range request.Imp {

if err := buildImpBanner(&imp); err != nil {
errs = append(errs, err)
continue
}
resImps = append(resImps, imp)
}
request.Imp = resImps
if errs != nil {
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: d.URI,
Body: reqJSON,
Headers: headers,
}, errs
}

func buildImpBanner(imp *openrtb.Imp) error {

if imp.Banner == nil {
return &errortypes.BadInput{
Message: fmt.Sprintf("We need a Banner Object in the request"),
}
}

if imp.Banner.W == nil && imp.Banner.H == nil {
bannerCopy := *imp.Banner
banner := &bannerCopy

if len(banner.Format) == 0 {
return &errortypes.BadInput{
Message: fmt.Sprintf("At least one size is required"),
}
}
format := banner.Format[0]
banner.W = &format.W
banner.H = &format.H
imp.Banner = banner
}

return nil
}

func getMediaTypeForImp(impID string, imps []openrtb.Imp) (openrtb_ext.BidType, error) {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impID {
return mediaType, nil
}
}

// This shouldnt happen. Lets handle it just incase by returning an error.
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find impression %s ", impID),
}
}
21 changes: 21 additions & 0 deletions adapters/deepintent/deepintent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package deepintent

import (
"testing"

"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderDeepintent, config.Adapter{
Endpoint: "https://prebid.deepintent.com/prebid"})

if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}

adapterstest.RunJSONBidderTest(t, "deepintenttest", bidder)
}
140 changes: 140 additions & 0 deletions adapters/deepintent/deepintenttest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"tagid": "16",
"displaymanager": "di_prebid",
"displaymanagerver": "2.0.0",
"ext": {
"bidder": {
"TagID": "16"
}
}
}
],
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"device": {
"ip": "123.123.123.123",
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx"
}
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://prebid.deepintent.com/prebid",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
],
"h":250,
"w":300
},
"tagid": "16",
"displaymanager": "di_prebid",
"displaymanagerver": "2.0.0",
"ext": {
"bidder": {
"TagID": "16"
}
}
}
],
"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://pub.admanmedia.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"
}
}
}
],
"seat": "adman"
}
],
"cur": "USD"
}
}
}
],

"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://pub.admanmedia.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 0f76d20

Please sign in to comment.