-
Notifications
You must be signed in to change notification settings - Fork 739
/
playdigo.go
160 lines (132 loc) · 4.06 KB
/
playdigo.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
156
157
158
159
160
package playdigo
import (
"encoding/json"
"errors"
"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/openrtb_ext"
)
type adapter struct {
endpoint string
}
type reqBodyExt struct {
PlaydigoBidderExt reqBodyExtBidder `json:"bidder"`
}
type reqBodyExtBidder struct {
Type string `json:"type"`
PlacementID string `json:"placementId,omitempty"`
EndpointID string `json:"endpointId,omitempty"`
}
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 errs []error
var adapterRequests []*adapters.RequestData
reqCopy := *request
for _, imp := range request.Imp {
reqCopy.Imp = []openrtb2.Imp{imp}
var bidderExt adapters.ExtImpBidder
var playdigoExt openrtb_ext.ImpExtPlaydigo
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
errs = append(errs, err)
continue
}
if err := json.Unmarshal(bidderExt.Bidder, &playdigoExt); err != nil {
errs = append(errs, err)
continue
}
impExt := reqBodyExt{PlaydigoBidderExt: reqBodyExtBidder{}}
if playdigoExt.PlacementID != "" {
impExt.PlaydigoBidderExt.PlacementID = playdigoExt.PlacementID
impExt.PlaydigoBidderExt.Type = "publisher"
} else if playdigoExt.EndpointID != "" {
impExt.PlaydigoBidderExt.EndpointID = playdigoExt.EndpointID
impExt.PlaydigoBidderExt.Type = "network"
}
finalyImpExt, err := json.Marshal(impExt)
if err != nil {
errs = append(errs, err)
continue
}
reqCopy.Imp[0].Ext = finalyImpExt
adapterReq, err := a.makeRequest(&reqCopy)
if err != nil {
errs = append(errs, err)
continue
}
adapterRequests = append(adapterRequests, adapterReq)
}
if len(adapterRequests) == 0 {
errs = append(errs, errors.New("found no valid impressions"))
return nil, errs
}
return adapterRequests, nil
}
func (a *adapter) makeRequest(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 adapters.IsResponseStatusCodeNoContent(responseData) {
return nil, nil
}
if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
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))
if len(response.Cur) != 0 {
bidResponse.Currency = response.Cur
}
for _, seatBid := range response.SeatBid {
for i := range seatBid.Bid {
bid := seatBid.Bid[i]
bidType, err := getBidType(bid)
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 getBidType(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
// determinate media type by bid response field mtype
switch bid.MType {
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
case openrtb2.MarkupAudio:
return openrtb_ext.BidTypeAudio, nil
case openrtb2.MarkupNative:
return openrtb_ext.BidTypeNative, nil
}
return "", fmt.Errorf("could not define media type for impression: %s", bid.ImpID)
}