-
Notifications
You must be signed in to change notification settings - Fork 737
/
sovrn.go
159 lines (133 loc) · 4.06 KB
/
sovrn.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
package sovrn
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"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"
"github.com/mxmCherry/openrtb/v15/openrtb2"
)
type SovrnAdapter struct {
URI string
}
func (s *SovrnAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
errs := make([]error, 0, len(request.Imp))
for i := 0; i < len(request.Imp); i++ {
_, err := preprocess(&request.Imp[i])
if err != nil {
errs = append(errs, err)
request.Imp = append(request.Imp[:i], request.Imp[i+1:]...)
i--
}
}
// If all the requests were malformed, don't bother making a server call with no impressions.
if len(request.Imp) == 0 {
return nil, errs
}
reqJSON, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}
headers := http.Header{}
headers.Add("Content-Type", "application/json")
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)))
}
}
if request.User != nil {
userID := strings.TrimSpace(request.User.BuyerUID)
if len(userID) > 0 {
headers.Add("Cookie", fmt.Sprintf("%s=%s", "ljt_reader", userID))
}
}
return []*adapters.RequestData{{
Method: "POST",
Uri: s.URI,
Body: reqJSON,
Headers: headers,
}}, errs
}
func addHeaderIfNonEmpty(headers http.Header, headerName string, headerValue string) {
if len(headerValue) > 0 {
headers.Add(headerName, headerValue)
}
}
func (s *SovrnAdapter) 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{&errortypes.BadServerResponse{
Message: err.Error(),
}}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
for _, sb := range bidResp.SeatBid {
for i := 0; i < len(sb.Bid); i++ {
bid := sb.Bid[i]
adm, err := url.QueryUnescape(bid.AdM)
if err == nil {
bid.AdM = adm
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: openrtb_ext.BidTypeBanner,
})
}
}
}
return bidResponse, nil
}
func preprocess(imp *openrtb2.Imp) (string, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return "", &errortypes.BadInput{
Message: err.Error(),
}
}
var sovrnExt openrtb_ext.ExtImpSovrn
if err := json.Unmarshal(bidderExt.Bidder, &sovrnExt); err != nil {
return "", &errortypes.BadInput{
Message: err.Error(),
}
}
imp.TagID = getTagid(sovrnExt)
if imp.BidFloor == 0 && sovrnExt.BidFloor > 0 {
imp.BidFloor = sovrnExt.BidFloor
}
return imp.TagID, nil
}
func getTagid(sovrnExt openrtb_ext.ExtImpSovrn) string {
if len(sovrnExt.Tagid) > 0 {
return sovrnExt.Tagid
} else {
return sovrnExt.TagId
}
}
// Builder builds a new instance of the Sovrn adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &SovrnAdapter{
URI: config.Endpoint,
}
return bidder, nil
}