-
Notifications
You must be signed in to change notification settings - Fork 735
/
axis.go
142 lines (116 loc) · 3.54 KB
/
axis.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
package axis
import (
"encoding/json"
"fmt"
"net/http"
"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/openrtb_ext"
)
type adapter struct {
endpoint string
}
type reqBodyExt struct {
AxisBidderExt openrtb_ext.ImpExtAxis `json:"bidder"`
}
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
bidder := &adapter{
endpoint: config.Endpoint,
}
return bidder, nil
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var adapterRequests []*adapters.RequestData
originalImpSlice := request.Imp
for i := range request.Imp {
currImp := originalImpSlice[i]
request.Imp = []openrtb2.Imp{currImp}
var bidderExt reqBodyExt
if err := json.Unmarshal(currImp.Ext, &bidderExt); err != nil {
continue
}
extJson, err := json.Marshal(bidderExt)
if err != nil {
return nil, []error{err}
}
request.Imp[0].Ext = extJson
adapterReq, err := a.buildRequest(request)
if err != nil {
return nil, []error{err}
}
if adapterReq != nil {
adapterRequests = append(adapterRequests, adapterReq)
}
}
request.Imp = originalImpSlice
return adapterRequests, nil
}
func (a *adapter) buildRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) {
reqJSON, err := json.Marshal(request)
if err != nil {
return nil, err
}
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,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
}, nil
}
func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
return nil, nil
}
if responseData.StatusCode != http.StatusOK {
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode),
}
return nil, []error{err}
}
var response openrtb2.BidResponse
if err := json.Unmarshal(responseData.Body, &response); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
bidResponse.Currency = response.Cur
impsMappedByID := make(map[string]openrtb2.Imp, len(request.Imp))
for i, imp := range request.Imp {
impsMappedByID[request.Imp[i].ID] = imp
}
for _, seatBid := range response.SeatBid {
for i := range seatBid.Bid {
bidType, err := getMediaTypeForImp(seatBid.Bid[i].ImpID, impsMappedByID)
if err != nil {
return nil, []error{err}
}
b := &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, nil
}
func getMediaTypeForImp(impID string, impMap map[string]openrtb2.Imp) (openrtb_ext.BidType, error) {
if index, found := impMap[impID]; found {
if index.Banner != nil {
return openrtb_ext.BidTypeBanner, nil
}
if index.Video != nil {
return openrtb_ext.BidTypeVideo, nil
}
if index.Native != nil {
return openrtb_ext.BidTypeNative, nil
}
}
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find impression \"%s\"", impID),
}
}