forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrespdataprocessor.go
176 lines (149 loc) · 4.84 KB
/
respdataprocessor.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package adservertargeting
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/prebid/prebid-server/v2/util/jsonutil"
jsonpatch "gopkg.in/evanphx/json-patch.v4"
)
const MaxKeyLength = 20
func processRequestTargetingData(adServerTargetingData *adServerTargetingData, targetingData map[string]string, bidImpId string) {
for key, val := range adServerTargetingData.RequestTargetingData {
if len(val.SingleVal) > 0 {
targetingData[key] = string(val.SingleVal)
} else if len(val.TargetingValueByImpId) > 0 {
targetingData[key] = string(val.TargetingValueByImpId[bidImpId])
}
}
}
func processResponseTargetingData(
adServerTargetingData *adServerTargetingData,
targetingData map[string]string,
bidderName string,
bid openrtb2.Bid,
bidsHolder bidsCache,
response *openrtb2.BidResponse,
seatExt json.RawMessage) []openrtb_ext.ExtBidderMessage {
var warnings []openrtb_ext.ExtBidderMessage
for _, respTargetingData := range adServerTargetingData.ResponseTargetingData {
key := resolveKey(respTargetingData, bidderName)
path := respTargetingData.Path
var value string
var err error
pathSplit := strings.Split(path, pathDelimiter)
switch pathSplit[0] {
case "seatbid":
switch pathSplit[1] {
case "bid":
value, err = getValueFromSeatBidBid(path, bidsHolder, bidderName, bid)
case "ext":
value, err = getValueFromExt(path, "seatbid.ext.", seatExt)
}
case "ext":
value, err = getValueFromExt(path, "ext.", response.Ext)
default:
value, err = getValueFromResp(path, response)
}
if err != nil {
message := fmt.Sprintf("%s for bidder: %s, bid id: %s", err.Error(), bidderName, bid.ID)
warnings = append(warnings, createWarning(message))
} else {
targetingData[key] = value
}
}
return warnings
}
func buildBidExt(targetingData map[string]string,
bid openrtb2.Bid,
warnings []openrtb_ext.ExtBidderMessage,
truncateTargetAttribute *int) json.RawMessage {
targetingDataTruncated := truncateTargetingKeys(targetingData, truncateTargetAttribute)
bidExtTargetingData := openrtb_ext.ExtBid{
Prebid: &openrtb_ext.ExtBidPrebid{
Targeting: targetingDataTruncated,
},
}
bidExtTargeting, err := jsonutil.Marshal(bidExtTargetingData)
if err != nil {
warnings = append(warnings, createWarning(err.Error())) //nolint: ineffassign,staticcheck
return nil
}
newExt, err := jsonpatch.MergePatch(bid.Ext, bidExtTargeting)
if err != nil {
warnings = append(warnings, createWarning(err.Error())) //nolint: ineffassign,staticcheck
return nil
}
return newExt
}
func resolveKey(respTargetingData ResponseTargetingData, bidderName string) string {
key := respTargetingData.Key
if respTargetingData.HasMacro {
key = strings.Replace(respTargetingData.Key, bidderMacro, bidderName, -1)
}
return key
}
func truncateTargetingKeys(targetingData map[string]string, truncateTargetAttribute *int) map[string]string {
maxLength := MaxKeyLength
if truncateTargetAttribute != nil {
maxLength = *truncateTargetAttribute
if maxLength <= 0 {
maxLength = MaxKeyLength
}
}
targetingDataTruncated := make(map[string]string)
for key, value := range targetingData {
newKey := openrtb_ext.TargetingKey(key).TruncateKey(maxLength)
targetingDataTruncated[newKey] = value
}
return targetingDataTruncated
}
func getValueFromSeatBidBid(path string, bidsHolder bidsCache, bidderName string, bid openrtb2.Bid) (string, error) {
bidBytes, err := bidsHolder.GetBid(bidderName, bid.ID, bid)
if err != nil {
return "", err
}
bidSplit := strings.Split(path, "seatbid.bid.")
value, err := splitAndGet(bidSplit[1], bidBytes, pathDelimiter)
if err != nil {
return "", err
}
return value, nil
}
func getValueFromExt(path, separator string, respExt json.RawMessage) (string, error) {
//path points to resp.ext, means path starts with ext
extSplit := strings.Split(path, separator)
value, err := splitAndGet(extSplit[1], respExt, pathDelimiter)
if err != nil {
return "", err
}
return value, nil
}
// getValueFromResp optimizes retrieval of paths for a bid response (not a seat or ext)
func getValueFromResp(path string, response *openrtb2.BidResponse) (string, error) {
value, err := getRespData(response, path)
if err != nil {
return "", err
}
return value, nil
}
func getRespData(bidderResp *openrtb2.BidResponse, field string) (string, error) {
// this code should be modified if there are changes in OpenRtb format
// it's up to date with OpenRTB 2.5 and 2.6
switch field {
case "id":
return bidderResp.ID, nil
case "bidid":
return bidderResp.BidID, nil
case "cur":
return bidderResp.Cur, nil
case "customdata":
return bidderResp.CustomData, nil
case "nbr":
return fmt.Sprint(bidderResp.NBR.Val()), nil
default:
return "", errors.Errorf("key not found for field in bid response: %s", field)
}
}