From 5cbf91e9124435083f8f6627ba49844f4273ca2b Mon Sep 17 00:00:00 2001 From: Viral Vala Date: Mon, 14 Dec 2020 19:30:40 +0530 Subject: [PATCH] refactored code --- adapters/tagbidder/bidder_config.go | 56 ++++++++++++++++++++ adapters/tagbidder/constant.go | 8 +-- adapters/tagbidder/mapper.go | 14 ++--- adapters/tagbidder/spotxtag/constant.go | 30 ----------- adapters/tagbidder/spotxtag/spotx_adapter.go | 2 +- adapters/tagbidder/tagbidder.go | 12 +++-- static/tagbidder-params/spotx.json | 29 ++++++++++ 7 files changed, 102 insertions(+), 49 deletions(-) create mode 100644 adapters/tagbidder/bidder_config.go create mode 100644 static/tagbidder-params/spotx.json diff --git a/adapters/tagbidder/bidder_config.go b/adapters/tagbidder/bidder_config.go new file mode 100644 index 00000000000..8c51ea0f1d3 --- /dev/null +++ b/adapters/tagbidder/bidder_config.go @@ -0,0 +1,56 @@ +package tagbidder + +import ( + "encoding/json" + "io/ioutil" + + "github.com/golang/glog" +) + +//Keys each macro mapping key definition +type Keys struct { + Cached *bool `json:"cached,omitempty"` + Type MacroKeyType `json:"type,omitempty"` +} + +//BidderConfig mapper json +type BidderConfig struct { + URL string `json:"url,omitempty"` + Keys map[string]Keys `json:"keys,omitempty"` +} + +var bidderConfig map[string]*BidderConfig + +//RegisterBidderConfig will be used by each bidder to set its respective macro Mapper +func RegisterBidderConfig(bidder string, config *BidderConfig) { + bidderConfig[bidder] = config +} + +//GetBidderConfig will return Mapper of specific bidder +func GetBidderConfig(bidder string) *BidderConfig { + return bidderConfig[bidder] +} + +//FetchBidderConfig returns new Mapper from JSON details +func FetchBidderConfig(confDir string, bidders []string) { + for _, bidderName := range bidders { + bidderString := string(bidderName) + fileData, err := ioutil.ReadFile(confDir + "/" + bidderString + ".json") + if err != nil { + glog.Fatalf("error reading from file %s: %v", confDir+"/"+bidderString+".json", err) + } + + var bidderConfig BidderConfig + if err := json.Unmarshal([]byte(fileData), &bidderConfig); nil != err { + glog.Fatalf("error parsing json in file %s: %v", confDir+"/"+bidderString+".json", err) + } + + mapper := NewMapperFromConfig(&bidderConfig) + if nil == mapper { + glog.Fatalf("no query parameters mapper for bidder " + bidderString) + } + + RegisterBidderMapper(bidderString, mapper) + RegisterBidderConfig(bidderString, &bidderConfig) + } +} diff --git a/adapters/tagbidder/constant.go b/adapters/tagbidder/constant.go index c6d90f66457..cfc5934b21e 100644 --- a/adapters/tagbidder/constant.go +++ b/adapters/tagbidder/constant.go @@ -155,10 +155,10 @@ const ( ) //MacroKeyType types of macro keys -type MacroKeyType int +type MacroKeyType string const ( - UnkownMacroKeys MacroKeyType = 0 - StandardORTBMacroKeys MacroKeyType = 1 - CustomORTBMacroKeys MacroKeyType = 2 + UnkownMacroKeys MacroKeyType = `` + StandardORTBMacroKeys MacroKeyType = `standard` + CustomORTBMacroKeys MacroKeyType = `custom` ) diff --git a/adapters/tagbidder/mapper.go b/adapters/tagbidder/mapper.go index 0fbaac49492..b0f1ac71b8e 100644 --- a/adapters/tagbidder/mapper.go +++ b/adapters/tagbidder/mapper.go @@ -1,7 +1,5 @@ package tagbidder -import "encoding/json" - type macroCallBack struct { cached bool callback func(IBidderMacro, string) string @@ -207,15 +205,10 @@ type MapperJSON struct { Keys map[string]MapperKey `json:"keys,omitempty"` } -//NewMapperFromJSON returns new Mapper from JSON details -func NewMapperFromJSON(jsonStr string) Mapper { - var mapperJSON MapperJSON - if err := json.Unmarshal([]byte(jsonStr), &mapperJSON); nil != err { - return nil - } - +//NewMapperFromConfig returns new Mapper from JSON details +func NewMapperFromConfig(config *BidderConfig) Mapper { newMapper := GetNewDefaultMapper() - for name, key := range mapperJSON.Keys { + for name, key := range config.Keys { switch key.Type { case StandardORTBMacroKeys: /*standard keys*/ if nil != key.Cached { @@ -226,6 +219,5 @@ func NewMapperFromJSON(jsonStr string) Mapper { newMapper.AddCustomMacro(name, cached) } } - return newMapper } diff --git a/adapters/tagbidder/spotxtag/constant.go b/adapters/tagbidder/spotxtag/constant.go index eabfeab40f9..066d14b6306 100644 --- a/adapters/tagbidder/spotxtag/constant.go +++ b/adapters/tagbidder/spotxtag/constant.go @@ -3,34 +3,4 @@ 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]` spotxURLWithMacros = `https://search.spotxchange.com/vast/2.00/%%Channel%%?VPI=MP4&app[bundle]=%%MacroAppBundle%%&app[name]=%%MacroAppName%%&app[cat]=%%MacroCategory%%&app[domain]=%%MacroDomain%%&app[privacypolicy]=%%MacroPrivacyPolicy%%&app[storeurl]=%%MacroAppStoreURL%%&app[ver]=%%MacroAppVersion%%&cb=%%MacroCacheBuster%%&device[devicetype]=%%MacroDeviceType%%&device[ifa]=%%MacroDeviceIFA%%&device[make]=%%MacroMake%%&device[model]=%%MacroModel%%&device[dnt]=%%MacroDNT%%&player_height=%%MacroVideoPlayerHeight%%&player_width=%%MacroVideoPlayerWidth%%&ip_addr=%%MacroIP%%&device[ua]=%%MacroUserAgent%%` - spotxVariableQueryParams = `` - - spotxMapperJSON = `{ - "keys": { - "channel_id": { - "cached": false, - "type": 2 - }, - "ad_unit": { - "cached": false, - "type": 2 - }, - "secure": { - "cached": false, - "type": 2 - }, - "ad_volume": { - "cached": false, - "type": 2 - }, - "price_floor": { - "cached": false, - "type": 2 - }, - "hide_skin": { - "cached": false, - "type": 2 - } - } - }` ) diff --git a/adapters/tagbidder/spotxtag/spotx_adapter.go b/adapters/tagbidder/spotxtag/spotx_adapter.go index 00404f21cdc..73da5e4d419 100644 --- a/adapters/tagbidder/spotxtag/spotx_adapter.go +++ b/adapters/tagbidder/spotxtag/spotx_adapter.go @@ -47,5 +47,5 @@ func (a *SpotxAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalReq } func init() { - tagbidder.RegisterNewTagBidder(`spotx`, NewSpotxMacro, spotxMapperJSON) + tagbidder.RegisterNewTagBidder(`spotx`, NewSpotxMacro) } diff --git a/adapters/tagbidder/tagbidder.go b/adapters/tagbidder/tagbidder.go index b4dfe84bfe6..46c70a90215 100644 --- a/adapters/tagbidder/tagbidder.go +++ b/adapters/tagbidder/tagbidder.go @@ -61,9 +61,15 @@ func (a *TagBidder) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters. return requestData, nil } +//MakeBids makes bids +func (a *TagBidder) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { + //response validation can be done here independently + var handler ITagResponseHandler + handler = NewVASTTagResponseHandler() + return handler.MakeBids(internalRequest, externalRequest, response) +} + //RegisterNewTagBidder will register new tag bidder -func RegisterNewTagBidder(bidderName string, bidderMacro func() IBidderMacro, mapperJSON string) { - spotxMapper := NewMapperFromJSON(mapperJSON) - RegisterBidderMapper(bidderName, spotxMapper) +func RegisterNewTagBidder(bidderName string, bidderMacro func() IBidderMacro) { RegisterNewBidderMacroInitializer(bidderName, bidderMacro) } diff --git a/static/tagbidder-params/spotx.json b/static/tagbidder-params/spotx.json new file mode 100644 index 00000000000..2138983d641 --- /dev/null +++ b/static/tagbidder-params/spotx.json @@ -0,0 +1,29 @@ +{ + "url": "https://search.spotxchange.com/vast/2.00/%%Channel%%?VPI=MP4&app[bundle]=%%MacroAppBundle%%&app[name]=%%MacroAppName%%&app[cat]=%%MacroCategory%%&app[domain]=%%MacroDomain%%&app[privacypolicy]=%%MacroPrivacyPolicy%%&app[storeurl]=%%MacroAppStoreURL%%&app[ver]=%%MacroAppVersion%%&cb=%%MacroCacheBuster%%&device[devicetype]=%%MacroDeviceType%%&device[ifa]=%%MacroDeviceIFA%%&device[make]=%%MacroMake%%&device[model]=%%MacroModel%%&device[dnt]=%%MacroDNT%%&player_height=%%MacroVideoPlayerHeight%%&player_width=%%MacroVideoPlayerWidth%%&ip_addr=%%MacroIP%%&device[ua]=%%MacroUserAgent%%", + "keys": { + "channel_id": { + "cached": false, + "type": "custom" + }, + "ad_unit": { + "cached": false, + "type": "custom" + }, + "secure": { + "cached": false, + "type": "custom" + }, + "ad_volume": { + "cached": false, + "type": "custom" + }, + "price_floor": { + "cached": false, + "type": "custom" + }, + "hide_skin": { + "cached": false, + "type": "custom" + } + } +} \ No newline at end of file