/
invibes.go
340 lines (300 loc) · 9.57 KB
/
invibes.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package invibes
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"text/template"
"github.com/mxmCherry/openrtb/v15/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/macros"
"github.com/prebid/prebid-server/metrics"
"github.com/prebid/prebid-server/openrtb_ext"
)
const adapterVersion = "prebid_1.0.0"
const invibesBidVersion = "4"
type InvibesAdRequest struct {
BidParamsJson string `json:"BidParamsJson"`
Location string `json:"Location"`
Lid string `json:"Lid"`
IsTestBid bool `json:"IsTestBid"`
Kw string `json:"Kw"`
IsAMP bool `json:"IsAmp"`
Width string `json:"Width"`
Height string `json:"Height"`
GDPRConsent string `json:"GdprConsent"`
GDPR bool `json:"Gdpr"`
Bvid string `json:"Bvid"`
InvibBVLog bool `json:"InvibBVLog"`
VideoAdDebug bool `json:"VideoAdDebug"`
}
type InvibesBidParams struct {
PlacementIDs []string `json:"PlacementIds"`
BidVersion string `json:"BidVersion"`
Properties map[string]InvibesPlacementProperty `json:"Properties"`
}
type InvibesPlacementProperty struct {
Formats []openrtb2.Format `json:"Formats"`
ImpID string `json:"ImpId"`
}
type InvibesInternalParams struct {
BidParams InvibesBidParams
DomainID int
IsAMP bool
GDPR bool
GDPRConsent string
TestBvid string
TestLog bool
}
type BidServerBidderResponse struct {
Currency string `json:"currency"`
TypedBids []BidServerTypedBid `json:"typedBids"`
Error string `json:"error"`
}
type BidServerTypedBid struct {
Bid openrtb2.Bid `json:"bid"`
DealPriority int `json:"dealPriority"`
}
func (a *InvibesInternalParams) IsTestRequest() bool {
return a.TestBvid != ""
}
type InvibesAdapter struct {
EndpointTemplate *template.Template
}
// Builder builds a new instance of the Invibes adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
template, err := template.New("endpointTemplate").Parse(config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
}
bidder := InvibesAdapter{
EndpointTemplate: template,
}
return &bidder, nil
}
func (a *InvibesAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var httpRequests []*adapters.RequestData
var tempErrors []error
gdprApplies, consentString := readGDPR(request)
var invibesInternalParams InvibesInternalParams = InvibesInternalParams{
BidParams: InvibesBidParams{
Properties: make(map[string]InvibesPlacementProperty),
BidVersion: invibesBidVersion,
},
}
for _, imp := range request.Imp {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
tempErrors = append(tempErrors, &errortypes.BadInput{
Message: "Error parsing bidderExt object",
})
continue
}
var invibesExt openrtb_ext.ExtImpInvibes
if err := json.Unmarshal(bidderExt.Bidder, &invibesExt); err != nil {
tempErrors = append(tempErrors, &errortypes.BadInput{
Message: "Error parsing invibesExt parameters",
})
continue
}
if imp.Banner == nil {
tempErrors = append(tempErrors, &errortypes.BadInput{
Message: "Banner not specified",
})
continue
}
adFormats := readAdFormats(*imp.Banner)
invibesInternalParams.DomainID = invibesExt.DomainID
invibesInternalParams.BidParams.PlacementIDs = append(invibesInternalParams.BidParams.PlacementIDs, strings.TrimSpace(invibesExt.PlacementID))
invibesInternalParams.BidParams.Properties[invibesExt.PlacementID] = InvibesPlacementProperty{
ImpID: imp.ID,
Formats: adFormats,
}
if invibesExt.Debug.TestBvid != "" {
invibesInternalParams.TestBvid = invibesExt.Debug.TestBvid
}
invibesInternalParams.TestLog = invibesExt.Debug.TestLog
}
if reqInfo.PbsEntryPoint == metrics.ReqTypeAMP {
invibesInternalParams.IsAMP = true
}
if len(invibesInternalParams.BidParams.PlacementIDs) == 0 {
return nil, tempErrors
}
var finalErrors []error
invibesInternalParams.GDPR = gdprApplies
invibesInternalParams.GDPRConsent = consentString
newHttpRequest, err := a.makeRequest(invibesInternalParams, reqInfo, httpRequests, request)
if err != nil {
finalErrors = append(finalErrors, err)
} else if newHttpRequest != nil {
httpRequests = append(httpRequests, newHttpRequest)
}
return httpRequests, finalErrors
}
func readGDPR(request *openrtb2.BidRequest) (bool, string) {
consentString := ""
if request.User != nil {
var extUser openrtb_ext.ExtUser
if err := json.Unmarshal(request.User.Ext, &extUser); err == nil {
consentString = extUser.Consent
}
}
gdprApplies := true
var extRegs openrtb_ext.ExtRegs
if request.Regs != nil {
if err := json.Unmarshal(request.Regs.Ext, &extRegs); err == nil {
if extRegs.GDPR != nil {
gdprApplies = (*extRegs.GDPR == 1)
}
}
}
return gdprApplies, consentString
}
func readAdFormats(currentBanner openrtb2.Banner) []openrtb2.Format {
var adFormats []openrtb2.Format
if currentBanner.Format != nil {
adFormats = currentBanner.Format
} else if currentBanner.W != nil && currentBanner.H != nil {
adFormats = []openrtb2.Format{
{
W: *currentBanner.W,
H: *currentBanner.H,
},
}
}
return adFormats
}
func (a *InvibesAdapter) makeRequest(invibesParams InvibesInternalParams, reqInfo *adapters.ExtraRequestInfo, existingRequests []*adapters.RequestData, request *openrtb2.BidRequest) (*adapters.RequestData, error) {
url, err := a.makeURL(request, invibesParams.DomainID)
if err != nil {
return nil, err
}
parameter, errp := a.makeParameter(invibesParams, request)
if errp != nil {
return nil, errp
}
body, errm := json.Marshal(parameter)
if errm != nil {
return nil, errm
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
if request.Device != nil {
headers.Add("User-Agent", request.Device.UA)
}
if request.Device != nil {
if request.Device.IP != "" {
headers.Add("X-Forwarded-For", request.Device.IP)
} else if request.Device.IPv6 != "" {
headers.Add("X-Forwarded-For", request.Device.IPv6)
}
}
if request.Site != nil {
headers.Add("Referer", request.Site.Page)
}
headers.Add("Aver", adapterVersion)
return &adapters.RequestData{
Method: "POST",
Uri: url,
Headers: headers,
Body: body,
}, nil
}
func (a *InvibesAdapter) makeParameter(invibesParams InvibesInternalParams, request *openrtb2.BidRequest) (*InvibesAdRequest, error) {
var lid string = ""
if request.User != nil && request.User.BuyerUID != "" {
lid = request.User.BuyerUID
}
if request.Site == nil {
return nil, &errortypes.BadInput{
Message: "Site not specified",
}
}
var width, height string
if request.Device != nil {
if request.Device.W > 0 {
width = strconv.FormatInt(request.Device.W, 10)
}
if request.Device.H > 0 {
height = strconv.FormatInt(request.Device.H, 10)
}
}
var invRequest InvibesAdRequest
bidParamsJson, err := json.Marshal(invibesParams.BidParams)
if err == nil {
invRequest = InvibesAdRequest{
IsTestBid: invibesParams.IsTestRequest(),
BidParamsJson: string(bidParamsJson),
Location: request.Site.Page,
Lid: lid,
Kw: request.Site.Keywords,
IsAMP: invibesParams.IsAMP,
Width: width,
Height: height,
GDPRConsent: invibesParams.GDPRConsent,
GDPR: invibesParams.GDPR,
Bvid: invibesParams.TestBvid,
InvibBVLog: invibesParams.TestLog,
VideoAdDebug: invibesParams.TestLog,
}
}
return &invRequest, err
}
func (a *InvibesAdapter) makeURL(request *openrtb2.BidRequest, domainID int) (string, error) {
host := "bid.videostep.com"
if domainID == 1 {
host = "adweb.videostepstage.com"
} else if domainID == 2 {
host = "adweb.invibesstage.com"
} else if domainID == 1001 {
host = "bid.videostep.com"
} else if domainID >= 1002 {
host = "bid" + strconv.Itoa(domainID-1000) + ".videostep.com"
}
var endpointURL *url.URL
endpointParams := macros.EndpointTemplateParams{Host: host}
host, err := macros.ResolveMacros(a.EndpointTemplate, endpointParams)
if err == nil {
endpointURL, err = url.Parse(host)
}
if err != nil {
return "", &errortypes.BadInput{
Message: "Unable to parse url template: " + err.Error(),
}
}
return endpointURL.String(), nil
}
func (a *InvibesAdapter) MakeBids(
internalRequest *openrtb2.BidRequest,
externalRequest *adapters.RequestData,
response *adapters.ResponseData,
) (*adapters.BidderResponse, []error) {
if response.StatusCode != http.StatusOK {
return nil, []error{fmt.Errorf("Unexpected status code: %d.", response.StatusCode)}
}
bidResponse := BidServerBidderResponse{}
if err := json.Unmarshal(response.Body, &bidResponse); err != nil {
return nil, []error{err}
}
var parsedResponses = adapters.NewBidderResponseWithBidsCapacity(len(bidResponse.TypedBids))
var errors []error
parsedResponses.Currency = bidResponse.Currency
if bidResponse.Error != "" {
return nil, []error{fmt.Errorf("Server error: %s.", bidResponse.Error)}
}
for _, typedbid := range bidResponse.TypedBids {
bid := typedbid.Bid
parsedResponses.Bids = append(parsedResponses.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: openrtb_ext.BidTypeBanner,
DealPriority: typedbid.DealPriority,
})
}
return parsedResponses, errors
}