-
Notifications
You must be signed in to change notification settings - Fork 715
/
dmx.go
360 lines (313 loc) · 8.78 KB
/
dmx.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package dmx
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/prebid/openrtb/v20/adcom1"
"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 DmxAdapter struct {
endpoint string
}
// Builder builds a new instance of the DistrictM DMX 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 := &DmxAdapter{
endpoint: config.Endpoint,
}
return bidder, nil
}
type dmxExt struct {
Bidder dmxParams `json:"bidder"`
}
type dmxPubExt struct {
Dmx dmxPubExtId `json:"dmx,omitempty"`
}
type dmxPubExtId struct {
Id string `json:"id,omitempty"`
}
type dmxParams struct {
TagId string `json:"tagid,omitempty"`
DmxId string `json:"dmxid,omitempty"`
MemberId string `json:"memberid,omitempty"`
PublisherId string `json:"publisher_id,omitempty"`
SellerId string `json:"seller_id,omitempty"`
Bidfloor float64 `json:"bidfloor,omitempty"`
}
var protocols = []adcom1.MediaCreativeSubtype{2, 3, 5, 6, 7, 8}
func UserSellerOrPubId(str1, str2 string) string {
if str1 != "" {
return str1
}
return str2
}
func (adapter *DmxAdapter) MakeRequests(request *openrtb2.BidRequest, req *adapters.ExtraRequestInfo) (reqsBidder []*adapters.RequestData, errs []error) {
var imps []openrtb2.Imp
var rootExtInfo dmxExt
var publisherId string
var sellerId string
var userExt openrtb_ext.ExtUser
var anyHasId = false
var reqCopy openrtb2.BidRequest = *request
var dmxReq *openrtb2.BidRequest = &reqCopy
var dmxRawPubId dmxPubExt
if request.User == nil {
if request.App == nil {
return nil, []error{errors.New("No user id or app id found. Could not send request to DMX.")}
}
}
if len(request.Imp) >= 1 {
err := json.Unmarshal(request.Imp[0].Ext, &rootExtInfo)
if err != nil {
errs = append(errs, err)
} else {
publisherId = UserSellerOrPubId(rootExtInfo.Bidder.PublisherId, rootExtInfo.Bidder.MemberId)
sellerId = rootExtInfo.Bidder.SellerId
}
}
if request.App != nil {
appCopy := *request.App
appPublisherCopy := *request.App.Publisher
dmxReq.App = &appCopy
dmxReq.App.Publisher = &appPublisherCopy
if dmxReq.App.Publisher.ID == "" {
dmxReq.App.Publisher.ID = publisherId
}
dmxRawPubId.Dmx.Id = UserSellerOrPubId(rootExtInfo.Bidder.PublisherId, rootExtInfo.Bidder.MemberId)
ext, err := json.Marshal(dmxRawPubId)
if err != nil {
errs = append(errs, fmt.Errorf("unable to marshal ext, %v", err))
return nil, errs
}
dmxReq.App.Publisher.Ext = ext
if dmxReq.App.ID != "" {
anyHasId = true
}
if anyHasId == false {
if idfa, valid := getIdfa(request); valid {
dmxReq.App.ID = idfa
anyHasId = true
}
}
} else {
dmxReq.App = nil
}
if request.Site != nil {
siteCopy := *request.Site
sitePublisherCopy := *request.Site.Publisher
dmxReq.Site = &siteCopy
dmxReq.Site.Publisher = &sitePublisherCopy
if dmxReq.Site.Publisher != nil {
if dmxReq.Site.Publisher.ID == "" {
dmxReq.Site.Publisher.ID = publisherId
}
dmxRawPubId.Dmx.Id = UserSellerOrPubId(rootExtInfo.Bidder.PublisherId, rootExtInfo.Bidder.MemberId)
ext, err := json.Marshal(dmxRawPubId)
if err != nil {
errs = append(errs, fmt.Errorf("unable to marshal ext, %v", err))
return nil, errs
}
dmxReq.Site.Publisher.Ext = ext
} else {
dmxReq.Site.Publisher = &openrtb2.Publisher{ID: publisherId}
}
} else {
dmxReq.Site = nil
}
if request.User != nil {
userCopy := *request.User
dmxReq.User = &userCopy
} else {
dmxReq.User = nil
}
if dmxReq.User != nil {
if dmxReq.User.ID != "" {
anyHasId = true
}
if dmxReq.User.Ext != nil {
if err := json.Unmarshal(dmxReq.User.Ext, &userExt); err == nil {
if len(userExt.Eids) > 0 {
anyHasId = true
}
}
}
}
for _, inst := range dmxReq.Imp {
var ins openrtb2.Imp
var params dmxExt
const intVal int8 = 1
source := (*json.RawMessage)(&inst.Ext)
if err := json.Unmarshal(*source, ¶ms); err != nil {
errs = append(errs, err)
}
if isDmxParams(params.Bidder) {
if inst.Banner != nil {
if len(inst.Banner.Format) != 0 {
bannerCopy := *inst.Banner
if params.Bidder.PublisherId != "" || params.Bidder.MemberId != "" {
imps = fetchParams(params, inst, ins, imps, &bannerCopy, nil, intVal)
} else {
return nil, []error{errors.New("Missing Params for auction to be send")}
}
}
}
if inst.Video != nil {
videoCopy := *inst.Video
if params.Bidder.PublisherId != "" || params.Bidder.MemberId != "" {
imps = fetchParams(params, inst, ins, imps, nil, &videoCopy, intVal)
} else {
return nil, []error{errors.New("Missing Params for auction to be send")}
}
}
}
}
dmxReq.Imp = imps
if anyHasId == false {
return nil, []error{errors.New("This request contained no identifier")}
}
oJson, err := json.Marshal(dmxReq)
if err != nil {
errs = append(errs, err)
return nil, errs
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
reqBidder := &adapters.RequestData{
Method: "POST",
Uri: adapter.endpoint + addParams(sellerId), //adapter.endpoint,
Body: oJson,
Headers: headers,
}
reqsBidder = append(reqsBidder, reqBidder)
return
}
func (adapter *DmxAdapter) MakeBids(request *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var errs []error
if http.StatusNoContent == response.StatusCode {
return nil, nil
}
if http.StatusBadRequest == response.StatusCode {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code 400"),
}}
}
if http.StatusOK != response.StatusCode {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected response no status code"),
}}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, request.Imp)
if err != nil {
errs = append(errs, err)
} else {
b := &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
}
if b.BidType == openrtb_ext.BidTypeVideo {
b.Bid.AdM = videoImpInsertion(b.Bid)
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
}
return bidResponse, errs
}
func fetchParams(params dmxExt, inst openrtb2.Imp, ins openrtb2.Imp, imps []openrtb2.Imp, banner *openrtb2.Banner, video *openrtb2.Video, intVal int8) []openrtb2.Imp {
var tempimp openrtb2.Imp
tempimp = inst
if params.Bidder.Bidfloor != 0 {
tempimp.BidFloor = params.Bidder.Bidfloor
}
if params.Bidder.TagId != "" {
tempimp.TagID = params.Bidder.TagId
tempimp.Secure = &intVal
}
if params.Bidder.DmxId != "" {
tempimp.TagID = params.Bidder.DmxId
tempimp.Secure = &intVal
}
if banner != nil {
if banner.H == nil || banner.W == nil {
banner.H = &banner.Format[0].H
banner.W = &banner.Format[0].W
}
tempimp.Banner = banner
}
if video != nil {
video.Protocols = checkProtocols(video)
tempimp.Video = video
}
if tempimp.TagID == "" {
return imps
}
imps = append(imps, tempimp)
return imps
}
func addParams(str string) string {
if str != "" {
return "?sellerid=" + url.QueryEscape(str)
}
return ""
}
func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner == nil && imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
}
return mediaType, nil
}
}
// This shouldnt happen. Lets handle it just incase by returning an error.
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find impression \"%s\" ", impID),
}
}
func videoImpInsertion(bid *openrtb2.Bid) string {
adm := bid.AdM
nurl := bid.NURL
search := "</Impression>"
imp := "</Impression><Impression><![CDATA[%s]]></Impression>"
wrapped_nurl := fmt.Sprintf(imp, nurl)
results := strings.Replace(adm, search, wrapped_nurl, 1)
return results
}
func isDmxParams(t interface{}) bool {
switch t.(type) {
case dmxParams:
return true
default:
return false
}
}
func getIdfa(request *openrtb2.BidRequest) (string, bool) {
if request.Device == nil {
return "", false
}
device := request.Device
if device.IFA != "" {
return device.IFA, true
}
return "", false
}
func checkProtocols(imp *openrtb2.Video) []adcom1.MediaCreativeSubtype {
if len(imp.Protocols) > 0 {
return imp.Protocols
}
return protocols
}