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

EPlanning: Add schain support #4146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
74 changes: 71 additions & 3 deletions adapters/eplanning/eplanning.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package eplanning

import (
"encoding/json"
"math/rand"
"net/http"
"net/url"
"strings"

"regexp"
"strings"

"fmt"

Expand Down Expand Up @@ -195,7 +195,13 @@ func (adapter *EPlanningAdapter) MakeRequests(request *openrtb2.BidRequest, reqI
query.Set("vctx", strconv.Itoa(impType))
query.Set("vv", vastVersionDefault)
}

if request.Source != nil && request.Source.Ext != nil {
if openRtbSchain := unmarshalSupplyChain(request); openRtbSchain != nil && len(openRtbSchain.Nodes) <= 2 {
if schainValue := makeSupplyChain(*openRtbSchain); schainValue != "" {
query.Set("sch", schainValue)
}
}
}
Comment on lines +198 to +204
Copy link
Collaborator

@przemkaczmarek przemkaczmarek Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if request.Source == nil || request.Source.Ext == nil {
	return
}

openRtbSchain := unmarshalSupplyChain(request)
if openRtbSchain == nil || len(openRtbSchain.Nodes) > 2 {
	return
}

if schainValue := makeSupplyChain(*openRtbSchain); schainValue != "" {
	query.Set("sch", schainValue)
}

This approach is clearer and eliminates unnecessary levels of nesting.

uriObj.RawQuery = query.Encode()
uri := uriObj.String()

Expand All @@ -212,6 +218,68 @@ func (adapter *EPlanningAdapter) MakeRequests(request *openrtb2.BidRequest, reqI
return requests, errors
}

func unmarshalSupplyChain(req *openrtb2.BidRequest) *openrtb2.SupplyChain {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may make things clearer to just pass unmarshalSupplyChain the request.Source.Ext object rather than the whole request object. Makes it clear what unmarshalSupplyChain is actually doing and using.

var extSChain openrtb_ext.ExtRequestPrebidSChain
err := jsonutil.Unmarshal(req.Source.Ext, &extSChain)
if err != nil {
return nil
}
return &extSChain.SChain
}

func makeSupplyChain(openRtbSchain openrtb2.SupplyChain) string {
if len(openRtbSchain.Nodes) == 0 {
return ""
}

const schainPrefixFmt = "%s,%d"
const schainNodeFmt = "!%s,%s,%s,%s,%s,%s,%s"
schainPrefix := fmt.Sprintf(schainPrefixFmt, openRtbSchain.Ver, openRtbSchain.Complete)
var sb strings.Builder
sb.WriteString(schainPrefix)
for _, node := range openRtbSchain.Nodes {
// has to be in order: asi,sid,hp,rid,name,domain,ext
schainNode := fmt.Sprintf(
schainNodeFmt,
makeNodeValue(node.ASI),
makeNodeValue(node.SID),
makeNodeValue(node.HP),
makeNodeValue(node.RID),
makeNodeValue(node.Name),
makeNodeValue(node.Domain),
makeNodeValue(node.Ext),
)
sb.WriteString(schainNode)
}
return sb.String()
}

func makeNodeValue(nodeParam any) string {
switch nodeParam := nodeParam.(type) {
case string:
return url.QueryEscape(nodeParam)
case *int8:
pointer := nodeParam
if pointer == nil {
return ""
}
return makeNodeValue(int(*pointer))
case int:
return strconv.Itoa(nodeParam)
case json.RawMessage:
if nodeParam != nil {
freeFormJson, err := json.Marshal(nodeParam)
if err != nil {
return ""
}
return makeNodeValue(string(freeFormJson))
}
return ""
default:
return ""
}
}

func isMobileDevice(request *openrtb2.BidRequest) bool {
return request.Device != nil && (request.Device.DeviceType == adcom1.DeviceMobile || request.Device.DeviceType == adcom1.DevicePhone || request.Device.DeviceType == adcom1.DeviceTablet)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"mockBidRequest": {
"id": "test-request-id",
"source": {
"ext": {
"schain": {
"ver": "1.0",
"complete": 1,
"nodes": [
{
"asi": "exchange1.com",
"sid": "1234",
"hp": 1,
"ext": "text"
},
{
"asi": "exchange2.com",
"sid": "abcd",
"hp": 1,
"ext": 1
},
{
"asi": "exchange3.com",
"sid": "abcdf",
"hp": 1,
"ext": 1
}
]
}
}
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"ci": "12345"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://rtb.e-planning.net/pbs/1/12345/1/FILE/ROS?e=300x250%3A300x250&ncb=1&ur=FILE",
"body": {},
"impIDs":["test-imp-id"]
},
"mockResponse": {
"status": 200,
"body": {
"sI": { "k": "12345" },
"sec": "ROS",
"sp": [
{
"k": "300x250",
"a": [{
"i": "123456789abcdef",
"pr": "0.5",
"adm": "<div>test</div>",
"crid": "abcdef123456789",
"id": "adid12345",
"w": 300,
"h": 250
}]
}
]
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "123456789abcdef",
"impid": "test-imp-id",
"price": 0.5,
"adm": "<div>test</div>",
"adid": "adid12345",
"crid": "abcdef123456789",
"w": 300,
"h": 250
},
"type": "banner"
}
]
}
]
}
94 changes: 94 additions & 0 deletions adapters/eplanning/eplanningtest/supplemental/bid-with-schain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"mockBidRequest": {
"id": "test-request-id",
"source": {
"ext": {
"schain": {
"ver": "1.0",
"complete": 1,
"nodes": [
{
"asi": "exchange1.com",
"sid": "1234",
"hp": 1,
"ext": "text"
}
]
}
}
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"ci": "12345"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://rtb.e-planning.net/pbs/1/12345/1/FILE/ROS?e=300x250%3A300x250&ncb=1&sch=1.0%2C1%21exchange1.com%2C1234%2C1%2C%2C%2C%2C%2522text%2522&ur=FILE",
"body": {},
"impIDs":["test-imp-id"]
},
"mockResponse": {
"status": 200,
"body": {
"sI": { "k": "12345" },
"sec": "ROS",
"sp": [
{
"k": "300x250",
"a": [{
"i": "123456789abcdef",
"pr": "0.5",
"adm": "<div>test</div>",
"crid": "abcdef123456789",
"id": "adid12345",
"w": 300,
"h": 250
}]
}
]
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "123456789abcdef",
"impid": "test-imp-id",
"price": 0.5,
"adm": "<div>test</div>",
"adid": "adid12345",
"crid": "abcdef123456789",
"w": 300,
"h": 250
},
"type": "banner"
}
]
}
]
}
Loading