-
Notifications
You must be signed in to change notification settings - Fork 749
/
33across.go
286 lines (229 loc) · 6.92 KB
/
33across.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
package ttx
import (
"encoding/json"
"fmt"
"net/http"
"github.com/mxmCherry/openrtb/v16/adcom1"
"github.com/mxmCherry/openrtb/v16/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)
type TtxAdapter struct {
endpoint string
}
type Ext struct {
Ttx impTtxExt `json:"ttx"`
}
type impTtxExt struct {
Prod string `json:"prod"`
Zoneid string `json:"zoneid,omitempty"`
}
type reqExt struct {
Ttx *reqTtxExt `json:"ttx,omitempty"`
}
type reqTtxExt struct {
Caller []TtxCaller `json:"caller,omitempty"`
}
type TtxCaller struct {
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
}
// CALLER Info used to track Prebid Server
// as one of the hops in the request to exchange
var CALLER = TtxCaller{"Prebid-Server", "n/a"}
type bidExt struct {
Ttx bidTtxExt `json:"ttx,omitempty"`
}
type bidTtxExt struct {
MediaType string `json:mediaType,omitempty`
}
// MakeRequests create the object for TTX Reqeust.
func (a *TtxAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errs []error
var adapterRequests []*adapters.RequestData
var groupedImps = make(map[string][]openrtb2.Imp)
// Construct request extension common to all imps
// NOTE: not blocking adapter requests on errors
// since request extension is optional.
reqExt, err := makeReqExt(request)
if err != nil {
errs = append(errs, err)
}
request.Ext = reqExt
// We only support SRA for requests containing same prod and
// zoneID, therefore group all imps accordingly and create a http
// request for each such group
for i := 0; i < len(request.Imp); i++ {
if impCopy, err := makeImps(request.Imp[i]); err == nil {
var impExt Ext
// Skip over imps whose extensions cannot be read since
// we cannot glean Prod or ZoneID which are required to
// group together. However let's not block request creation.
if err := json.Unmarshal(impCopy.Ext, &impExt); err == nil {
impKey := impExt.Ttx.Prod + impExt.Ttx.Zoneid
groupedImps[impKey] = append(groupedImps[impKey], impCopy)
} else {
errs = append(errs, err)
}
} else {
errs = append(errs, err)
}
}
for _, impList := range groupedImps {
if adapterReq, err := a.makeRequest(*request, impList); err == nil {
adapterRequests = append(adapterRequests, adapterReq)
} else {
errs = append(errs, err)
}
}
return adapterRequests, errs
}
func (a *TtxAdapter) makeRequest(request openrtb2.BidRequest, impList []openrtb2.Imp) (*adapters.RequestData, error) {
request.Imp = impList
// Last Step
reqJSON, err := json.Marshal(request)
if err != nil {
return nil, err
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
return &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: reqJSON,
Headers: headers,
}, nil
}
func makeImps(imp openrtb2.Imp) (openrtb2.Imp, error) {
if imp.Banner == nil && imp.Video == nil {
return openrtb2.Imp{}, &errortypes.BadInput{
Message: fmt.Sprintf("Imp ID %s must have at least one of [Banner, Video] defined", imp.ID),
}
}
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return openrtb2.Imp{}, &errortypes.BadInput{
Message: err.Error(),
}
}
var ttxExt openrtb_ext.ExtImp33across
if err := json.Unmarshal(bidderExt.Bidder, &ttxExt); err != nil {
return openrtb2.Imp{}, &errortypes.BadInput{
Message: err.Error(),
}
}
var impExt Ext
impExt.Ttx.Prod = ttxExt.ProductId
impExt.Ttx.Zoneid = ttxExt.SiteId
if len(ttxExt.ZoneId) > 0 {
impExt.Ttx.Zoneid = ttxExt.ZoneId
}
impExtJSON, err := json.Marshal(impExt)
if err != nil {
return openrtb2.Imp{}, &errortypes.BadInput{
Message: err.Error(),
}
}
imp.Ext = impExtJSON
// Validate Video if it exists
if imp.Video != nil {
videoCopy, err := validateVideoParams(imp.Video, impExt.Ttx.Prod)
imp.Video = videoCopy
if err != nil {
return openrtb2.Imp{}, &errortypes.BadInput{
Message: err.Error(),
}
}
}
return imp, nil
}
func makeReqExt(request *openrtb2.BidRequest) ([]byte, error) {
var reqExt reqExt
if len(request.Ext) > 0 {
if err := json.Unmarshal(request.Ext, &reqExt); err != nil {
return nil, err
}
}
if reqExt.Ttx == nil {
reqExt.Ttx = &reqTtxExt{}
}
if reqExt.Ttx.Caller == nil {
reqExt.Ttx.Caller = make([]TtxCaller, 0)
}
reqExt.Ttx.Caller = append(reqExt.Ttx.Caller, CALLER)
return json.Marshal(reqExt)
}
// MakeBids make the bids for the bid response.
func (a *TtxAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if response.StatusCode == http.StatusNoContent {
return nil, nil
}
if response.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}
if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
var bidExt bidExt
var bidType openrtb_ext.BidType
if err := json.Unmarshal(sb.Bid[i].Ext, &bidExt); err != nil {
bidType = openrtb_ext.BidTypeBanner
} else {
bidType = getBidType(bidExt)
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
})
}
}
return bidResponse, nil
}
func validateVideoParams(video *openrtb2.Video, prod string) (*openrtb2.Video, error) {
videoCopy := *video
if videoCopy.W == 0 ||
videoCopy.H == 0 ||
videoCopy.Protocols == nil ||
videoCopy.MIMEs == nil ||
videoCopy.PlaybackMethod == nil {
return nil, &errortypes.BadInput{
Message: "One or more invalid or missing video field(s) w, h, protocols, mimes, playbackmethod",
}
}
if videoCopy.Placement == 0 {
videoCopy.Placement = 2
}
if prod == "instream" {
videoCopy.Placement = 1
if videoCopy.StartDelay == nil {
videoCopy.StartDelay = adcom1.StartDelay.Ptr(0)
}
}
return &videoCopy, nil
}
func getBidType(ext bidExt) openrtb_ext.BidType {
if ext.Ttx.MediaType == "video" {
return openrtb_ext.BidTypeVideo
}
return openrtb_ext.BidTypeBanner
}
// Builder builds a new instance of the 33Across adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &TtxAdapter{
endpoint: config.Endpoint,
}
return bidder, nil
}