-
Notifications
You must be signed in to change notification settings - Fork 739
/
datablocks.go
192 lines (154 loc) · 4.58 KB
/
datablocks.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 datablocks
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"text/template"
"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/macros"
"github.com/prebid/prebid-server/openrtb_ext"
)
type DatablocksAdapter struct {
EndpointTemplate *template.Template
}
func (a *DatablocksAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
errs := make([]error, 0, len(request.Imp))
headers := http.Header{
"Content-Type": {"application/json"},
"Accept": {"application/json"},
}
// Pull the host and source ID info from the bidder params.
reqImps, err := splitImpressions(request.Imp)
if err != nil {
errs = append(errs, err)
}
requests := []*adapters.RequestData{}
for reqExt, reqImp := range reqImps {
request.Imp = reqImp
reqJson, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
continue
}
urlParams := macros.EndpointTemplateParams{Host: reqExt.Host, SourceId: strconv.Itoa(reqExt.SourceId)}
url, err := macros.ResolveMacros(a.EndpointTemplate, urlParams)
if err != nil {
errs = append(errs, err)
continue
}
request := adapters.RequestData{
Method: "POST",
Uri: url,
Body: reqJson,
Headers: headers}
requests = append(requests, &request)
}
return requests, errs
}
/*
internal original request in OpenRTB, external = result of us having converted it (what comes out of MakeRequests)
*/
func (a *DatablocksAdapter) 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.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("ERR, response with status %d", response.StatusCode),
}}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponse()
bidResponse.Currency = bidResp.Cur
for _, seatBid := range bidResp.SeatBid {
for i := 0; i < len(seatBid.Bid); i++ {
bid := seatBid.Bid[i]
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: getMediaType(bid.ImpID, internalRequest.Imp),
})
}
}
return bidResponse, nil
}
func splitImpressions(imps []openrtb2.Imp) (map[openrtb_ext.ExtImpDatablocks][]openrtb2.Imp, error) {
var m = make(map[openrtb_ext.ExtImpDatablocks][]openrtb2.Imp)
for _, imp := range imps {
bidderParams, err := getBidderParams(&imp)
if err != nil {
return nil, err
}
v, ok := m[*bidderParams]
if ok {
m[*bidderParams] = append(v, imp)
} else {
m[*bidderParams] = []openrtb2.Imp{imp}
}
}
return m, nil
}
func getBidderParams(imp *openrtb2.Imp) (*openrtb_ext.ExtImpDatablocks, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: fmt.Sprintf("Missing bidder ext: %s", err.Error()),
}
}
var datablocksExt openrtb_ext.ExtImpDatablocks
if err := json.Unmarshal(bidderExt.Bidder, &datablocksExt); err != nil {
return nil, &errortypes.BadInput{
Message: fmt.Sprintf("Cannot Resolve host or sourceId: %s", err.Error()),
}
}
if datablocksExt.SourceId < 1 {
return nil, &errortypes.BadInput{
Message: "Invalid/Missing SourceId",
}
}
if len(datablocksExt.Host) < 1 {
return nil, &errortypes.BadInput{
Message: "Invalid/Missing Host",
}
}
return &datablocksExt, nil
}
func getMediaType(impID string, imps []openrtb2.Imp) openrtb_ext.BidType {
bidType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impID {
if imp.Video != nil {
bidType = openrtb_ext.BidTypeVideo
break
} else if imp.Native != nil {
bidType = openrtb_ext.BidTypeNative
break
} else {
bidType = openrtb_ext.BidTypeBanner
break
}
}
}
return bidType
}
// Builder builds a new instance of the Datablocks adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
template, err := template.New("endpointTemplate").Parse(config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
}
bidder := &DatablocksAdapter{
EndpointTemplate: template,
}
return bidder, nil
}