-
Notifications
You must be signed in to change notification settings - Fork 739
/
logicad.go
162 lines (141 loc) · 4.84 KB
/
logicad.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
package logicad
import (
"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 LogicadAdapter struct {
endpoint string
}
func (adapter *LogicadAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
if len(request.Imp) == 0 {
return nil, []error{&errortypes.BadInput{Message: "No impression in the bid request"}}
}
pub2impressions, imps, errs := getImpressionsInfo(request.Imp)
if len(pub2impressions) == 0 || len(imps) == 0 {
return nil, errs
}
result := make([]*adapters.RequestData, 0, len(pub2impressions))
for k, imps := range pub2impressions {
bidRequest, err := adapter.buildAdapterRequest(request, &k, imps)
if err != nil {
errs = append(errs, err)
} else {
result = append(result, bidRequest)
}
}
return result, errs
}
func getImpressionsInfo(imps []openrtb2.Imp) (map[openrtb_ext.ExtImpLogicad][]openrtb2.Imp, []openrtb2.Imp, []error) {
errors := make([]error, 0, len(imps))
resImps := make([]openrtb2.Imp, 0, len(imps))
res := make(map[openrtb_ext.ExtImpLogicad][]openrtb2.Imp)
for _, imp := range imps {
impExt, err := getImpressionExt(&imp)
if err != nil {
errors = append(errors, err)
continue
}
if err := validateImpression(&impExt); err != nil {
errors = append(errors, err)
continue
}
if res[impExt] == nil {
res[impExt] = make([]openrtb2.Imp, 0)
}
res[impExt] = append(res[impExt], imp)
resImps = append(resImps, imp)
}
return res, resImps, errors
}
func validateImpression(impExt *openrtb_ext.ExtImpLogicad) error {
if impExt.Tid == "" {
return &errortypes.BadInput{Message: "No tid value provided"}
}
return nil
}
func getImpressionExt(imp *openrtb2.Imp) (openrtb_ext.ExtImpLogicad, error) {
var bidderExt adapters.ExtImpBidder
var logicadExt openrtb_ext.ExtImpLogicad
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return logicadExt, &errortypes.BadInput{
Message: err.Error(),
}
}
if err := json.Unmarshal(bidderExt.Bidder, &logicadExt); err != nil {
return logicadExt, &errortypes.BadInput{
Message: err.Error(),
}
}
return logicadExt, nil
}
func (adapter *LogicadAdapter) buildAdapterRequest(prebidBidRequest *openrtb2.BidRequest, params *openrtb_ext.ExtImpLogicad, imps []openrtb2.Imp) (*adapters.RequestData, error) {
newBidRequest := createBidRequest(prebidBidRequest, params, imps)
reqJSON, err := json.Marshal(newBidRequest)
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: adapter.endpoint,
Body: reqJSON,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(imps)}, nil
}
func createBidRequest(prebidBidRequest *openrtb2.BidRequest, params *openrtb_ext.ExtImpLogicad, imps []openrtb2.Imp) *openrtb2.BidRequest {
bidRequest := *prebidBidRequest
bidRequest.Imp = imps
for idx := range bidRequest.Imp {
imp := &bidRequest.Imp[idx]
imp.TagID = params.Tid
imp.Ext = nil
}
return &bidRequest
}
// MakeBids translates Logicad bid response to prebid-server specific format
func (adapter *LogicadAdapter) 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.StatusOK {
msg := fmt.Sprintf("Unexpected http status code: %d", response.StatusCode)
return nil, []error{&errortypes.BadServerResponse{Message: msg}}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
msg := fmt.Sprintf("Bad server response: %d", err)
return nil, []error{&errortypes.BadServerResponse{Message: msg}}
}
if len(bidResp.SeatBid) != 1 {
msg := fmt.Sprintf("Invalid SeatBids count: %d", len(bidResp.SeatBid))
return nil, []error{&errortypes.BadServerResponse{Message: msg}}
}
seatBid := bidResp.SeatBid[0]
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(seatBid.Bid))
for i := 0; i < len(seatBid.Bid); i++ {
bid := seatBid.Bid[i]
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: openrtb_ext.BidTypeBanner,
})
}
if bidResp.Cur != "" {
bidResponse.Currency = bidResp.Cur
}
return bidResponse, nil
}
// Builder builds a new instance of the Logicad 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 := &LogicadAdapter{
endpoint: config.Endpoint,
}
return bidder, nil
}