-
Notifications
You must be signed in to change notification settings - Fork 726
/
mgid.go
178 lines (149 loc) · 4.4 KB
/
mgid.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package mgid
import (
"bytes"
"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 MgidAdapter struct {
endpoint string
}
type ReqExt struct {
PlacementId string `json:"placementId"`
AccountId string `json:"accountId"`
}
type RespBidExt struct {
CreativeType openrtb_ext.BidType `json:"crtype"`
}
func (a *MgidAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (adapterRequests []*adapters.RequestData, errs []error) {
adapterReq, errs := a.makeRequest(request)
if adapterReq != nil && len(errs) == 0 {
adapterRequests = append(adapterRequests, adapterReq)
}
return
}
func (a *MgidAdapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, []error) {
var errs []error
path, err := preprocess(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}
// Last Step
reqJSON, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}
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 + path,
Body: reqJSON,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
}, errs
}
// Mutate the request to get it ready to send to yieldmo.
func preprocess(request *openrtb2.BidRequest) (path string, err error) {
if request.TMax == 0 {
request.TMax = 200
}
for i := 0; i < len(request.Imp); i++ {
var imp = request.Imp[i]
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return "", &errortypes.BadInput{
Message: err.Error(),
}
}
var mgidExt openrtb_ext.ExtImpMgid
if err := json.Unmarshal(bidderExt.Bidder, &mgidExt); err != nil {
return "", &errortypes.BadInput{
Message: err.Error(),
}
}
if path == "" {
path = mgidExt.AccountId
}
if mgidExt.PlacementId == "" {
request.Imp[i].TagID = imp.ID
} else {
request.Imp[i].TagID = mgidExt.PlacementId + "/" + imp.ID
}
cur := ""
if mgidExt.Currency != "" && mgidExt.Currency != "USD" {
cur = mgidExt.Currency
}
if cur == "" && mgidExt.Cur != "" && mgidExt.Cur != "USD" {
cur = mgidExt.Cur
}
bidfloor := mgidExt.BidFloor
if bidfloor <= 0 {
bidfloor = mgidExt.BidFloor2
}
if bidfloor > 0 {
request.Imp[i].BidFloor = bidfloor
}
if cur != "" {
request.Imp[i].BidFloorCur = cur
}
}
if path == "" {
return "", &errortypes.BadInput{
Message: "accountId is not set",
}
}
return
}
func (a *MgidAdapter) MakeBids(bidReq *openrtb2.BidRequest, unused *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}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
bidResponse.Currency = bidResp.Cur
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidType := openrtb_ext.BidTypeBanner
if len(sb.Bid[i].Ext) > 0 && bytes.Contains(sb.Bid[i].Ext, []byte("crtype")) {
ext := RespBidExt{}
if err := json.Unmarshal(sb.Bid[i].Ext, &ext); err == nil && len(ext.CreativeType) > 0 {
bidType = ext.CreativeType
}
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
})
}
}
return bidResponse, nil
}
// Builder builds a new instance of the Mgid 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 := &MgidAdapter{
endpoint: config.Endpoint,
}
return bidder, nil
}