-
Notifications
You must be signed in to change notification settings - Fork 739
/
iqx.go
164 lines (133 loc) · 4.17 KB
/
iqx.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
package iqx
import (
"encoding/json"
"fmt"
"net/http"
"text/template"
"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/macros"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)
type bidType struct {
Type string `json:"type"`
}
type bidExt struct {
Prebid bidType `json:"prebid"`
}
type adapter struct {
endpoint *template.Template
}
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
tmpl, err := template.New("endpointTemplate").Parse(config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse endpoint URL template: %v", err)
}
bidder := &adapter{
endpoint: tmpl,
}
return bidder, nil
}
func (a *adapter) buildEndpointFromRequest(imp *openrtb2.Imp) (string, error) {
var impExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &impExt); err != nil {
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to deserialize bidder impression extension: %v", err),
}
}
var iqzonexExt openrtb_ext.ExtIQX
if err := json.Unmarshal(impExt.Bidder, &iqzonexExt); err != nil {
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to deserialize IQZonex extension: %v", err),
}
}
endpointParams := macros.EndpointTemplateParams{
Host: iqzonexExt.Env,
SourceId: iqzonexExt.Pid,
}
return macros.ResolveMacros(a.endpoint, endpointParams)
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var requests []*adapters.RequestData
var errs []error
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
requestCopy := *request
for _, imp := range request.Imp {
requestCopy.Imp = []openrtb2.Imp{imp}
endpoint, err := a.buildEndpointFromRequest(&imp)
if err != nil {
errs = append(errs, err)
continue
}
requestJSON, err := json.Marshal(requestCopy)
if err != nil {
errs = append(errs, err)
continue
}
request := &adapters.RequestData{
Method: http.MethodPost,
Body: requestJSON,
Uri: endpoint,
Headers: headers,
}
requests = append(requests, request)
}
return requests, errs
}
func (a *adapter) MakeBids(openRTBRequest *openrtb2.BidRequest, requestToBidder *adapters.RequestData, bidderRawResponse *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if adapters.IsResponseStatusCodeNoContent(bidderRawResponse) {
return nil, nil
}
if bidderRawResponse.StatusCode == http.StatusServiceUnavailable {
return nil, []error{&errortypes.BadInput{
Message: "Bidder IQZonex is unavailable. Please contact the bidder support.",
}}
}
if err := adapters.CheckResponseStatusCodeForErrors(bidderRawResponse); err != nil {
return nil, []error{err}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(bidderRawResponse.Body, &bidResp); err != nil {
return nil, []error{err}
}
if len(bidResp.SeatBid) == 0 {
return nil, []error{&errortypes.BadServerResponse{
Message: "Array SeatBid cannot be empty",
}}
}
return prepareBidResponse(bidResp.SeatBid)
}
func prepareBidResponse(seats []openrtb2.SeatBid) (*adapters.BidderResponse, []error) {
errs := []error{}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(seats))
for _, seatBid := range seats {
for bidId, bid := range seatBid.Bid {
bidType, err := getMediaTypeForBid(bid)
if err != nil {
errs = append(errs, err)
continue
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[bidId],
BidType: bidType,
})
}
}
return bidResponse, errs
}
func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
switch bid.MType {
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
case openrtb2.MarkupNative:
return openrtb_ext.BidTypeNative, nil
default:
return "", fmt.Errorf("failed to parse bid mtype for impression id \"%s\"", bid.ImpID)
}
}