-
Notifications
You must be signed in to change notification settings - Fork 739
/
brave.go
executable file
·158 lines (134 loc) · 4.35 KB
/
brave.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
package brave
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
}
// Builder builds a new instance of the Brave adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
uri, 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: uri,
}
return bidder, nil
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var braveExt *openrtb_ext.ExtImpBrave
var err error
braveExt, err = a.getImpressionExt(&request.Imp[0])
if err != nil {
return nil, []error{err}
}
request.Imp[0].Ext = nil
reqJSON, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
url, err := a.buildEndpointURL(braveExt)
if err != nil {
return nil, []error{err}
}
return []*adapters.RequestData{{
Method: http.MethodPost,
Body: reqJSON,
Uri: url,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
}}, nil
}
func (a *adapter) getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtImpBrave, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: "ext.bidder not provided",
}
}
var braveExt openrtb_ext.ExtImpBrave
if err := json.Unmarshal(bidderExt.Bidder, &braveExt); err != nil {
return nil, &errortypes.BadInput{
Message: "ext.bidder not provided",
}
}
return &braveExt, nil
}
func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtImpBrave) (string, error) {
endpointParams := macros.EndpointTemplateParams{PublisherID: params.PlacementID}
return macros.ResolveMacros(a.endpoint, endpointParams)
}
func (a *adapter) MakeBids(
receivedRequest *openrtb2.BidRequest,
bidderRequest *adapters.RequestData,
bidderResponse *adapters.ResponseData,
) (
*adapters.BidderResponse,
[]error,
) {
if bidderResponse.StatusCode == http.StatusNoContent {
return nil, []error{&errortypes.BadInput{Message: "No bid"}}
}
if bidderResponse.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", bidderResponse.StatusCode),
}}
}
if bidderResponse.StatusCode == http.StatusServiceUnavailable {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Service Unavailable. Status Code: [ %d ] ", bidderResponse.StatusCode),
}}
}
if bidderResponse.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Something went wrong, please contact your Account Manager. Status Code: [ %d ] ", bidderResponse.StatusCode),
}}
}
var bidResponse openrtb2.BidResponse
if err := json.Unmarshal(bidderResponse.Body, &bidResponse); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "Bad Server Response",
}}
}
if len(bidResponse.SeatBid) == 0 {
return nil, []error{&errortypes.BadServerResponse{
Message: "Empty SeatBid array",
}}
}
bidResponseFinal := adapters.NewBidderResponseWithBidsCapacity(len(bidResponse.SeatBid[0].Bid))
sb := bidResponse.SeatBid[0]
for _, bid := range sb.Bid {
bidResponseFinal.Bids = append(bidResponseFinal.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: getMediaTypeForImp(bid.ImpID, receivedRequest.Imp),
})
}
return bidResponseFinal, 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.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
} else if imp.Native != nil {
mediaType = openrtb_ext.BidTypeNative
}
return mediaType
}
}
return mediaType
}