-
Notifications
You must be signed in to change notification settings - Fork 739
/
imds.go
216 lines (185 loc) · 5.8 KB
/
imds.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package imds
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"text/template"
"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/macros"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)
const adapterVersion string = "pbs-go/1.0.0"
type adapter struct {
EndpointTemplate *template.Template
}
type SyncEndpointTemplateParams struct {
SeatId string
TagId string
}
type ReqExt struct {
SeatId string `json:"seatId"`
}
// Builder builds a new instance of the Imds adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (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 := &adapter{
EndpointTemplate: template,
}
return bidder, nil
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errs []error
var bidRequests []*adapters.RequestData
adapterReq, errors := a.makeRequest(request)
if adapterReq != nil {
bidRequests = append(bidRequests, adapterReq)
}
errs = append(errs, errors...)
return bidRequests, errs
}
func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, []error) {
var errs []error
var validImps []openrtb2.Imp
var re *ReqExt
var firstExtImp *openrtb_ext.ExtImpImds = nil
for _, imp := range request.Imp {
validExtImpObj, err := getExtImpObj(&imp) // getExtImpObj returns {seatId:"", tagId:""}
if err != nil {
errs = append(errs, err)
continue
}
// if the bid request is missing seatId or TagId then ignore it
if validExtImpObj.SeatId == "" || validExtImpObj.TagId == "" {
errs = append(errs, &errortypes.BadServerResponse{
Message: fmt.Sprintf("Invalid Impression"),
})
continue
}
// right here is where we need to take out the tagId and then add it to imp
imp.TagID = validExtImpObj.TagId
validImps = append(validImps, imp)
if firstExtImp == nil {
firstExtImp = validExtImpObj
}
}
if len(validImps) == 0 {
return nil, errs
}
var err error
if firstExtImp == nil || firstExtImp.SeatId == "" || firstExtImp.TagId == "" {
return nil, append(errs, &errortypes.BadServerResponse{
Message: fmt.Sprintf("Invalid Impression"),
})
}
// this is where the empty seatId is filled
re = &ReqExt{SeatId: firstExtImp.SeatId}
// create JSON Request Body
request.Imp = validImps
request.Ext, err = json.Marshal(re)
if err != nil {
return nil, append(errs, err)
}
reqJSON, err := json.Marshal(request)
if err != nil {
return nil, append(errs, err)
}
// set Request Headers
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
// create Request Uri
reqUri, err := a.buildEndpointURL(firstExtImp)
if err != nil {
return nil, append(errs, err)
}
return &adapters.RequestData{
Method: http.MethodPost,
Uri: reqUri,
Body: reqJSON,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
}, errs
}
// Builds enpoint url based on adapter-specific pub settings from imp.ext
func (adapter *adapter) buildEndpointURL(params *openrtb_ext.ExtImpImds) (string, error) {
return macros.ResolveMacros(adapter.EndpointTemplate, macros.EndpointTemplateParams{AccountID: url.QueryEscape(params.SeatId), SourceId: url.QueryEscape(adapterVersion)})
}
func getExtImpObj(imp *openrtb2.Imp) (*openrtb_ext.ExtImpImds, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: err.Error(),
}
}
var imdsExt openrtb_ext.ExtImpImds
if err := json.Unmarshal(bidderExt.Bidder, &imdsExt); err != nil {
return nil, &errortypes.BadInput{
Message: err.Error(),
}
}
return &imdsExt, nil
}
// MakeBids make the bids for the bid response.
func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
const errorMessage string = "Unexpected status code: %d. Run with request.debug = 1 for more info"
switch {
case response.StatusCode == http.StatusNoContent:
return nil, nil
case response.StatusCode == http.StatusBadRequest:
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf(errorMessage, response.StatusCode),
}}
case response.StatusCode != http.StatusOK:
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf(errorMessage, response.StatusCode),
}}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
var mediaType = getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
if mediaType != openrtb_ext.BidTypeBanner && mediaType != openrtb_ext.BidTypeVideo {
continue
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: mediaType,
})
}
}
return bidResponse, 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.Banner != nil {
break
}
if imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
break
}
if imp.Native != nil {
mediaType = openrtb_ext.BidTypeNative
break
}
if imp.Audio != nil {
mediaType = openrtb_ext.BidTypeAudio
break
}
}
}
return mediaType
}