-
Notifications
You must be signed in to change notification settings - Fork 739
/
adf.go
155 lines (129 loc) · 3.94 KB
/
adf.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
package adf
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 adfRequestExt struct {
openrtb_ext.ExtRequest
PriceType string `json:"pt"`
}
// Builder builds a new instance of the Adf adapter for the given bidder with the given config.
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, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errors []error
var validImps = make([]openrtb2.Imp, 0, len(request.Imp))
priceType := ""
for _, imp := range request.Imp {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
errors = append(errors, &errortypes.BadInput{
Message: err.Error(),
})
continue
}
var adfImpExt openrtb_ext.ExtImpAdf
if err := json.Unmarshal(bidderExt.Bidder, &adfImpExt); err != nil {
errors = append(errors, &errortypes.BadInput{
Message: err.Error(),
})
continue
}
imp.TagID = adfImpExt.MasterTagID.String()
validImps = append(validImps, imp)
// If imps specify priceType they should all be the same. If they differ, only the first one will be used
if adfImpExt.PriceType != "" && priceType == "" {
priceType = adfImpExt.PriceType
}
}
if priceType != "" {
requestExt := adfRequestExt{}
var err error
if len(request.Ext) > 0 {
if err = json.Unmarshal(request.Ext, &requestExt); err != nil {
errors = append(errors, err)
}
}
if err == nil {
requestExt.PriceType = priceType
if request.Ext, err = json.Marshal(&requestExt); err != nil {
errors = append(errors, err)
}
}
}
request.Imp = validImps
requestJSON, err := json.Marshal(request)
if err != nil {
errors = append(errors, err)
return nil, errors
}
requestData := &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: requestJSON,
}
return []*adapters.RequestData{requestData}, errors
}
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.StatusBadRequest {
err := &errortypes.BadInput{
Message: "Unexpected status code: 400. Bad request from publisher.",
}
return nil, []error{err}
}
if responseData.StatusCode != http.StatusOK {
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d.", 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
var errors []error
for _, seatBid := range response.SeatBid {
for i, bid := range seatBid.Bid {
bidType, err := getMediaTypeForBid(bid)
if err != nil {
errors = append(errors, err)
continue
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: bidType,
})
}
}
return bidResponse, errors
}
func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
if bid.Ext != nil {
var bidExt openrtb_ext.ExtBid
err := json.Unmarshal(bid.Ext, &bidExt)
if err == nil && bidExt.Prebid != nil {
return openrtb_ext.ParseBidType(string(bidExt.Prebid.Type))
}
}
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Failed to parse impression \"%s\" mediatype", bid.ImpID),
}
}