-
Notifications
You must be signed in to change notification settings - Fork 739
/
unicorn.go
280 lines (234 loc) · 6.86 KB
/
unicorn.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package unicorn
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/buger/jsonparser"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)
type adapter struct {
endpoint string
}
// unicornImpExt is imp ext for UNICORN
type unicornImpExt struct {
Context *unicornImpExtContext `json:"context,omitempty"`
Bidder openrtb_ext.ExtImpUnicorn `json:"bidder"`
}
type unicornImpExtContext struct {
Data interface{} `json:"data,omitempty"`
}
// unicornExt is ext for UNICORN
type unicornExt struct {
Prebid *openrtb_ext.ExtImpPrebid `json:"prebid,omitempty"`
AccountID int64 `json:"accountId,omitempty"`
}
// Builder builds a new instance of the UNICORN adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
bidder := &adapter{
endpoint: config.Endpoint,
}
return bidder, nil
}
// MakeRequests makes the HTTP requests which should be made to fetch bids.
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var extRegs openrtb_ext.ExtRegs
if request.Regs != nil {
if request.Regs.COPPA == 1 {
return nil, []error{&errortypes.BadInput{
Message: "COPPA is not supported",
}}
}
if err := json.Unmarshal(request.Regs.Ext, &extRegs); err == nil {
if extRegs.GDPR != nil && (*extRegs.GDPR == 1) {
return nil, []error{&errortypes.BadInput{
Message: "GDPR is not supported",
}}
}
if extRegs.USPrivacy != "" {
return nil, []error{&errortypes.BadInput{
Message: "CCPA is not supported",
}}
}
}
}
err := modifyImps(request)
if err != nil {
return nil, []error{err}
}
if err := modifyApp(request); err != nil {
return nil, []error{err}
}
var modifiableSource openrtb2.Source
if request.Source != nil {
modifiableSource = *request.Source
} else {
modifiableSource = openrtb2.Source{}
}
modifiableSource.Ext = setSourceExt()
request.Source = &modifiableSource
request.Ext, err = setExt(request)
if err != nil {
return nil, []error{err}
}
requestJSON, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
}
requestData := &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: requestJSON,
Headers: getHeaders(request),
}
return []*adapters.RequestData{requestData}, nil
}
func getHeaders(request *openrtb2.BidRequest) http.Header {
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
headers.Add("X-Openrtb-Version", "2.5")
if request.Device != nil {
if len(request.Device.UA) > 0 {
headers.Add("User-Agent", request.Device.UA)
}
if len(request.Device.IPv6) > 0 {
headers.Add("X-Forwarded-For", request.Device.IPv6)
}
if len(request.Device.IP) > 0 {
headers.Add("X-Forwarded-For", request.Device.IP)
}
}
return headers
}
func modifyImps(request *openrtb2.BidRequest) error {
for i := 0; i < len(request.Imp); i++ {
imp := &request.Imp[i]
var ext unicornImpExt
err := json.Unmarshal(imp.Ext, &ext)
if err != nil {
return &errortypes.BadInput{
Message: fmt.Sprintf("Error while decoding imp[%d].ext: %s", i, err),
}
}
if ext.Bidder.PlacementID == "" {
ext.Bidder.PlacementID, err = getStoredRequestImpID(imp)
if err != nil {
return &errortypes.BadInput{
Message: fmt.Sprintf("Error get StoredRequestImpID from imp[%d]: %s", i, err),
}
}
}
imp.Ext, err = json.Marshal(ext)
if err != nil {
return &errortypes.BadInput{
Message: fmt.Sprintf("Error while encoding imp[%d].ext: %s", i, err),
}
}
secure := int8(1)
imp.Secure = &secure
imp.TagID = ext.Bidder.PlacementID
}
return nil
}
func getStoredRequestImpID(imp *openrtb2.Imp) (string, error) {
v, err := jsonparser.GetString(imp.Ext, "prebid", "storedrequest", "id")
if err != nil {
return "", fmt.Errorf("stored request id not found: %s", err)
}
return v, nil
}
func setSourceExt() json.RawMessage {
return json.RawMessage(`{"stype": "prebid_server_uncn", "bidder": "unicorn"}`)
}
func modifyApp(request *openrtb2.BidRequest) error {
if request.App == nil {
return errors.New("request app is required")
}
modifiableApp := *request.App
mediaId, err := jsonparser.GetString(request.Imp[0].Ext, "bidder", "mediaId")
if err == nil {
modifiableApp.ID = mediaId
}
publisherId, err := jsonparser.GetString(request.Imp[0].Ext, "bidder", "publisherId")
if err == nil {
var publisher openrtb2.Publisher
if modifiableApp.Publisher != nil {
publisher = *modifiableApp.Publisher
} else {
publisher = openrtb2.Publisher{}
}
publisher.ID = publisherId
modifiableApp.Publisher = &publisher
}
request.App = &modifiableApp
return nil
}
func setExt(request *openrtb2.BidRequest) (json.RawMessage, error) {
accountID, err := jsonparser.GetInt(request.Imp[0].Ext, "bidder", "accountId")
if err != nil {
return nil, fmt.Errorf("accountId field is required")
}
var decodedExt *unicornExt
err = json.Unmarshal(request.Ext, &decodedExt)
if err != nil {
decodedExt = &unicornExt{
Prebid: nil,
}
}
decodedExt.AccountID = accountID
ext, err := json.Marshal(decodedExt)
if err != nil {
return nil, &errortypes.BadInput{
Message: fmt.Sprintf("Error while encoding ext, err: %s", err),
}
}
return ext, nil
}
// MakeBids unpacks the server's response into Bids.
func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
return nil, nil
}
if responseData.StatusCode == http.StatusBadRequest {
err := &errortypes.BadInput{
Message: "Unexpected http status code: 400",
}
return nil, []error{err}
}
if responseData.StatusCode != http.StatusOK {
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected http status code: %d", responseData.StatusCode),
}
return nil, []error{err}
}
var response openrtb2.BidResponse
if err := json.Unmarshal(responseData.Body, &response); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
bidResponse.Currency = response.Cur
for _, seatBid := range response.SeatBid {
for _, bid := range seatBid.Bid {
bid := bid
var bidType openrtb_ext.BidType
for _, imp := range request.Imp {
if imp.ID == bid.ImpID {
if imp.Banner != nil {
bidType = openrtb_ext.BidTypeBanner
}
}
}
b := &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, nil
}