Skip to content

Commit

Permalink
OTT-55 Second Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pm-viral-vala committed Dec 1, 2020
1 parent 911db3e commit 81841e5
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 149 deletions.
38 changes: 20 additions & 18 deletions adapters/tagbidder/bidder_macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion adapters/tagbidder/ibidder_macro.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 0 additions & 9 deletions adapters/tagbidder/itagbidder.go

This file was deleted.

43 changes: 41 additions & 2 deletions adapters/tagbidder/macro_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 16 additions & 14 deletions adapters/tagbidder/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = &macroCallBack{cached: isCached, callback: IBidderMacro.Custom}
}

var _defaultMapper = mapper{
var _defaultMapper = Mapper{
//Request
MacroTest: &macroCallBack{cached: false, callback: IBidderMacro.MacroTest},
MacroTimeout: &macroCallBack{cached: false, callback: IBidderMacro.MacroTimeout},
Expand Down Expand Up @@ -178,17 +176,21 @@ var _defaultMapper = mapper{
MacroCacheBuster: &macroCallBack{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]
}
*/
7 changes: 7 additions & 0 deletions adapters/tagbidder/spotxtag/constant.go
Original file line number Diff line number Diff line change
@@ -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 = ``
)
44 changes: 44 additions & 0 deletions adapters/tagbidder/spotxtag/spotx_adapter.go
Original file line number Diff line number Diff line change
@@ -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"),
}
}
Loading

0 comments on commit 81841e5

Please sign in to comment.