-
Notifications
You must be signed in to change notification settings - Fork 739
/
sovrnXsp.go
172 lines (147 loc) · 4.27 KB
/
sovrnXsp.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
package sovrnXsp
import (
"encoding/json"
"fmt"
"net/http"
"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"
"github.com/prebid/openrtb/v20/openrtb2"
)
type adapter struct {
Endpoint string
}
// bidExt.CreativeType values.
const (
creativeTypeBanner int = 0
creativeTypeVideo = 1
creativeTypeNative = 2
creativeTypeAudio = 3
)
// Bid response extension from XSP.
type bidExt struct {
CreativeType int `json:"creative_type"`
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
appCopy := *request.App
if appCopy.Publisher == nil {
appCopy.Publisher = &openrtb2.Publisher{}
} else {
publisherCopy := *appCopy.Publisher
appCopy.Publisher = &publisherCopy
}
request.App = &appCopy
var errors []error
var imps []openrtb2.Imp
for idx, imp := range request.Imp {
if imp.Banner == nil && imp.Video == nil && imp.Native == nil {
continue
}
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
err = &errortypes.BadInput{
Message: fmt.Sprintf("imp #%d: ext.bidder not provided", idx),
}
errors = append(errors, err)
continue
}
var xspExt openrtb_ext.ExtImpSovrnXsp
if err := json.Unmarshal(bidderExt.Bidder, &xspExt); err != nil {
err = &errortypes.BadInput{
Message: fmt.Sprintf("imp #%d: %s", idx, err.Error()),
}
errors = append(errors, err)
continue
}
request.App.Publisher.ID = xspExt.PubID
if xspExt.MedID != "" {
request.App.ID = xspExt.MedID
}
if xspExt.ZoneID != "" {
imp.TagID = xspExt.ZoneID
}
imps = append(imps, imp)
}
if len(imps) == 0 {
return nil, append(errors, &errortypes.BadInput{
Message: "no matching impression with ad format",
})
}
request.Imp = imps
requestJson, err := json.Marshal(request)
if err != nil {
return nil, append(errors, err)
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
headers.Add("x-openrtb-version", "2.5")
return []*adapters.RequestData{{
Method: "POST",
Uri: a.Endpoint,
Body: requestJson,
Headers: headers,
}}, errors
}
func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if adapters.IsResponseStatusCodeNoContent(responseData) {
return nil, nil
}
if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
return nil, []error{err}
}
var response openrtb2.BidResponse
if err := json.Unmarshal(responseData.Body, &response); err != nil {
return nil, []error{err}
}
var errors []error
result := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
for _, seatBid := range response.SeatBid {
for _, bid := range seatBid.Bid {
bid := bid
var ext bidExt
if err := json.Unmarshal(bid.Ext, &ext); err != nil {
errors = append(errors, err)
continue
}
var bidType openrtb_ext.BidType
var mkupType openrtb2.MarkupType
switch ext.CreativeType {
case creativeTypeBanner:
bidType = openrtb_ext.BidTypeBanner
mkupType = openrtb2.MarkupBanner
case creativeTypeVideo:
bidType = openrtb_ext.BidTypeVideo
mkupType = openrtb2.MarkupVideo
case creativeTypeNative:
bidType = openrtb_ext.BidTypeNative
mkupType = openrtb2.MarkupNative
default:
errors = append(errors, &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unsupported creative type: %d", ext.CreativeType),
})
continue
}
if bid.MType == 0 {
bid.MType = mkupType
}
result.Bids = append(result.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
})
}
}
if len(result.Bids) == 0 {
// it's possible an empty seat array was sent as a response
return nil, errors
}
return result, errors
}
// Builder builds a new instance of the SovrnXSP 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 := &adapter{
Endpoint: config.Endpoint,
}
return bidder, nil
}