Skip to content
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

Deepintent adapter #1524

Merged
merged 20 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
142 changes: 142 additions & 0 deletions adapters/deepintent/deepintent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package deepintent

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

"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)

// DeepintentAdapter struct
type DeepintentAdapter struct {
URI string
}

// NewDeepintentBidder Initializes the Bidder
func NewDeepintentBidder(endpoint string) *DeepintentAdapter {
return &DeepintentAdapter{
URI: endpoint,
}
}

func (d *DeepintentAdapter) makeRequest(request *openrtb.BidRequest) (*adapters.RequestData, []error) {
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
sourabhg marked this conversation as resolved.
Show resolved Hide resolved

var errs []error

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
}

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

// 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.StatusNotFound {
return nil, []error{&errortypes.BadServerResponse{
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}
sourabhg marked this conversation as resolved.
Show resolved Hide resolved

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)
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
} else {
b := &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
}
return bidResponse, errs
}

func getMediaTypeForImp(impID string, imps []openrtb.Imp) (openrtb_ext.BidType, error) {
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner == nil && imp.Video != nil {
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
mediaType = openrtb_ext.BidTypeVideo
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
}
return mediaType, nil
}
}
sourabhg marked this conversation as resolved.
Show resolved Hide resolved

// This shouldnt happen. Lets handle it just incase by returning an error.
return "", &errortypes.BadInput{
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
Message: fmt.Sprintf("Failed to find impression \"%s\" ", impID),
}
}

//MakeRequests which creates request object for Deepintent DSP
func (d *DeepintentAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
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, err)
continue
}

if err = json.Unmarshal(bidderExt.Bidder, &deepintentExt); err != nil {
errs = append(errs, err)
continue
}

reqCopy.Imp[0].TagID = deepintentExt.TagId
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
reqCopy.Imp[0].DisplayManager = "di_prebid"
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
reqCopy.Imp[0].DisplayManagerVer = "2.0.0"

adapterReq, errors := d.makeRequest(&reqCopy)
if adapterReq != nil {
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
adapterRequests = append(adapterRequests, adapterReq)
}
errs = append(errs, errors...)
}
return adapterRequests, errs
}
12 changes: 12 additions & 0 deletions adapters/deepintent/deepintent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package deepintent

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
deepintentAdapter := NewDeepintentBidder("https://prebid.deepintent.com/prebid")
adapterstest.RunJSONBidderTest(t, "deepintenttest", deepintentAdapter)
}
138 changes: 138 additions & 0 deletions adapters/deepintent/deepintenttest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{
"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"
sourabhg marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
],
"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
}
]
},
"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