-
Notifications
You must be signed in to change notification settings - Fork 689
/
adot.go
118 lines (98 loc) · 3.3 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
package adot
import (
"encoding/json"
"fmt"
"net/http"
"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 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")
return []*adapters.RequestData{{
Method: "POST",
Uri: a.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 {
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")
}