Skip to content

Commit

Permalink
33Across: Add support for multi-imp requests (prebid#1609)
Browse files Browse the repository at this point in the history
  • Loading branch information
curlyblueeagle authored and shunj-nb committed Nov 8, 2022
1 parent 607973e commit b4e0e98
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 204 deletions.
122 changes: 81 additions & 41 deletions adapters/33across/33across.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@ type TtxAdapter struct {
}

type Ext struct {
Ttx ext `json:"ttx"`
Ttx impTtxExt `json:"ttx"`
}

type ext struct {
type impTtxExt struct {
Prod string `json:"prod"`
Zoneid string `json:"zoneid,omitempty"`
}

type reqExt struct {
Ttx *reqTtxExt `json:"ttx,omitempty"`
}

type reqTtxExt struct {
Caller []TtxCaller `json:"caller,omitempty"`
}

type TtxCaller struct {
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
}

// CALLER Info used to track Prebid Server
// as one of the hops in the request to exchange
var CALLER = TtxCaller{"Prebid-Server", "n/a"}

type bidExt struct {
Ttx bidTtxExt `json:"ttx,omitempty"`
}
Expand All @@ -38,39 +55,41 @@ func (a *TtxAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters
var errs []error
var adapterRequests []*adapters.RequestData

adapterReq, errors := a.makeRequest(request)
if adapterReq != nil {
adapterRequests = append(adapterRequests, adapterReq)
// Construct request extension common to all imps
// NOTE: not blocking adapter requests on errors
// since request extension is optional.
reqExt, err := makeReqExt(request)
if err != nil {
errs = append(errs, err)
}
request.Ext = reqExt

// Break up multi-imp request into multiple external requests since we don't
// support SRA in our exchange server
for i := 0; i < len(request.Imp); i++ {
if adapterReq, err := a.makeRequest(*request, request.Imp[i]); err == nil {
adapterRequests = append(adapterRequests, adapterReq)
} else {
errs = append(errs, err)
}
}
errs = append(errs, errors...)

return adapterRequests, errors
return adapterRequests, errs
}

// Update the request object to include custom value
// site.id
func (a *TtxAdapter) makeRequest(request *openrtb.BidRequest) (*adapters.RequestData, []error) {
var errs []error
func (a *TtxAdapter) makeRequest(request openrtb.BidRequest, imp openrtb.Imp) (*adapters.RequestData, error) {
impCopy, err := makeImps(imp)

// Make a copy as we don't want to change the original request
reqCopy := *request
if err := preprocess(&reqCopy); err != nil {
errs = append(errs, err)
if err != nil {
return nil, err
}

if reqCopy.Imp[0].Banner == nil && reqCopy.Imp[0].Video == nil {
errs = append(errs, &errortypes.BadInput{
Message: "At least one of [banner, video] formats must be defined in Imp. None found",
})

return nil, errs
}
request.Imp = []openrtb.Imp{*impCopy}

// Last Step
reqJSON, err := json.Marshal(reqCopy)
reqJSON, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
return nil, errs
return nil, err
}

headers := http.Header{}
Expand All @@ -81,63 +100,84 @@ func (a *TtxAdapter) makeRequest(request *openrtb.BidRequest) (*adapters.Request
Uri: a.endpoint,
Body: reqJSON,
Headers: headers,
}, errs
}, nil
}

// Mutate the request to get it ready to send to ttx.
func preprocess(request *openrtb.BidRequest) error {
var imp = &request.Imp[0]
func makeImps(imp openrtb.Imp) (*openrtb.Imp, error) {
if imp.Banner == nil && imp.Video == nil {
return nil, &errortypes.BadInput{
Message: fmt.Sprintf("Imp ID %s must have at least one of [Banner, Video] defined", imp.ID),
}
}

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

var ttxExt openrtb_ext.ExtImp33across
if err := json.Unmarshal(bidderExt.Bidder, &ttxExt); err != nil {
return &errortypes.BadInput{
return nil, &errortypes.BadInput{
Message: err.Error(),
}
}

var impExt Ext
impExt.Ttx.Prod = ttxExt.ProductId

// Add zoneid if it's defined
impExt.Ttx.Zoneid = ttxExt.SiteId

if len(ttxExt.ZoneId) > 0 {
impExt.Ttx.Zoneid = ttxExt.ZoneId
}

impExtJSON, err := json.Marshal(impExt)
if err != nil {
return &errortypes.BadInput{
return nil, &errortypes.BadInput{
Message: err.Error(),
}
}

imp.Ext = impExtJSON

if request.Site != nil {
siteCopy := *request.Site
siteCopy.ID = ttxExt.SiteId
request.Site = &siteCopy
}

// Validate Video if it exists
if imp.Video != nil {
videoCopy, err := validateVideoParams(imp.Video, impExt.Ttx.Prod)

imp.Video = videoCopy

if err != nil {
return &errortypes.BadInput{
return nil, &errortypes.BadInput{
Message: err.Error(),
}
}
}

return nil
return &imp, nil
}

func makeReqExt(request *openrtb.BidRequest) ([]byte, error) {
var reqExt reqExt

if len(request.Ext) > 0 {
if err := json.Unmarshal(request.Ext, &reqExt); err != nil {
return nil, err
}
}

if reqExt.Ttx == nil {
reqExt.Ttx = &reqTtxExt{}
}

if reqExt.Ttx.Caller == nil {
reqExt.Ttx.Caller = make([]TtxCaller, 0)
}

reqExt.Ttx.Caller = append(reqExt.Ttx.Caller, CALLER)

return json.Marshal(reqExt)
}

// MakeBids make the bids for the bid response.
Expand Down
112 changes: 0 additions & 112 deletions adapters/33across/33acrosstest/exemplary/app-video.json

This file was deleted.

17 changes: 13 additions & 4 deletions adapters/33across/33acrosstest/exemplary/bidresponse-defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
"expectedRequest": {
"uri": "http://ssc.33across.com",
"body": {
"ext": {
"ttx": {
"caller": [
{
"name": "Prebid-Server",
"version": "n/a"
}
]
}
},
"id": "test-request-id",
"imp": [
{
Expand All @@ -44,14 +54,13 @@
},
"ext": {
"ttx": {
"prod": "instream"
"prod": "instream",
"zoneid": "fake-site-id"
}
}
}
],
"site": {
"id": "fake-site-id"
}
"site": {}
}
},
"mockResponse": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
"expectedRequest": {
"uri": "http://ssc.33across.com",
"body": {
"ext": {
"ttx": {
"caller": [
{
"name": "Prebid-Server",
"version": "n/a"
}
]
}
},
"id": "test-request-id",
"imp": [
{
Expand All @@ -42,14 +52,13 @@
},
"ext": {
"ttx": {
"prod": "instream"
"prod": "instream",
"zoneid": "fake-site-id"
}
}
}
],
"site": {
"id": "fake-site-id"
}
"site": {}
}
},
"mockResponse": {
Expand Down
Loading

0 comments on commit b4e0e98

Please sign in to comment.