-
Notifications
You must be signed in to change notification settings - Fork 700
/
outbrain.go
182 lines (159 loc) · 4.99 KB
/
outbrain.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
package outbrain
import (
"encoding/json"
"fmt"
"net/http"
"github.com/mxmCherry/openrtb/v15/native1"
nativeResponse "github.com/mxmCherry/openrtb/v15/native1/response"
"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/openrtb_ext"
)
type adapter struct {
endpoint string
}
// Builder builds a new instance of the Outbrain adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &adapter{
endpoint: config.Endpoint,
}
return bidder, nil
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
reqCopy := *request
var errs []error
var outbrainExt openrtb_ext.ExtImpOutbrain
for i := 0; i < len(reqCopy.Imp); i++ {
imp := reqCopy.Imp[i]
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
errs = append(errs, err)
continue
}
if err := json.Unmarshal(bidderExt.Bidder, &outbrainExt); err != nil {
errs = append(errs, err)
continue
}
if outbrainExt.TagId != "" {
imp.TagID = outbrainExt.TagId
reqCopy.Imp[i] = imp
}
}
publisher := &openrtb2.Publisher{
ID: outbrainExt.Publisher.Id,
Name: outbrainExt.Publisher.Name,
Domain: outbrainExt.Publisher.Domain,
}
if reqCopy.Site != nil {
siteCopy := *reqCopy.Site
siteCopy.Publisher = publisher
reqCopy.Site = &siteCopy
} else if reqCopy.App != nil {
appCopy := *reqCopy.App
appCopy.Publisher = publisher
reqCopy.App = &appCopy
}
if outbrainExt.BCat != nil {
reqCopy.BCat = outbrainExt.BCat
}
if outbrainExt.BAdv != nil {
reqCopy.BAdv = outbrainExt.BAdv
}
requestJSON, err := json.Marshal(reqCopy)
if err != nil {
errs = append(errs, err)
return nil, errs
}
requestData := &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: requestJSON,
}
return []*adapters.RequestData{requestData}, nil
}
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 status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.",
}
return nil, []error{err}
}
if responseData.StatusCode != http.StatusOK {
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", 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
var errs []error
for _, seatBid := range response.SeatBid {
for i := range seatBid.Bid {
bid := seatBid.Bid[i]
bidType, err := getMediaTypeForImp(bid.ImpID, request.Imp)
if err != nil {
errs = append(errs, err)
continue
}
if bidType == openrtb_ext.BidTypeNative {
var nativePayload nativeResponse.Response
if err := json.Unmarshal(json.RawMessage(bid.AdM), &nativePayload); err != nil {
errs = append(errs, err)
continue
}
transformEventTrackers(&nativePayload)
nativePayloadJson, err := json.Marshal(nativePayload)
if err != nil {
errs = append(errs, err)
continue
}
bid.AdM = string(nativePayloadJson)
}
b := &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, errs
}
func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
for _, imp := range imps {
if imp.ID == impID {
if imp.Native != nil {
return openrtb_ext.BidTypeNative, nil
} else if imp.Banner != nil {
return openrtb_ext.BidTypeBanner, nil
}
}
}
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find native/banner impression \"%s\" ", impID),
}
}
func transformEventTrackers(nativePayload *nativeResponse.Response) {
// the native-trk.js library used to trigger the trackers currently doesn't support the native 1.2 eventtrackers,
// so transform them to the deprecated imptrackers and jstracker
for _, eventTracker := range nativePayload.EventTrackers {
if eventTracker.Event != native1.EventTypeImpression {
continue
}
switch eventTracker.Method {
case native1.EventTrackingMethodImage:
nativePayload.ImpTrackers = append(nativePayload.ImpTrackers, eventTracker.URL)
case native1.EventTrackingMethodJS:
nativePayload.JSTracker = fmt.Sprintf("<script src=\"%s\"></script>", eventTracker.URL)
}
}
nativePayload.EventTrackers = nil
}