forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 5
/
consumable.go
164 lines (146 loc) · 4.45 KB
/
consumable.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
package consumable
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
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errs []error
headers := http.Header{
"Content-Type": {"application/json"},
"Accept": {"application/json"},
}
bodyBytes, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
}
if request.Site != nil {
_, consumableExt, err := extractExtensions(request.Imp[0])
if err != nil {
return nil, err
}
if consumableExt.SiteId == 0 && consumableExt.NetworkId == 0 && consumableExt.UnitId == 0 {
return nil, []error{&errortypes.FailedToRequestBids{
Message: "SiteId, NetworkId and UnitId are all required for site requests",
}}
}
requests := []*adapters.RequestData{
{
Method: "POST",
Uri: "https://e.serverbid.com/sb/rtb",
Body: bodyBytes,
Headers: headers,
},
}
return requests, errs
} else {
_, consumableExt, err := extractExtensions(request.Imp[0])
if err != nil {
return nil, err
}
if consumableExt.PlacementId == "" {
return nil, []error{&errortypes.FailedToRequestBids{
Message: "PlacementId is required for non-site requests",
}}
}
requests := []*adapters.RequestData{
{
Method: "POST",
Uri: "https://e.serverbid.com/rtb/bid?s=" + consumableExt.PlacementId,
Body: bodyBytes,
Headers: headers,
},
}
return requests, errs
}
}
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}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
bidResponse.Currency = response.Cur
var errors []error
for _, seatBid := range response.SeatBid {
for i, bid := range seatBid.Bid {
bidType, err := getMediaTypeForBid(bid)
if err != nil {
errors = append(errors, err)
continue
}
var bidVideo *openrtb_ext.ExtBidPrebidVideo
if bidType == openrtb_ext.BidTypeVideo {
bidVideo = &openrtb_ext.ExtBidPrebidVideo{Duration: int(bid.Dur)}
}
switch bidType {
case openrtb_ext.BidTypeAudio:
seatBid.Bid[i].MType = openrtb2.MarkupAudio
break
case openrtb_ext.BidTypeVideo:
seatBid.Bid[i].MType = openrtb2.MarkupVideo
break
case openrtb_ext.BidTypeBanner:
seatBid.Bid[i].MType = openrtb2.MarkupBanner
break
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: bidType,
BidVideo: bidVideo,
})
}
}
return bidResponse, nil
}
func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
if bid.MType != 0 {
switch bid.MType {
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
case openrtb2.MarkupAudio:
return openrtb_ext.BidTypeAudio, nil
}
}
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Failed to parse impression \"%s\" mediatype", bid.ImpID),
}
}
// Builder builds a new instance of the Consumable 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
}
func extractExtensions(impression openrtb2.Imp) (*adapters.ExtImpBidder, *openrtb_ext.ExtImpConsumable, []error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(impression.Ext, &bidderExt); err != nil {
return nil, nil, []error{&errortypes.BadInput{
Message: err.Error(),
}}
}
var consumableExt openrtb_ext.ExtImpConsumable
if err := json.Unmarshal(bidderExt.Bidder, &consumableExt); err != nil {
return nil, nil, []error{&errortypes.BadInput{
Message: err.Error(),
}}
}
return &bidderExt, &consumableExt, nil
}