-
Notifications
You must be signed in to change notification settings - Fork 717
/
unruly.go
157 lines (140 loc) · 4.47 KB
/
unruly.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
package unruly
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 adapter struct {
endPoint string
}
// Builder builds a new instance of the Unruly adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &adapter{
endPoint: config.Endpoint,
}
return bidder, nil
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
errs := make([]error, 0, len(request.Imp))
request, errs = a.preProcess(request, errs)
if request != nil {
reqJSON, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}
if a.endPoint != "" {
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
return []*adapters.RequestData{{
Method: "POST",
Uri: a.endPoint,
Body: reqJSON,
Headers: headers,
}}, errs
}
}
return nil, errs
}
func (a *adapter) preProcess(req *openrtb2.BidRequest, errors []error) (*openrtb2.BidRequest, []error) {
numRequests := len(req.Imp)
for i := 0; i < numRequests; i++ {
imp := req.Imp[i]
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
err = &errortypes.BadInput{
Message: fmt.Sprintf("ext data not provided in imp id=%s. Abort all Request", imp.ID),
}
errors = append(errors, err)
return nil, errors
}
var unrulyExt openrtb_ext.ExtImpUnruly
if err := json.Unmarshal(bidderExt.Bidder, &unrulyExt); err != nil {
err = &errortypes.BadInput{
Message: fmt.Sprintf("siteid not provided in imp id=%s. Abort all Request", imp.ID),
}
errors = append(errors, err)
return nil, errors
}
unrulyExtCopy, err := json.Marshal(&unrulyExt)
if err != nil {
errors = append(errors, err)
return nil, errors
}
bidderExtCopy := struct {
Bidder json.RawMessage `json:"bidder,omitempty"`
}{unrulyExtCopy}
impExtCopy, err := json.Marshal(&bidderExtCopy)
if err != nil {
errors = append(errors, err)
return nil, errors
}
imp.Ext = impExtCopy
req.Imp[i] = imp
}
return req, errors
}
func (a *adapter) 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 openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("bad server response: %d. ", err),
}}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
var errs []error
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
var bidType, err = getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
if err != nil {
errs = append(errs, err...)
} else {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
})
}
}
}
return bidResponse, errs
}
func getMediaTypeForImp(impId string, imps []openrtb2.Imp) (openrtb_ext.BidType, []error) {
var errs []error
var noMatchingImps []string
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impId {
if imp.Banner != nil {
mediaType = openrtb_ext.BidTypeBanner
} else if imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
} else {
errs = append(errs, fmt.Errorf("bid responses mediaType didn't match supported mediaTypes"))
}
return mediaType, errs
} else {
noMatchingImps = append(noMatchingImps, imp.ID)
}
}
errs = append(errs, fmt.Errorf("Bid response imp ID %s not found in bid request containing imps %v\n", impId, noMatchingImps))
return mediaType, errs
}