-
Notifications
You must be signed in to change notification settings - Fork 742
/
pubnative.go
189 lines (160 loc) · 4.87 KB
/
pubnative.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
package pubnative
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"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 PubnativeAdapter struct {
URI string
}
func (a *PubnativeAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
impCount := len(request.Imp)
requestData := make([]*adapters.RequestData, 0, impCount)
errs := []error{}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
err := checkRequest(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}
for _, imp := range request.Imp {
requestCopy := *request
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
errs = append(errs, err)
continue
}
var pubnativeExt openrtb_ext.ExtImpPubnative
if err := json.Unmarshal(bidderExt.Bidder, &pubnativeExt); err != nil {
errs = append(errs, err)
continue
}
err := convertImpression(&imp)
if err != nil {
errs = append(errs, err)
continue
}
requestCopy.Imp = []openrtb2.Imp{imp}
reqJSON, err := json.Marshal(&requestCopy)
if err != nil {
errs = append(errs, err)
return nil, errs
}
queryParams := url.Values{}
queryParams.Add("apptoken", pubnativeExt.AppAuthToken)
queryParams.Add("zoneid", strconv.Itoa(pubnativeExt.ZoneID))
queryString := queryParams.Encode()
reqData := &adapters.RequestData{
Method: "POST",
Uri: fmt.Sprintf("%s?%s", a.URI, queryString),
Body: reqJSON,
Headers: headers,
}
requestData = append(requestData, reqData)
}
return requestData, errs
}
func checkRequest(request *openrtb2.BidRequest) error {
if request.Device == nil || len(request.Device.OS) == 0 {
return &errortypes.BadInput{
Message: "Impression is missing device OS information",
}
}
return nil
}
func convertImpression(imp *openrtb2.Imp) error {
if imp.Banner == nil && imp.Video == nil && imp.Native == nil {
return &errortypes.BadInput{
Message: "Pubnative only supports banner, video or native ads.",
}
}
if imp.Banner != nil {
bannerCopy, err := convertBanner(imp.Banner)
if err != nil {
return err
}
imp.Banner = bannerCopy
}
return nil
}
// make sure that banner has openrtb 2.3-compatible size information
func convertBanner(banner *openrtb2.Banner) (*openrtb2.Banner, error) {
if banner.W == nil || banner.H == nil || *banner.W == 0 || *banner.H == 0 {
if len(banner.Format) > 0 {
f := banner.Format[0]
bannerCopy := *banner
bannerCopy.W = openrtb2.Int64Ptr(f.W)
bannerCopy.H = openrtb2.Int64Ptr(f.H)
return &bannerCopy, nil
} else {
return nil, &errortypes.BadInput{
Message: "Size information missing for banner",
}
}
}
return banner, nil
}
func (a *PubnativeAdapter) 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 parsedResponse openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &parsedResponse); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: err.Error(),
}}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
for _, sb := range parsedResponse.SeatBid {
for i := 0; i < len(sb.Bid); i++ {
bid := sb.Bid[i]
if bid.Price != 0 {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: getMediaTypeForImp(bid.ImpID, internalRequest.Imp),
})
}
}
}
return bidResponse, nil
}
// Builder builds a new instance of the Pubnative 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 := &PubnativeAdapter{
URI: config.Endpoint,
}
return bidder, nil
}
func getMediaTypeForImp(impId string, imps []openrtb2.Imp) openrtb_ext.BidType {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impId {
if imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
} else if imp.Native != nil {
mediaType = openrtb_ext.BidTypeNative
}
return mediaType
}
}
return mediaType
}