From 81841e5dd2a651cc2fa9d9a5dc0cc2167a7574f8 Mon Sep 17 00:00:00 2001 From: Viral Vala Date: Tue, 1 Dec 2020 17:45:31 +0530 Subject: [PATCH] OTT-55 Second Commit --- adapters/tagbidder/bidder_macro.go | 38 ++++---- adapters/tagbidder/ibidder_macro.go | 6 +- adapters/tagbidder/itagbidder.go | 9 -- adapters/tagbidder/macro_processor.go | 43 ++++++++- adapters/tagbidder/mapper.go | 30 +++--- adapters/tagbidder/spotxtag/constant.go | 7 ++ adapters/tagbidder/spotxtag/spotx_adapter.go | 44 +++++++++ .../spotx_macro.go} | 92 ++++--------------- adapters/tagbidder/spotxtag/spotx_mapper.go | 18 ++++ adapters/tagbidder/tagbidder.go | 43 +++++++++ adapters/tagbidder/tagbidder_test.go | 31 ------- 11 files changed, 212 insertions(+), 149 deletions(-) delete mode 100644 adapters/tagbidder/itagbidder.go create mode 100644 adapters/tagbidder/spotxtag/constant.go create mode 100644 adapters/tagbidder/spotxtag/spotx_adapter.go rename adapters/tagbidder/{spotx_bidder_test.go => spotxtag/spotx_macro.go} (52%) create mode 100644 adapters/tagbidder/spotxtag/spotx_mapper.go create mode 100644 adapters/tagbidder/tagbidder.go delete mode 100644 adapters/tagbidder/tagbidder_test.go diff --git a/adapters/tagbidder/bidder_macro.go b/adapters/tagbidder/bidder_macro.go index 940b1ab73cd..8c8a3bfba10 100644 --- a/adapters/tagbidder/bidder_macro.go +++ b/adapters/tagbidder/bidder_macro.go @@ -5,36 +5,38 @@ import "github.com/PubMatic-OpenWrap/openrtb" //BidderMacro default implementation type BidderMacro struct { IBidderMacro - request *openrtb.BidRequest - isApp bool - imp *openrtb.Imp - publisher *openrtb.Publisher - content *openrtb.Content + Request *openrtb.BidRequest + IsApp bool + Imp *openrtb.Imp + Publisher *openrtb.Publisher + Content *openrtb.Content } //NewBidderMacro contains definition for all openrtb macro's -func NewBidderMacro(request *openrtb.BidRequest) *BidderMacro { - bidder := &BidderMacro{ - request: request, - } - bidder.init() - return bidder +func NewBidderMacro() *BidderMacro { + return &BidderMacro{} } func (tag *BidderMacro) init() { - if nil != tag.request.App { - tag.isApp = true - tag.publisher = tag.request.App.Publisher - tag.content = tag.request.App.Content + if nil != tag.Request.App { + tag.IsApp = true + tag.Publisher = tag.Request.App.Publisher + tag.Content = tag.Request.App.Content } else { - tag.publisher = tag.request.Site.Publisher - tag.content = tag.request.Site.Content + tag.Publisher = tag.Request.Site.Publisher + tag.Content = tag.Request.Site.Content } } +//InitBidRequest will initialise BidRequest +func (tag *BidderMacro) InitBidRequest(request *openrtb.BidRequest) { + tag.Request = request + tag.init() +} + //LoadImpression will set current imp func (tag *BidderMacro) LoadImpression(imp *openrtb.Imp) error { - tag.imp = imp + tag.Imp = imp return nil } diff --git a/adapters/tagbidder/ibidder_macro.go b/adapters/tagbidder/ibidder_macro.go index 1716c501924..58535dc5168 100644 --- a/adapters/tagbidder/ibidder_macro.go +++ b/adapters/tagbidder/ibidder_macro.go @@ -1,8 +1,12 @@ package tagbidder +import "github.com/PubMatic-OpenWrap/openrtb" + //IBidderMacro interface will capture all macro definition type IBidderMacro interface { - ITagBidder + //Helper Function + InitBidRequest(request *openrtb.BidRequest) + LoadImpression(imp *openrtb.Imp) error //Request MacroTest(string) string diff --git a/adapters/tagbidder/itagbidder.go b/adapters/tagbidder/itagbidder.go deleted file mode 100644 index bca198ad4c2..00000000000 --- a/adapters/tagbidder/itagbidder.go +++ /dev/null @@ -1,9 +0,0 @@ -package tagbidder - -import "github.com/PubMatic-OpenWrap/openrtb" - -type ITagBidder interface { - Name() string - URL() string - LoadImpression(imp *openrtb.Imp) error -} diff --git a/adapters/tagbidder/macro_processor.go b/adapters/tagbidder/macro_processor.go index e139130607d..795911a55a6 100644 --- a/adapters/tagbidder/macro_processor.go +++ b/adapters/tagbidder/macro_processor.go @@ -20,15 +20,20 @@ const ( macroEscapeSuffixLen int = len(macroEscapeSuffix) ) +//Flags to customize macro processing wrappers +type Flags struct { + RemoveEmptyParam bool +} + //MacroProcessor struct to hold openrtb request and cache values type MacroProcessor struct { bidder IBidderMacro - mapper mapper + mapper Mapper macroCache map[string]string } //NewMacroProcessor will process macro's of openrtb bid request -func NewMacroProcessor(mapper mapper) *MacroProcessor { +func NewMacroProcessor(mapper Mapper) *MacroProcessor { return &MacroProcessor{ mapper: mapper, macroCache: make(map[string]string), @@ -133,6 +138,40 @@ func (mp *MacroProcessor) Process(in string) (response string) { return } +//ProcessURL : Substitute macros in input string +func (mp *MacroProcessor) ProcessURL(uri string, flags Flags) (response string) { + if !flags.RemoveEmptyParam { + return mp.Process(uri) + } + + url, _ := url.Parse(uri) + + //Path + url.Path = mp.Process(url.Path) + + //Values + var out bytes.Buffer + values := url.Query() + for k, v := range values { + replaced := mp.Process(v[0]) + if len(replaced) > 0 { + if out.Len() > 0 { + out.WriteByte('&') + } + out.WriteString(k) + out.WriteByte('=') + out.WriteString(replaced) + } + } + + url.RawQuery = out.String() + response = url.String() + + glog.V(3).Infof("[MACRO]:in:[%s]\nreplaced:[%s]\n", uri, response) + + return +} + //Dump : will print all cached macro and its values func (mp *MacroProcessor) Dump() { if glog.V(3) { diff --git a/adapters/tagbidder/mapper.go b/adapters/tagbidder/mapper.go index d912fe06c69..eddd22c27b3 100644 --- a/adapters/tagbidder/mapper.go +++ b/adapters/tagbidder/mapper.go @@ -5,12 +5,10 @@ type macroCallBack struct { callback func(IBidderMacro, string) string } -type mapper map[string]*macroCallBack +type Mapper map[string]*macroCallBack -var bidderMapper map[string]mapper - -func (obj mapper) clone() mapper { - cloned := make(mapper, len(obj)) +func (obj Mapper) clone() Mapper { + cloned := make(Mapper, len(obj)) for k, v := range obj { newCallback := *v cloned[k] = &newCallback @@ -19,18 +17,18 @@ func (obj mapper) clone() mapper { } //SetCache value to specific key -func (obj *mapper) SetCache(key string, value bool) { +func (obj *Mapper) SetCache(key string, value bool) { if value, ok := (*obj)[key]; ok { value.cached = true } } //AddCustomMacro for adding custom macro whose definition will be present in IBidderMacro.Custom method -func (obj *mapper) AddCustomMacro(key string, isCached bool) { +func (obj *Mapper) AddCustomMacro(key string, isCached bool) { (*obj)[key] = ¯oCallBack{cached: isCached, callback: IBidderMacro.Custom} } -var _defaultMapper = mapper{ +var _defaultMapper = Mapper{ //Request MacroTest: ¯oCallBack{cached: false, callback: IBidderMacro.MacroTest}, MacroTimeout: ¯oCallBack{cached: false, callback: IBidderMacro.MacroTimeout}, @@ -178,17 +176,21 @@ var _defaultMapper = mapper{ MacroCacheBuster: ¯oCallBack{cached: false, callback: IBidderMacro.MacroCacheBuster}, } -//GetNewDefaultMapper will return clone of default mapper function -func GetNewDefaultMapper() mapper { +//GetNewDefaultMapper will return clone of default Mapper function +func GetNewDefaultMapper() Mapper { return _defaultMapper.clone() } -//SetBidderMapper will be used by each bidder to set its respective macro mapper -func SetBidderMapper(bidder string, bidderMap mapper) { +/* +var bidderMapper map[string]Mapper + +//SetBidderMapper will be used by each bidder to set its respective macro Mapper +func SetBidderMapper(bidder string, bidderMap Mapper) { bidderMapper[bidder] = bidderMap } -//GetBidderMapper will return mapper of specific bidder -func GetBidderMapper(bidder string) mapper { +//GetBidderMapper will return Mapper of specific bidder +func GetBidderMapper(bidder string) Mapper { return bidderMapper[bidder] } +*/ diff --git a/adapters/tagbidder/spotxtag/constant.go b/adapters/tagbidder/spotxtag/constant.go new file mode 100644 index 00000000000..0a2c76cc3e0 --- /dev/null +++ b/adapters/tagbidder/spotxtag/constant.go @@ -0,0 +1,7 @@ +package spotxtag + +const ( + spotxURL = `https://search.spotxchange.com/vast/2.00/85394?VPI=MP4&app[bundle]=[REPLACE_ME]&app[name]=[REPLACE_ME]&app[cat]=[REPLACE_ME]&app[domain]=[REPLACE_ME]&app[privacypolicy]=[REPLACE_ME]&app[storeurl]=[REPLACE_ME]&app[ver]=[REPLACE_ME]&cb=[REPLACE_ME]&device[devicetype]=[REPLACE_ME]&device[ifa]=[REPLACE_ME]&device[make]=[REPLACE_ME]&device[model]=[REPLACE_ME]&device[dnt]=[REPLACE_ME]&player_height=[REPLACE_ME]&player_width=[REPLACE_ME]&ip_addr=[REPLACE_ME]&device[ua]=[REPLACE_ME]]&schain=[REPLACE_ME]` + spotxFixedQueryParams = `` + spotxVariableQueryParams = `` +) diff --git a/adapters/tagbidder/spotxtag/spotx_adapter.go b/adapters/tagbidder/spotxtag/spotx_adapter.go new file mode 100644 index 00000000000..41e29e67ded --- /dev/null +++ b/adapters/tagbidder/spotxtag/spotx_adapter.go @@ -0,0 +1,44 @@ +package spotxtag + +import ( + "fmt" + "net/http" + + "github.com/PubMatic-OpenWrap/openrtb" + "github.com/PubMatic-OpenWrap/prebid-server/adapters" + "github.com/PubMatic-OpenWrap/prebid-server/adapters/tagbidder" +) + +type SpotxAdapter struct { + *tagbidder.TagBidder + uri string +} + +func NewSpotxAdapter(uri string) *SpotxAdapter { + return &SpotxAdapter{ + TagBidder: tagbidder.NewTagBidder(), + uri: uri, + } +} + +func (a *SpotxAdapter) GetURI() string { + return a.uri +} + +func (a *SpotxAdapter) GetHeaders() http.Header { + return http.Header{} +} + +func (a *SpotxAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { + return a.TagBidder.MakeRequests( + request, reqInfo, + NewSpotxMacro(), + spotxMapper, + tagbidder.Flags{RemoveEmptyParam: true}) +} + +func (a *SpotxAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { + return nil, []error{ + fmt.Errorf("No Bid"), + } +} diff --git a/adapters/tagbidder/spotx_bidder_test.go b/adapters/tagbidder/spotxtag/spotx_macro.go similarity index 52% rename from adapters/tagbidder/spotx_bidder_test.go rename to adapters/tagbidder/spotxtag/spotx_macro.go index b540ed68c56..706c6b6264e 100644 --- a/adapters/tagbidder/spotx_bidder_test.go +++ b/adapters/tagbidder/spotxtag/spotx_macro.go @@ -1,42 +1,34 @@ -package tagbidder +package spotxtag import ( "encoding/json" "github.com/PubMatic-OpenWrap/openrtb" "github.com/PubMatic-OpenWrap/prebid-server/adapters" + "github.com/PubMatic-OpenWrap/prebid-server/adapters/tagbidder" "github.com/PubMatic-OpenWrap/prebid-server/errortypes" "github.com/PubMatic-OpenWrap/prebid-server/openrtb_ext" ) -const ( - spotxURL = `https://search.spotxchange.com/vast/2.00/85394?VPI=MP4&app[bundle]=[REPLACE_ME]&app[name]=[REPLACE_ME]&app[cat]=[REPLACE_ME]&app[domain]=[REPLACE_ME]&app[privacypolicy]=[REPLACE_ME]&app[storeurl]=[REPLACE_ME]&app[ver]=[REPLACE_ME]&cb=[REPLACE_ME]&device[devicetype]=[REPLACE_ME]&device[ifa]=[REPLACE_ME]&device[make]=[REPLACE_ME]&device[model]=[REPLACE_ME]&device[dnt]=[REPLACE_ME]&player_height=[REPLACE_ME]&player_width=[REPLACE_ME]&ip_addr=[REPLACE_ME]&device[ua]=[REPLACE_ME]]&schain=[REPLACE_ME]` - spotxFixedQueryParams = `` - spotxVariableQueryParams = `` -) - -//SpotxBidderMacro contains openrtb macros for spotx adapter -type SpotxBidderMacro struct { - *BidderMacro +//SpotxMacro contains openrtb macros for spotx adapter +type SpotxMacro struct { + *tagbidder.BidderMacro /*bidder specific extensions*/ ext *openrtb_ext.ExtImpSpotX } -var spotxMapper mapper -var spotxCustomMapper map[string]func(*SpotxBidderMacro) string - -//NewSpotxBidderMacro contains spotx specific parameter parsing -func NewSpotxBidderMacro(request *openrtb.BidRequest) *SpotxBidderMacro { - bidder := &SpotxBidderMacro{ - BidderMacro: NewBidderMacro(request), +//NewSpotxMacro contains spotx specific parameter parsing +func NewSpotxMacro() *SpotxMacro { + bidder := &SpotxMacro{ + BidderMacro: tagbidder.NewBidderMacro(), } return bidder } //LoadImpression will set current imp -func (tag *SpotxBidderMacro) LoadImpression(imp *openrtb.Imp) error { - tag.imp = imp +func (tag *SpotxMacro) LoadImpression(imp *openrtb.Imp) error { + tag.Imp = imp //reload ext object var bidderExt adapters.ExtImpBidder @@ -53,76 +45,28 @@ func (tag *SpotxBidderMacro) LoadImpression(imp *openrtb.Imp) error { return nil } -//URL will set current imp -func (tag *SpotxBidderMacro) URL() string { - return spotxURL -} - -//Name will set current imp -func (tag *SpotxBidderMacro) Name() string { - return `spotx` -} - //Custom contains definition for CacheBuster Parameter -func (tag *SpotxBidderMacro) Custom(key string) string { - //First Method - if callback, ok := spotxCustomMapper[key]; ok { - return callback(tag) - } - +func (tag *SpotxMacro) Custom(key string) string { //Second Method switch key { case `channel_id`: //do processing - return channelID(tag) + return tag.ext.ChannelID } return "" } //MacroVideoAPI overriding default behaviour of MacroVideoAPI -func (tag *SpotxBidderMacro) MacroVideoAPI(key string) string { +func (tag *SpotxMacro) MacroVideoAPI(key string) string { return "MP4" } -func channelID(tag *SpotxBidderMacro) string { - return tag.ext.ChannelID -} - -//Second Method of Adding Custom Macro's -func addCustomMacro(key string, cached bool, callback func(*SpotxBidderMacro) string) { - spotxMapper.AddCustomMacro(key, cached) - spotxCustomMapper[key] = callback -} - -func initSpotxMapper() { - spotxMapper = GetNewDefaultMapper() - /* - //updating parameter caching status - spotxMapper.SetCache(MacroTest, true) - */ - - /* - //adding custom macros - //First Method - spotxMapper.AddCustomMacro(`ad_unit"`,false) - - //Second Method - addCustomMacro(`channel_id`, false, channelID) - */ - - SetBidderMapper(`spotx`, spotxMapper) -} - -func init() { - initSpotxMapper() -} - /* Custom Mapper Example -var spotxCustomMapper map[string]func(*SpotxBidderMacro) string +var spotxCustomMapper map[string]func(*SpotxMacro) string //Second Method of Adding Custom Macro's -func addCustomMacro(key string, cached bool, callback func(*SpotxBidderMacro) string) { +func addCustomMacro(key string, cached bool, callback func(*SpotxMacro) string) { spotxMapper.AddCustomMacro(key, cached) spotxCustomMapper[key] = callback } @@ -131,14 +75,14 @@ func addCustomMacro(key string, cached bool, callback func(*SpotxBidderMacro) st addCustomMacro(`channel_id`, false, channelID) //Custom contains definition for CacheBuster Parameter -func (tag *SpotxBidderMacro) Custom(key string) string { +func (tag *SpotxMacro) Custom(key string) string { //First Method if callback, ok := spotxCustomMapper[key]; ok { return callback(tag) } } -func channelID(tag *SpotxBidderMacro) string { +func channelID(tag *SpotxMacro) string { return tag.ext.ChannelID } diff --git a/adapters/tagbidder/spotxtag/spotx_mapper.go b/adapters/tagbidder/spotxtag/spotx_mapper.go new file mode 100644 index 00000000000..c03eb1095ad --- /dev/null +++ b/adapters/tagbidder/spotxtag/spotx_mapper.go @@ -0,0 +1,18 @@ +package spotxtag + +import "github.com/PubMatic-OpenWrap/prebid-server/adapters/tagbidder" + +var spotxMapper tagbidder.Mapper + +func init() { + spotxMapper = tagbidder.GetNewDefaultMapper() + /* + //updating parameter caching status + spotxMapper.SetCache(MacroTest, true) + + //adding custom macros + spotxMapper.AddCustomMacro(`ad_unit"`,false) + */ + + //SetBidderMapper(`spotx`, spotxMapper) +} diff --git a/adapters/tagbidder/tagbidder.go b/adapters/tagbidder/tagbidder.go new file mode 100644 index 00000000000..0660adaf6d6 --- /dev/null +++ b/adapters/tagbidder/tagbidder.go @@ -0,0 +1,43 @@ +package tagbidder + +import ( + "net/http" + + "github.com/PubMatic-OpenWrap/openrtb" + "github.com/PubMatic-OpenWrap/prebid-server/adapters" +) + +type ITagBidder interface { + GetURI() string + GetHeaders() http.Header +} + +type TagBidder struct { + ITagBidder +} + +func NewTagBidder() *TagBidder { + return &TagBidder{} +} + +func (a *TagBidder) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo, bidderMacro IBidderMacro, bidderMapper Mapper, flags Flags) ([]*adapters.RequestData, []error) { + macroProcessor := NewMacroProcessor(bidderMapper) + + bidderMacro.InitBidRequest(request) + + requestData := []*adapters.RequestData{} + for i := range request.Imp { + bidderMacro.LoadImpression(&request.Imp[i]) + + uri := macroProcessor.ProcessURL(a.ITagBidder.GetURI(), flags) + + requestData = append(requestData, &adapters.RequestData{ + ImpIndex: i, + Method: `GET`, + Uri: uri, + Headers: a.ITagBidder.GetHeaders(), + }) + } + + return requestData, nil +} diff --git a/adapters/tagbidder/tagbidder_test.go b/adapters/tagbidder/tagbidder_test.go deleted file mode 100644 index 3cc1cf430be..00000000000 --- a/adapters/tagbidder/tagbidder_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package tagbidder - -import ( - "fmt" - "net/http" - "testing" - - "github.com/PubMatic-OpenWrap/openrtb" -) - -func getHTTPRequest(tagBidder ITagBidder, mp *MacroProcessor, bidRequest *openrtb.BidRequest) { - for i := range bidRequest.Imp { - tagBidder.LoadImpression(&bidRequest.Imp[i]) - queryString := mp.Process(tagBidder.URL()) - fmt.Printf("Query:%v\n", queryString) - } -} - -func TestBidder(t *testing.T) { - var bidRequest *openrtb.BidRequest - tagBidder := NewSpotxBidderMacro(bidRequest) - mapper := GetBidderMapper(tagBidder.Name()) - mp := NewMacroProcessor(mapper) - - getHTTPRequest(tagBidder, mp, bidRequest) -} - -type IRequestHandler interface { - GetHeaders() http.Header - GetHTTP -}