From 9a29770a24672e4f2e624152d77952689abaff14 Mon Sep 17 00:00:00 2001 From: saurabh-narkhede Date: Wed, 28 Feb 2024 10:26:37 +0000 Subject: [PATCH 1/2] dchain support --- dchain/dchain.go | 59 ++++++++++++++++++++++++++++++++++++++++++++ exchange/exchange.go | 5 ++++ 2 files changed, 64 insertions(+) create mode 100644 dchain/dchain.go diff --git a/dchain/dchain.go b/dchain/dchain.go new file mode 100644 index 00000000000..03b608e203e --- /dev/null +++ b/dchain/dchain.go @@ -0,0 +1,59 @@ +package dchain + +import ( + "encoding/json" + "strconv" + + "github.com/prebid/prebid-server/v2/openrtb_ext" +) + +type Dchain struct { + Ver string `json:"ver,omitempty"` + Complete int `json:"complete,omitempty"` + Nodes []DchainNodes `json:"nodes,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` +} + +type DchainNodes struct { + Asi string `json:"asi,omitempty"` + Bsid string `json:"bsid,omitempty"` + Rid string `json:"rid,omitempty"` + Name string `json:"name,omitempty"` + Domain string `json:"domain,omitempty"` + Ext json.RawMessage `json:"ext,omitempty"` +} + +func IsValidDchain(dchain Dchain) bool { + if dchain.Complete != 0 && dchain.Complete != 1 { + return false + } + if dchain.Nodes == nil || len(dchain.Nodes) == 0 { + return false + } + return true +} + +func AddDchainNode(prebidMeta *openrtb_ext.ExtBidPrebidMeta) { + var dchain Dchain + if err := json.Unmarshal(prebidMeta.DChain, &dchain); err == nil && IsValidDchain(dchain) { + dchain.Nodes = append(dchain.Nodes, + DchainNodes{Asi: prebidMeta.AdapterCode}, + ) + prebidMeta.DChain, _ = json.Marshal(dchain) + return + } + basicDchain := Dchain{ + Ver: "1.0", + Complete: 0, + Nodes: []DchainNodes{}, + } + + if prebidMeta.NetworkID != 0 && prebidMeta.NetworkName != "" { + basicDchain.Nodes = append(basicDchain.Nodes, + DchainNodes{Name: prebidMeta.NetworkName, Bsid: strconv.Itoa(prebidMeta.NetworkID)}, + ) + } + + basicDchain.Nodes = append(basicDchain.Nodes, DchainNodes{Name: prebidMeta.AdapterCode}) + prebidMeta.DChain, _ = json.Marshal(basicDchain) +} diff --git a/exchange/exchange.go b/exchange/exchange.go index 003ce559dc2..724d91755a1 100644 --- a/exchange/exchange.go +++ b/exchange/exchange.go @@ -14,6 +14,7 @@ import ( "strings" "time" + "github.com/prebid/prebid-server/v2/dchain" "github.com/prebid/prebid-server/v2/privacy" "github.com/prebid/prebid-server/v2/adapters" @@ -1334,6 +1335,10 @@ func makeBidExtJSON(ext json.RawMessage, prebid *openrtb_ext.ExtBidPrebid, impEx prebid.Meta = &openrtb_ext.ExtBidPrebidMeta{} } + if prebid.Meta.DChain != nil { + dchain.AddDchainNode(prebid.Meta) + } + prebid.Meta.AdapterCode = adapter.String() // ext.prebid.storedrequestattributes and ext.prebid.passthrough From 439bdf19fa1489358cc51aca5bfc384a570c1cc6 Mon Sep 17 00:00:00 2001 From: saurabh-narkhede Date: Tue, 5 Mar 2024 06:22:00 +0000 Subject: [PATCH 2/2] initial commit dchain support --- adapters/pubmatic/pubmatic.go | 6 ++++++ exchange/exchange.go | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/adapters/pubmatic/pubmatic.go b/adapters/pubmatic/pubmatic.go index 9876041ec4e..6d5241aad18 100644 --- a/adapters/pubmatic/pubmatic.go +++ b/adapters/pubmatic/pubmatic.go @@ -450,6 +450,12 @@ func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externa BidVideo: &openrtb_ext.ExtBidPrebidVideo{}, } + if dchain, _, _, err := jsonparser.Get(bid.Ext, "dchain"); err == nil && len(dchain) > 0 { + typedBid.BidMeta = &openrtb_ext.ExtBidPrebidMeta{ + DChain: dchain, + } + } + var bidExt *pubmaticBidExt err := json.Unmarshal(bid.Ext, &bidExt) if err != nil { diff --git a/exchange/exchange.go b/exchange/exchange.go index 7aa8526c955..402a47546c7 100644 --- a/exchange/exchange.go +++ b/exchange/exchange.go @@ -1346,12 +1346,12 @@ func makeBidExtJSON(ext json.RawMessage, prebid *openrtb_ext.ExtBidPrebid, impEx prebid.Meta = &openrtb_ext.ExtBidPrebidMeta{} } + prebid.Meta.AdapterCode = adapter.String() + if prebid.Meta.DChain != nil { dchain.AddDchainNode(prebid.Meta) } - prebid.Meta.AdapterCode = adapter.String() - // ext.prebid.storedrequestattributes and ext.prebid.passthrough if impExtInfo, ok := impExtInfoMap[impId]; ok { prebid.Passthrough = impExtInfoMap[impId].Passthrough