-
Notifications
You must be signed in to change notification settings - Fork 739
/
vrtcal.go
108 lines (88 loc) · 2.91 KB
/
vrtcal.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
package vrtcal
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 VrtcalAdapter struct {
endpoint string
}
func (a *VrtcalAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errs []error
var adapterRequests []*adapters.RequestData
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")
reqData := adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: reqJSON,
Headers: headers,
}
adapterRequests = append(adapterRequests, &reqData)
return adapterRequests, errs
}
// MakeBids make the bids for the bid response.
func (a *VrtcalAdapter) 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.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)
var errs []error
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidType, err := getReturnTypeForImp(sb.Bid[i].MType)
if err == nil {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
})
} else {
errs = append(errs, err)
}
}
}
return bidResponse, errs
}
// Builder builds a new instance of the Vrtcal 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 := &VrtcalAdapter{
endpoint: config.Endpoint,
}
return bidder, nil
}
func getReturnTypeForImp(mType openrtb2.MarkupType) (openrtb_ext.BidType, error) {
if mType == openrtb2.MarkupBanner {
return openrtb_ext.BidTypeBanner, nil
} else if mType == openrtb2.MarkupVideo {
return openrtb_ext.BidTypeVideo, nil
} else if mType == openrtb2.MarkupNative {
return openrtb_ext.BidTypeNative, nil
} else {
return "", &errortypes.BadServerResponse{
Message: "Unsupported return type"}
}
}