-
Notifications
You must be signed in to change notification settings - Fork 715
/
gamma.go
306 lines (283 loc) · 8.7 KB
/
gamma.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
package gamma
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/openrtb/v20/openrtb3"
"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 GammaAdapter struct {
URI string
}
type gammaBid struct {
openrtb2.Bid //base
VastXML string `json:"vastXml,omitempty"`
VastURL string `json:"vastUrl,omitempty"`
}
type gammaSeatBid struct {
Bid []gammaBid `json:"bid"`
Group int8 `json:"group,omitempty"`
Ext json.RawMessage `json:"ext,omitempty"`
}
type gammaBidResponse struct {
ID string `json:"id"`
SeatBid []gammaSeatBid `json:"seatbid,omitempty"`
BidID string `json:"bidid,omitempty"`
Cur string `json:"cur,omitempty"`
CustomData string `json:"customdata,omitempty"`
NBR *openrtb3.NoBidReason `json:"nbr,omitempty"`
Ext json.RawMessage `json:"ext,omitempty"`
}
func checkParams(gammaExt openrtb_ext.ExtImpGamma) error {
if gammaExt.PartnerID == "" {
return &errortypes.BadInput{
Message: "PartnerID is empty",
}
}
if gammaExt.ZoneID == "" {
return &errortypes.BadInput{
Message: "ZoneID is empty",
}
}
if gammaExt.WebID == "" {
return &errortypes.BadInput{
Message: "WebID is empty",
}
}
return nil
}
func (a *GammaAdapter) makeRequest(request *openrtb2.BidRequest, imp openrtb2.Imp) (*adapters.RequestData, []error) {
var errors []error
var bidderExt adapters.ExtImpBidder
err := json.Unmarshal(imp.Ext, &bidderExt)
if err != nil {
err = &errortypes.BadInput{
Message: "ext.bidder not provided",
}
errors = append(errors, err)
return nil, errors
}
var gammaExt openrtb_ext.ExtImpGamma
err = json.Unmarshal(bidderExt.Bidder, &gammaExt)
if err != nil {
err = &errortypes.BadInput{
Message: "ext.bidder.publisher not provided",
}
errors = append(errors, err)
return nil, errors
}
err = checkParams(gammaExt)
if err != nil {
errors = append(errors, err)
return nil, errors
}
thisURI := a.URI
thisURI = thisURI + "?id=" + gammaExt.PartnerID
thisURI = thisURI + "&zid=" + gammaExt.ZoneID
thisURI = thisURI + "&wid=" + gammaExt.WebID
thisURI = thisURI + "&bidid=" + imp.ID
thisURI = thisURI + "&hb=pbmobile"
if request.Device != nil {
if request.Device.IP != "" {
thisURI = thisURI + "&device_ip=" + request.Device.IP
}
if request.Device.Model != "" {
thisURI = thisURI + "&device_model=" + request.Device.Model
}
if request.Device.OS != "" {
thisURI = thisURI + "&device_os=" + request.Device.OS
}
if request.Device.UA != "" {
thisURI = thisURI + "&device_ua=" + url.QueryEscape(request.Device.UA)
}
if request.Device.IFA != "" {
thisURI = thisURI + "&device_ifa=" + request.Device.IFA
}
}
if request.App != nil {
if request.App.ID != "" {
thisURI = thisURI + "&app_id=" + request.App.ID
}
if request.App.Bundle != "" {
thisURI = thisURI + "&app_bundle=" + request.App.Bundle
}
if request.App.Name != "" {
thisURI = thisURI + "&app_name=" + request.App.Name
}
}
headers := http.Header{}
headers.Add("Accept", "*/*")
headers.Add("x-openrtb-version", "2.5")
if request.Device != nil {
addHeaderIfNonEmpty(headers, "User-Agent", request.Device.UA)
addHeaderIfNonEmpty(headers, "X-Forwarded-For", request.Device.IP)
addHeaderIfNonEmpty(headers, "Accept-Language", request.Device.Language)
if request.Device.DNT != nil {
addHeaderIfNonEmpty(headers, "DNT", strconv.Itoa(int(*request.Device.DNT)))
}
}
headers.Add("Connection", "keep-alive")
headers.Add("cache-control", "no-cache")
headers.Add("Accept-Encoding", "gzip, deflate")
return &adapters.RequestData{
Method: "GET",
Uri: thisURI,
Headers: headers,
ImpIDs: []string{imp.ID},
}, errors
}
func (a *GammaAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
errs := make([]error, 0, len(request.Imp))
if len(request.Imp) == 0 {
err := &errortypes.BadInput{
Message: "No impressions in the bid request",
}
errs = append(errs, err)
return nil, errs
}
var invalidImpIndex []int
for i := 0; i < len(request.Imp); i++ {
if request.Imp[i].Banner != nil {
bannerCopy := *request.Imp[i].Banner
if bannerCopy.W == nil && bannerCopy.H == nil && len(bannerCopy.Format) > 0 {
firstFormat := bannerCopy.Format[0]
bannerCopy.W = &(firstFormat.W)
bannerCopy.H = &(firstFormat.H)
}
request.Imp[i].Banner = &bannerCopy
} else if request.Imp[i].Video == nil {
err := &errortypes.BadInput{
Message: fmt.Sprintf("Gamma only supports banner and video media types. Ignoring imp id=%s", request.Imp[i].ID),
}
errs = append(errs, err)
invalidImpIndex = append(invalidImpIndex, i)
}
}
var adapterRequests []*adapters.RequestData
if len(invalidImpIndex) == 0 {
for _, imp := range request.Imp {
adapterReq, errors := a.makeRequest(request, imp)
if adapterReq != nil {
adapterRequests = append(adapterRequests, adapterReq)
}
errs = append(errs, errors...)
}
} else if len(request.Imp) == len(invalidImpIndex) {
//only true if every Imp was not a Banner or a Video
err := &errortypes.BadInput{
Message: "No valid impression in the bid request",
}
errs = append(errs, err)
return nil, errs
} else {
var j int = 0
for i := 0; i < len(request.Imp); i++ {
if j < len(invalidImpIndex) && i == invalidImpIndex[j] {
j++
} else {
adapterReq, errors := a.makeRequest(request, request.Imp[i])
if adapterReq != nil {
adapterRequests = append(adapterRequests, adapterReq)
}
errs = append(errs, errors...)
}
}
}
return adapterRequests, errs
}
func convertBid(gBid gammaBid, mediaType openrtb_ext.BidType) *openrtb2.Bid {
bid := gBid.Bid
if mediaType == openrtb_ext.BidTypeVideo {
//Return inline VAST XML Document (Section 6.4.2)
if len(gBid.VastXML) > 0 {
if len(gBid.VastURL) > 0 {
bid.NURL = gBid.VastURL
}
bid.AdM = gBid.VastXML
} else {
return nil
}
} else {
if len(gBid.Bid.AdM) == 0 {
return nil
}
}
return &bid
}
func (a *GammaAdapter) 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.BadServerResponse{
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 gammaResp gammaBidResponse
if err := json.Unmarshal(response.Body, &gammaResp); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("bad server response: %d. ", err),
}}
}
//(Section 7.1 No-Bid Signaling)
if len(gammaResp.SeatBid) == 0 {
return nil, nil
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(gammaResp.SeatBid[0].Bid))
errs := make([]error, 0, len(gammaResp.SeatBid[0].Bid))
for _, sb := range gammaResp.SeatBid {
for i := range sb.Bid {
mediaType := getMediaTypeForImp(gammaResp.ID, internalRequest.Imp)
bid := convertBid(sb.Bid[i], mediaType)
if bid != nil {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: bid,
BidType: mediaType,
})
} else {
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf("Missing Ad Markup. Run with request.debug = 1 for more info"),
}
errs = append(errs, err)
}
}
}
return bidResponse, errs
}
// Adding header fields to request header
func addHeaderIfNonEmpty(headers http.Header, headerName string, headerValue string) {
if len(headerValue) > 0 {
headers.Add(headerName, headerValue)
}
}
// getMediaTypeForImp figures out which media type this bid is for.
func getMediaTypeForImp(impId string, imps []openrtb2.Imp) openrtb_ext.BidType {
mediaType := openrtb_ext.BidTypeBanner //default type
for _, imp := range imps {
if imp.ID == impId {
if imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
}
return mediaType
}
}
return mediaType
}
// Builder builds a new instance of the Gamma 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 := &GammaAdapter{
URI: config.Endpoint,
}
return bidder, nil
}