Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
pm-viral-vala committed Dec 14, 2020
1 parent 26c0619 commit 5cbf91e
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 49 deletions.
56 changes: 56 additions & 0 deletions adapters/tagbidder/bidder_config.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
8 changes: 4 additions & 4 deletions adapters/tagbidder/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
14 changes: 3 additions & 11 deletions adapters/tagbidder/mapper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package tagbidder

import "encoding/json"

type macroCallBack struct {
cached bool
callback func(IBidderMacro, string) string
Expand Down Expand Up @@ -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 {
Expand All @@ -226,6 +219,5 @@ func NewMapperFromJSON(jsonStr string) Mapper {
newMapper.AddCustomMacro(name, cached)
}
}

return newMapper
}
30 changes: 0 additions & 30 deletions adapters/tagbidder/spotxtag/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}`
)
2 changes: 1 addition & 1 deletion adapters/tagbidder/spotxtag/spotx_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func (a *SpotxAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalReq
}

func init() {
tagbidder.RegisterNewTagBidder(`spotx`, NewSpotxMacro, spotxMapperJSON)
tagbidder.RegisterNewTagBidder(`spotx`, NewSpotxMacro)
}
12 changes: 9 additions & 3 deletions adapters/tagbidder/tagbidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
29 changes: 29 additions & 0 deletions static/tagbidder-params/spotx.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit 5cbf91e

Please sign in to comment.