-
Notifications
You must be signed in to change notification settings - Fork 763
/
Copy pathlunamedia.go
241 lines (215 loc) · 7.5 KB
/
lunamedia.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
package lunamedia
import (
"encoding/json"
"fmt"
"net/http"
"text/template"
"github.com/prebid/openrtb/v19/openrtb2"
"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/macros"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)
type LunaMediaAdapter struct {
EndpointTemplate *template.Template
}
// MakeRequests prepares request information for prebid-server core
func (adapter *LunaMediaAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
errs := make([]error, 0, len(request.Imp))
if len(request.Imp) == 0 {
errs = append(errs, &errortypes.BadInput{Message: "No impression in the bid request"})
return nil, errs
}
pub2impressions, imps, err := getImpressionsInfo(request.Imp)
if len(imps) == 0 {
return nil, err
}
errs = append(errs, err...)
if len(pub2impressions) == 0 {
return nil, errs
}
result := make([]*adapters.RequestData, 0, len(pub2impressions))
for k, imps := range pub2impressions {
bidRequest, err := adapter.buildAdapterRequest(request, &k, imps)
if err != nil {
errs = append(errs, err)
return nil, errs
} else {
result = append(result, bidRequest)
}
}
return result, errs
}
// getImpressionsInfo checks each impression for validity and returns impressions copy with corresponding exts
func getImpressionsInfo(imps []openrtb2.Imp) (map[openrtb_ext.ExtImpLunaMedia][]openrtb2.Imp, []openrtb2.Imp, []error) {
errors := make([]error, 0, len(imps))
resImps := make([]openrtb2.Imp, 0, len(imps))
res := make(map[openrtb_ext.ExtImpLunaMedia][]openrtb2.Imp)
for _, imp := range imps {
impExt, err := getImpressionExt(&imp)
if err != nil {
errors = append(errors, err)
continue
}
if err := validateImpression(impExt); err != nil {
errors = append(errors, err)
continue
}
//dispatchImpressions
//Group impressions by LunaMedia-specific parameters `pubid
if err := compatImpression(&imp); err != nil {
errors = append(errors, err)
continue
}
if res[*impExt] == nil {
res[*impExt] = make([]openrtb2.Imp, 0)
}
res[*impExt] = append(res[*impExt], imp)
resImps = append(resImps, imp)
}
return res, resImps, errors
}
func validateImpression(impExt *openrtb_ext.ExtImpLunaMedia) error {
if impExt.PublisherID == "" {
return &errortypes.BadInput{Message: "No pubid value provided"}
}
return nil
}
// Alter impression info to comply with LunaMedia platform requirements
func compatImpression(imp *openrtb2.Imp) error {
imp.Ext = nil //do not forward ext to LunaMedia platform
if imp.Banner != nil {
return compatBannerImpression(imp)
}
return nil
}
func compatBannerImpression(imp *openrtb2.Imp) error {
// Create a copy of the banner, since imp is a shallow copy of the original.
bannerCopy := *imp.Banner
banner := &bannerCopy
//As banner.w/h are required fields for LunaMedia platform - take the first format entry
if banner.W == nil || banner.H == nil {
if len(banner.Format) == 0 {
return &errortypes.BadInput{Message: "Expected at least one banner.format entry or explicit w/h"}
}
format := banner.Format[0]
banner.Format = banner.Format[1:]
banner.W = &format.W
banner.H = &format.H
imp.Banner = banner
}
return nil
}
func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtImpLunaMedia, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: err.Error(),
}
}
var LunaMediaExt openrtb_ext.ExtImpLunaMedia
if err := json.Unmarshal(bidderExt.Bidder, &LunaMediaExt); err != nil {
return nil, &errortypes.BadInput{
Message: err.Error(),
}
}
return &LunaMediaExt, nil
}
func (adapter *LunaMediaAdapter) buildAdapterRequest(prebidBidRequest *openrtb2.BidRequest, params *openrtb_ext.ExtImpLunaMedia, imps []openrtb2.Imp) (*adapters.RequestData, error) {
newBidRequest := createBidRequest(prebidBidRequest, params, imps)
reqJSON, err := json.Marshal(newBidRequest)
if err != nil {
return nil, 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")
url, err := adapter.buildEndpointURL(params)
if err != nil {
return nil, err
}
return &adapters.RequestData{
Method: "POST",
Uri: url,
Body: reqJSON,
Headers: headers}, nil
}
func createBidRequest(prebidBidRequest *openrtb2.BidRequest, params *openrtb_ext.ExtImpLunaMedia, imps []openrtb2.Imp) *openrtb2.BidRequest {
bidRequest := *prebidBidRequest
bidRequest.Imp = imps
for idx := range bidRequest.Imp {
imp := &bidRequest.Imp[idx]
imp.TagID = params.Placement
}
if bidRequest.Site != nil {
// Need to copy Site as Request is a shallow copy
siteCopy := *bidRequest.Site
bidRequest.Site = &siteCopy
bidRequest.Site.Publisher = nil
bidRequest.Site.Domain = ""
}
if bidRequest.App != nil {
// Need to copy App as Request is a shallow copy
appCopy := *bidRequest.App
bidRequest.App = &appCopy
bidRequest.App.Publisher = nil
}
return &bidRequest
}
// Builds enpoint url based on adapter-specific pub settings from imp.ext
func (adapter *LunaMediaAdapter) buildEndpointURL(params *openrtb_ext.ExtImpLunaMedia) (string, error) {
endpointParams := macros.EndpointTemplateParams{PublisherID: params.PublisherID}
return macros.ResolveMacros(adapter.EndpointTemplate, endpointParams)
}
// MakeBids translates LunaMedia bid response to prebid-server specific format
func (adapter *LunaMediaAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var msg = ""
if response.StatusCode == http.StatusNoContent {
return nil, nil
}
if response.StatusCode != http.StatusOK {
msg = fmt.Sprintf("Unexpected http status code: %d", response.StatusCode)
return nil, []error{&errortypes.BadServerResponse{Message: msg}}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
msg = fmt.Sprintf("Bad server response: %d", err)
return nil, []error{&errortypes.BadServerResponse{Message: msg}}
}
if len(bidResp.SeatBid) != 1 {
var msg = fmt.Sprintf("Invalid SeatBids count: %d", len(bidResp.SeatBid))
return nil, []error{&errortypes.BadServerResponse{Message: msg}}
}
seatBid := bidResp.SeatBid[0]
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid[0].Bid))
for i := 0; i < len(seatBid.Bid); i++ {
bid := seatBid.Bid[i]
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: getMediaTypeForImpID(bid.ImpID, internalRequest.Imp),
})
}
return bidResponse, nil
}
// getMediaTypeForImp figures out which media type this bid is for
func getMediaTypeForImpID(impID string, imps []openrtb2.Imp) openrtb_ext.BidType {
for _, imp := range imps {
if imp.ID == impID && imp.Video != nil {
return openrtb_ext.BidTypeVideo
}
}
return openrtb_ext.BidTypeBanner
}
// Builder builds a new instance of the LunaMedia adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
urlTemplate, err := template.New("endpointTemplate").Parse(config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
}
bidder := &LunaMediaAdapter{
EndpointTemplate: urlTemplate,
}
return bidder, nil
}