-
Notifications
You must be signed in to change notification settings - Fork 689
/
visx.go
173 lines (144 loc) · 4.69 KB
/
visx.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
package visx
import (
"encoding/json"
"fmt"
"net/http"
"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 VisxAdapter struct {
endpoint string
}
type visxBidExtPrebidMeta struct {
MediaType openrtb_ext.BidType `json:"mediaType"`
}
type visxBidExtPrebid struct {
Meta visxBidExtPrebidMeta `json:"meta"`
}
type visxBidExt struct {
Prebid visxBidExtPrebid `json:"prebid"`
}
type visxBid struct {
ImpID string `json:"impid"`
Price float64 `json:"price"`
UID int `json:"auid"`
CrID string `json:"crid,omitempty"`
AdM string `json:"adm,omitempty"`
ADomain []string `json:"adomain,omitempty"`
DealID string `json:"dealid,omitempty"`
W uint64 `json:"w,omitempty"`
H uint64 `json:"h,omitempty"`
Ext json.RawMessage `json:"ext,omitempty"`
}
type visxSeatBid struct {
Bid []visxBid `json:"bid"`
Seat string `json:"seat,omitempty"`
}
type visxResponse struct {
SeatBid []visxSeatBid `json:"seatbid,omitempty"`
}
// MakeRequests makes the HTTP requests which should be made to fetch bids.
func (a *VisxAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errors = make([]error, 0)
// copy the request, because we are going to mutate it
requestCopy := *request
if len(requestCopy.Cur) == 0 {
requestCopy.Cur = []string{"USD"}
}
reqJSON, err := json.Marshal(requestCopy)
if err != nil {
errors = append(errors, err)
return nil, errors
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
return []*adapters.RequestData{{
Method: "POST",
Uri: a.endpoint,
Body: reqJSON,
Headers: headers,
}}, errors
}
// MakeBids unpacks the server's response into Bids.
func (a *VisxAdapter) 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 visxResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bid := openrtb2.Bid{}
bid.ID = internalRequest.ID
bid.CrID = sb.Bid[i].CrID
bid.ImpID = sb.Bid[i].ImpID
bid.Price = sb.Bid[i].Price
bid.AdM = sb.Bid[i].AdM
bid.W = int64(sb.Bid[i].W)
bid.H = int64(sb.Bid[i].H)
bid.ADomain = sb.Bid[i].ADomain
bid.DealID = sb.Bid[i].DealID
bid.Ext = sb.Bid[i].Ext
bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp, sb.Bid[i])
if err != nil {
return nil, []error{err}
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
})
}
}
return bidResponse, nil
}
func getMediaTypeForImp(impID string, imps []openrtb2.Imp, bid visxBid) (openrtb_ext.BidType, error) {
for _, imp := range imps {
if imp.ID == impID {
var ext visxBidExt
if err := json.Unmarshal(bid.Ext, &ext); err == nil {
if ext.Prebid.Meta.MediaType == openrtb_ext.BidTypeBanner {
return openrtb_ext.BidTypeBanner, nil
}
if ext.Prebid.Meta.MediaType == openrtb_ext.BidTypeVideo {
return openrtb_ext.BidTypeVideo, nil
}
}
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner, nil
}
if imp.Video != nil {
return openrtb_ext.BidTypeVideo, nil
}
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unknown impression type for ID: \"%s\"", impID),
}
}
}
// This shouldnt happen. Lets handle it just incase by returning an error.
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Failed to find impression for ID: \"%s\"", impID),
}
}
// Builder builds a new instance of the Visx adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &VisxAdapter{
endpoint: config.Endpoint,
}
return bidder, nil
}