-
Notifications
You must be signed in to change notification settings - Fork 690
/
adot.go
155 lines (131 loc) · 4.27 KB
/
adot.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 adot
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/mxmCherry/openrtb/v15/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)
type adapter struct {
endpoint string
}
type adotBidExt struct {
Adot bidExt `json:"adot"`
}
type bidExt struct {
MediaType string `json:"media_type"`
}
// Builder builds a new instance of the Adot adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &adapter{
endpoint: config.Endpoint,
}
return bidder, nil
}
// MakeRequests makes the HTTP requests which should be made to fetch bids.
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var reqJSON []byte
var publisherPath string
var err error
if reqJSON, err = json.Marshal(request); err != nil {
return nil, []error{fmt.Errorf("unable to marshal openrtb request (%v)", err)}
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
if adotExt := getImpAdotExt(&request.Imp[0]); adotExt != nil {
publisherPath = adotExt.PublisherPath
} else {
publisherPath = ""
}
endpoint := strings.Replace(a.endpoint, "{PUBLISHER_PATH}", publisherPath, -1)
return []*adapters.RequestData{{
Method: "POST",
Uri: endpoint,
Body: reqJSON,
Headers: headers,
}}, nil
}
// MakeBids unpacks the server's response into Bids.
// The bidder return a status code 204 when it cannot delivery an ad.
func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if response.StatusCode == http.StatusNoContent {
return nil, nil
}
if response.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}
if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
bidsCapacity := 1
if len(bidResp.SeatBid) > 0 {
bidsCapacity = len(bidResp.SeatBid[0].Bid)
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(bidsCapacity)
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
if bidType, err := getMediaTypeForBid(&sb.Bid[i]); err == nil {
resolveMacros(&sb.Bid[i])
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
})
}
}
}
return bidResponse, nil
}
// getMediaTypeForBid determines which type of bid.
func getMediaTypeForBid(bid *openrtb2.Bid) (openrtb_ext.BidType, error) {
if bid == nil {
return "", fmt.Errorf("the bid request object is nil")
}
var impExt adotBidExt
if err := json.Unmarshal(bid.Ext, &impExt); err == nil {
switch impExt.Adot.MediaType {
case string(openrtb_ext.BidTypeBanner):
return openrtb_ext.BidTypeBanner, nil
case string(openrtb_ext.BidTypeVideo):
return openrtb_ext.BidTypeVideo, nil
case string(openrtb_ext.BidTypeNative):
return openrtb_ext.BidTypeNative, nil
}
}
return "", fmt.Errorf("unrecognized bid type in response from adot")
}
// resolveMacros resolves OpenRTB macros in nurl and adm
func resolveMacros(bid *openrtb2.Bid) {
if bid == nil {
return
}
price := strconv.FormatFloat(bid.Price, 'f', -1, 64)
bid.NURL = strings.Replace(bid.NURL, "${AUCTION_PRICE}", price, -1)
bid.AdM = strings.Replace(bid.AdM, "${AUCTION_PRICE}", price, -1)
}
// getImpAdotExt parses and return first imp ext or nil
func getImpAdotExt(imp *openrtb2.Imp) *openrtb_ext.ExtImpAdot {
var extImpAdot openrtb_ext.ExtImpAdot
var extBidder adapters.ExtImpBidder
err := json.Unmarshal(imp.Ext, &extBidder)
if err != nil {
return nil
}
err = json.Unmarshal(extBidder.Bidder, &extImpAdot)
if err != nil {
return nil
}
return &extImpAdot
}