-
Notifications
You must be signed in to change notification settings - Fork 699
/
impactify.go
185 lines (155 loc) · 5.11 KB
/
impactify.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
package impactify
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/prebid/openrtb/v19/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 adapter struct {
endpoint string
}
type ImpactifyExtBidder struct {
Impactify openrtb_ext.ExtImpImpactify `json:"impactify"`
}
type DefaultExtBidder struct {
Bidder openrtb_ext.ExtImpImpactify `json:"bidder"`
}
func (a *adapter) MakeRequests(bidRequest *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var adapterRequests []*adapters.RequestData
for i := 0; i < len(bidRequest.Imp); i++ {
// Check if imp comes with bid floor amount defined in a foreign currency
if bidRequest.Imp[i].BidFloor > 0 && bidRequest.Imp[i].BidFloorCur != "" && strings.ToUpper(bidRequest.Imp[i].BidFloorCur) != "USD" {
// Convert to US dollars
convertedValue, err := reqInfo.ConvertCurrency(bidRequest.Imp[i].BidFloor, bidRequest.Imp[i].BidFloorCur, "USD")
if err != nil {
return nil, []error{err}
}
bidRequest.Imp[i].BidFloorCur = "USD"
bidRequest.Imp[i].BidFloor = convertedValue
}
// Set the CUR of bid to USD after converting all floors
bidRequest.Cur = []string{"USD"}
var impactifyExt ImpactifyExtBidder
var defaultExt DefaultExtBidder
err := json.Unmarshal(bidRequest.Imp[i].Ext, &defaultExt)
if err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unable to decode the imp ext : \"%s\"", bidRequest.Imp[i].ID),
}}
}
impactifyExt.Impactify = defaultExt.Bidder
bidRequest.Imp[i].Ext, err = json.Marshal(impactifyExt)
if err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unable to decode the imp ext : \"%s\"", bidRequest.Imp[i].ID),
}}
}
}
if len(bidRequest.Imp) == 0 {
return nil, []error{&errortypes.BadInput{
Message: "No valid impressions in the bid request",
}}
}
reqJSON, err := json.Marshal(bidRequest)
if err != nil {
return nil, []error{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")
if bidRequest.Device != nil {
if bidRequest.Device.UA != "" {
headers.Add("User-Agent", bidRequest.Device.UA)
}
// Add IPv4 or IPv6 if available
if bidRequest.Device.IP != "" {
headers.Add("X-Forwarded-For", bidRequest.Device.IP)
} else if bidRequest.Device.IPv6 != "" {
headers.Add("X-Forwarded-For", bidRequest.Device.IPv6)
}
}
if bidRequest.Site != nil {
headers.Add("Referer", bidRequest.Site.Page)
}
// set user's cookie
if bidRequest.User != nil && bidRequest.User.BuyerUID != "" {
headers.Add("Cookie", "uids="+bidRequest.User.BuyerUID)
}
adapterRequests = append(adapterRequests, &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: reqJSON,
Headers: headers,
})
return adapterRequests, nil
}
func (a *adapter) MakeBids(
internalRequest *openrtb2.BidRequest,
externalRequest *adapters.RequestData,
response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if response.StatusCode == http.StatusNoContent {
return nil, nil
} else if response.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: "Invalid request.",
}}
} else if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected HTTP status %d.", response.StatusCode),
}}
}
var openRtbBidResponse openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &openRtbBidResponse); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "Bad server body response",
}}
}
if len(openRtbBidResponse.SeatBid) == 0 {
return nil, nil
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(openRtbBidResponse.SeatBid[0].Bid))
bidResponse.Currency = openRtbBidResponse.Cur
sb := openRtbBidResponse.SeatBid[0]
for i := 0; i < len(sb.Bid); i++ {
if !(sb.Bid[i].Price > 0) {
continue
}
impMediaType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
if err != nil {
return nil, []error{err}
}
bid := sb.Bid[i]
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: impMediaType,
})
}
return bidResponse, nil
}
func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner, nil
} else if imp.Video != nil {
return openrtb_ext.BidTypeVideo, nil
}
}
}
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find a supported media type impression \"%s\"", impID),
}
}
// Builder builds a new instance of the Impactify 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
}