-
Notifications
You must be signed in to change notification settings - Fork 735
/
kidoz.go
192 lines (169 loc) · 4.98 KB
/
kidoz.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
package kidoz
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/prebid/openrtb/v20/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/openrtb_ext"
)
type KidozAdapter struct {
endpoint string
}
func (a *KidozAdapter) MakeRequests(request *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
headers.Add("x-openrtb-version", "2.5")
impressions := request.Imp
result := make([]*adapters.RequestData, 0, len(impressions))
errs := make([]error, 0, len(impressions))
for i, impression := range impressions {
if impression.Banner == nil && impression.Video == nil {
errs = append(errs, &errortypes.BadInput{
Message: "Kidoz only supports banner or video ads",
})
continue
}
if impression.Banner != nil {
banner := impression.Banner
if banner.Format == nil {
errs = append(errs, &errortypes.BadInput{
Message: "banner format required",
})
continue
}
if len(banner.Format) == 0 {
errs = append(errs, &errortypes.BadInput{
Message: "banner format array is empty",
})
continue
}
}
if len(impression.Ext) == 0 {
errs = append(errs, errors.New("impression extensions required"))
continue
}
var bidderExt adapters.ExtImpBidder
err := json.Unmarshal(impression.Ext, &bidderExt)
if err != nil {
errs = append(errs, err)
continue
}
if len(bidderExt.Bidder) == 0 {
errs = append(errs, errors.New("bidder required"))
continue
}
var impressionExt openrtb_ext.ExtImpKidoz
err = json.Unmarshal(bidderExt.Bidder, &impressionExt)
if err != nil {
errs = append(errs, err)
continue
}
if impressionExt.AccessToken == "" {
errs = append(errs, errors.New("Kidoz access_token required"))
continue
}
if impressionExt.PublisherID == "" {
errs = append(errs, errors.New("Kidoz publisher_id required"))
continue
}
request.Imp = impressions[i : i+1]
body, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
continue
}
result = append(result, &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: body,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
})
}
request.Imp = impressions
if len(result) == 0 {
return nil, errs
}
return result, errs
}
func (a *KidozAdapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var errs []error
switch responseData.StatusCode {
case http.StatusNoContent:
fallthrough
case http.StatusServiceUnavailable:
return nil, nil
case http.StatusBadRequest:
fallthrough
case http.StatusUnauthorized:
fallthrough
case http.StatusForbidden:
return nil, []error{&errortypes.BadInput{
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode) + " " + string(responseData.Body),
}}
case http.StatusOK:
break
default:
return nil, []error{&errortypes.BadServerResponse{
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode) + " " + string(responseData.Body),
}}
}
var bidResponse openrtb2.BidResponse
err := json.Unmarshal(responseData.Body, &bidResponse)
if err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: err.Error(),
}}
}
response := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
for _, seatBid := range bidResponse.SeatBid {
for _, bid := range seatBid.Bid {
thisBid := bid
bidType := GetMediaTypeForImp(bid.ImpID, request.Imp)
if bidType == UndefinedMediaType {
errs = append(errs, &errortypes.BadServerResponse{
Message: "ignoring bid id=" + bid.ID + ", request doesn't contain any valid impression with id=" + bid.ImpID,
})
continue
}
response.Bids = append(response.Bids, &adapters.TypedBid{
Bid: &thisBid,
BidType: bidType,
})
}
}
return response, errs
}
// Builder builds a new instance of the Kidoz 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 := &KidozAdapter{
endpoint: config.Endpoint,
}
return bidder, nil
}
const UndefinedMediaType = openrtb_ext.BidType("")
func GetMediaTypeForImp(impID string, imps []openrtb2.Imp) openrtb_ext.BidType {
var bidType openrtb_ext.BidType = UndefinedMediaType
for _, impression := range imps {
if impression.ID != impID {
continue
}
switch {
case impression.Banner != nil:
bidType = openrtb_ext.BidTypeBanner
case impression.Video != nil:
bidType = openrtb_ext.BidTypeVideo
case impression.Native != nil:
bidType = openrtb_ext.BidTypeNative
case impression.Audio != nil:
bidType = openrtb_ext.BidTypeAudio
}
break
}
return bidType
}