forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestlookup.go
138 lines (117 loc) · 4.02 KB
/
requestlookup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package adservertargeting
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/buger/jsonparser"
"github.com/pkg/errors"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)
func getAdServerTargeting(reqWrapper *openrtb_ext.RequestWrapper) ([]openrtb_ext.AdServerTarget, error) {
reqExt, err := reqWrapper.GetRequestExt()
if err != nil {
return nil, err
}
reqExtPrebid := reqExt.GetPrebid()
if reqExtPrebid == nil {
return nil, nil
}
return reqExtPrebid.AdServerTargeting, nil
}
func validateAdServerTargeting(adServerTargeting []openrtb_ext.AdServerTarget) ([]openrtb_ext.AdServerTarget, []openrtb_ext.ExtBidderMessage) {
var validatedAdServerTargeting []openrtb_ext.AdServerTarget
var warnings []openrtb_ext.ExtBidderMessage
for i, targetingObj := range adServerTargeting {
isDataCorrect := true
if len(targetingObj.Key) == 0 {
isDataCorrect = false
warnings = append(warnings, createWarning(fmt.Sprintf("Key is empty for the ad server targeting object at index %d", i)))
}
if len(targetingObj.Value) == 0 {
isDataCorrect = false
warnings = append(warnings, createWarning(fmt.Sprintf("Value is empty for the ad server targeting object at index %d", i)))
}
targetingObjSource := DataSource(strings.ToLower(targetingObj.Source))
if targetingObjSource != SourceStatic &&
targetingObjSource != SourceBidRequest &&
targetingObjSource != SourceBidResponse {
isDataCorrect = false
warnings = append(warnings, createWarning(fmt.Sprintf("Incorrect source for the ad server targeting object at index %d", i)))
}
if isDataCorrect {
validatedAdServerTargeting = append(validatedAdServerTargeting, targetingObj)
}
}
return validatedAdServerTargeting, warnings
}
func getValueFromBidRequest(dataHolder *requestCache, path string, queryParams url.Values) (RequestTargetingData, error) {
//use the path specified in 'value' to look for data in the ortb bidrequest.
res := RequestTargetingData{}
//check if key points to query param from ext.prebid.amp.data
ampDataValue, err := getValueFromQueryParam(path, queryParams)
if ampDataValue != nil || err != nil {
res.SingleVal = ampDataValue
return res, err
}
// check if key points to imp data
impData, err := getValueFromImp(path, dataHolder)
if len(impData) > 0 || err != nil {
res.TargetingValueByImpId = impData
return res, err
}
// get data by key from request
requestValue, err := getDataFromRequestJson(path, dataHolder)
if requestValue != nil || err != nil {
res.SingleVal = requestValue
return res, err
}
return res, nil
}
func getValueFromQueryParam(path string, queryParams url.Values) (json.RawMessage, error) {
ampDataSplit, hasPrefix := verifyPrefixAndTrim(path, "ext.prebid.amp.data.")
if hasPrefix {
val := queryParams.Get(ampDataSplit)
if val != "" {
return json.RawMessage(val), nil
} else {
return nil, errors.Errorf("value not found for path: %s", path)
}
}
return nil, nil
}
func getValueFromImp(path string, dataHolder *requestCache) (map[string][]byte, error) {
impsDatas := make(map[string][]byte, 0)
impSplit, hasPrefix := verifyPrefixAndTrim(path, "imp.")
if hasPrefix {
//If imp is specified in the path, the assumption is that the specific imp[] desired corresponds
//to the seatbid[].bid[] we're working on. i.e. imp[].id=seatbid[].bid[].impid
// key points to data in imp
keySplit := strings.Split(impSplit, pathDelimiter)
impsData, err := dataHolder.GetImpsData()
if err != nil {
return nil, err
}
for _, impData := range impsData {
id, _, _, err := jsonparser.Get(impData, "id")
if err != nil {
return nil, err
}
value, err := typedLookup(impData, path, keySplit...)
if err != nil {
return nil, err
}
impsDatas[string(id)] = value
}
}
return impsDatas, nil
}
func getDataFromRequestJson(path string, dataHolder *requestCache) (json.RawMessage, error) {
keySplit := strings.Split(path, pathDelimiter)
reqJson := dataHolder.GetReqJson()
value, err := typedLookup(reqJson, path, keySplit...)
if err != nil {
return nil, err
}
return value, nil
}