-
Notifications
You must be signed in to change notification settings - Fork 742
/
revcontent.go
106 lines (86 loc) · 2.78 KB
/
revcontent.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
package revcontent
import (
"encoding/json"
"fmt"
"net/http"
"github.com/prebid/openrtb/v19/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)
type adapter struct {
endpoint string
}
// Builder builds a new instance of the Revcontent 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 := &adapter{
endpoint: config.Endpoint,
}
return bidder, nil
}
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
reqBody, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
}
if err := checkRequest(request); err != nil {
return nil, []error{err}
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
req := &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: reqBody,
Headers: headers,
}
return []*adapters.RequestData{req}, nil
}
func checkRequest(request *openrtb2.BidRequest) error {
if (request.App == nil || len(request.App.Name) == 0) && (request.Site == nil || len(request.Site.Domain) == 0) {
return &errortypes.BadInput{
Message: "Impression is missing app name or site domain, and must contain one.",
}
}
return nil
}
// MakeBids make the bids for the bid response.
func (a *adapter) 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.", response.StatusCode),
}}
}
if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("unexpected status code: %d.", response.StatusCode),
}}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
var mediaType = getBidType(sb.Bid[i].AdM)
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: mediaType,
})
}
}
return bidResponse, nil
}
func getBidType(bidAdm string) openrtb_ext.BidType {
// native: {"ver":"1.1","assets":...
// banner: <div id='rtb-widget...
if bidAdm != "" && bidAdm[:1] == "<" {
return openrtb_ext.BidTypeBanner
}
return openrtb_ext.BidTypeNative
}