forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 5
/
decenterads.go
124 lines (108 loc) · 3.36 KB
/
decenterads.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
package decenterads
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"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
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
impressions := request.Imp
result := make([]*adapters.RequestData, 0, len(impressions))
errs := make([]error, 0, len(impressions))
for _, impression := range impressions {
var impExt map[string]json.RawMessage
if err := json.Unmarshal(impression.Ext, &impExt); err != nil {
errs = append(errs, fmt.Errorf("unable to parse bidder parameers: %s", err))
continue
}
bidderExt, bidderExtExists := impExt["bidder"]
if !bidderExtExists || len(bidderExt) == 0 {
errs = append(errs, errors.New("bidder parameters required"))
continue
}
impression.Ext = bidderExt
request.Imp = []openrtb2.Imp{impression}
body, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
continue
}
result = append(result, &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: body,
Headers: headers,
})
}
request.Imp = impressions
return result, errs
}
func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var errs []error
switch responseData.StatusCode {
case http.StatusNoContent:
return nil, nil
case http.StatusBadRequest:
return nil, []error{&errortypes.BadInput{
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode),
}}
case http.StatusOK:
break
default:
return nil, []error{&errortypes.BadServerResponse{
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode),
}}
}
var bidResponse openrtb2.BidResponse
err := json.Unmarshal(responseData.Body, &bidResponse)
if err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: err.Error(),
}}
}
response := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
for _, seatBid := range bidResponse.SeatBid {
for i := range seatBid.Bid {
response.Bids = append(response.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: getMediaTypeForImp(seatBid.Bid[i].ImpID, request.Imp),
})
}
}
return response, errs
}
func getMediaTypeForImp(impID string, imps []openrtb2.Imp) openrtb_ext.BidType {
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner
} else if imp.Video != nil {
return openrtb_ext.BidTypeVideo
} else if imp.Native != nil {
return openrtb_ext.BidTypeNative
} else if imp.Audio != nil {
return openrtb_ext.BidTypeAudio
}
}
}
return openrtb_ext.BidTypeBanner
}
// Builder builds a new instance of the DecenterAds 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
}