-
Notifications
You must be signed in to change notification settings - Fork 735
/
motorik.go
176 lines (147 loc) · 4.75 KB
/
motorik.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
package motorik
import (
"encoding/json"
"fmt"
"net/http"
"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"
)
type adapter struct {
endpoint *template.Template
}
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{
endpoint: template,
}
return bidder, nil
}
func getHeaders(request *openrtb2.BidRequest) http.Header {
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
headers.Add("X-Openrtb-Version", "2.5")
if request.Device != nil {
if len(request.Device.UA) > 0 {
headers.Add("User-Agent", request.Device.UA)
}
if len(request.Device.IPv6) > 0 {
headers.Add("X-Forwarded-For", request.Device.IPv6)
}
if len(request.Device.IP) > 0 {
headers.Add("X-Forwarded-For", request.Device.IP)
}
}
return headers
}
func (a *adapter) MakeRequests(openRTBRequest *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (requestsToBidder []*adapters.RequestData, errs []error) {
motorikExt, err := getImpressionExt(&openRTBRequest.Imp[0])
if err != nil {
return nil, []error{err}
}
openRTBRequest.Imp[0].Ext = nil
url, err := a.buildEndpointURL(motorikExt)
if err != nil {
return nil, []error{err}
}
reqJSON, err := json.Marshal(openRTBRequest)
if err != nil {
return nil, []error{err}
}
return []*adapters.RequestData{{
Method: http.MethodPost,
Body: reqJSON,
Uri: url,
Headers: getHeaders(openRTBRequest),
ImpIDs: openrtb_ext.GetImpIDs(openRTBRequest.Imp),
}}, nil
}
func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtMotorik, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Error parsing motorikExt - " + err.Error(),
}
}
var motorikExt openrtb_ext.ExtMotorik
if err := json.Unmarshal(bidderExt.Bidder, &motorikExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Error parsing bidderExt - " + err.Error(),
}
}
return &motorikExt, nil
}
func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtMotorik) (string, error) {
endpointParams := macros.EndpointTemplateParams{AccountID: params.AccountID, SourceId: params.PlacementID}
return macros.ResolveMacros(a.endpoint, endpointParams)
}
func checkResponseStatusCodes(response *adapters.ResponseData) error {
if response.StatusCode == http.StatusServiceUnavailable {
return &errortypes.BadServerResponse{
Message: fmt.Sprintf("Something went wrong Status Code: [ %d ] ", response.StatusCode),
}
}
return adapters.CheckResponseStatusCodeForErrors(response)
}
func (a *adapter) MakeBids(openRTBRequest *openrtb2.BidRequest, requestToBidder *adapters.RequestData, bidderRawResponse *adapters.ResponseData) (bidderResponse *adapters.BidderResponse, errs []error) {
if adapters.IsResponseStatusCodeNoContent(bidderRawResponse) {
return nil, nil
}
httpStatusError := checkResponseStatusCodes(bidderRawResponse)
if httpStatusError != nil {
return nil, []error{httpStatusError}
}
responseBody := bidderRawResponse.Body
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(responseBody, &bidResp); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "Bad Server Response",
}}
}
if len(bidResp.SeatBid) == 0 {
return nil, []error{&errortypes.BadServerResponse{
Message: "Empty SeatBid array",
}}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
var bidsArray []*adapters.TypedBid
for _, sb := range bidResp.SeatBid {
for idx, bid := range sb.Bid {
bidType, err := getMediaTypeForImp(bid.ImpID, openRTBRequest.Imp)
if err != nil {
return nil, []error{err}
}
bidsArray = append(bidsArray, &adapters.TypedBid{
Bid: &sb.Bid[idx],
BidType: bidType,
})
}
}
bidResponse.Bids = bidsArray
return bidResponse, nil
}
func getMediaTypeForImp(impId string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
for _, imp := range imps {
if imp.ID == impId {
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner, nil
} else if imp.Video != nil {
return openrtb_ext.BidTypeVideo, nil
} else if imp.Native != nil {
return openrtb_ext.BidTypeNative, nil
}
break
}
}
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find impression \"%s\"", impId),
}
}