-
Notifications
You must be signed in to change notification settings - Fork 739
/
kargo.go
90 lines (77 loc) · 2.58 KB
/
kargo.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
package kargo
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 {
URI string
}
type kargoExt struct {
MediaType string `json:"mediaType"`
}
// Builder builds a new instance of the Kargo 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{
URI: config.Endpoint, // base url of bidding server
}
return bidder, nil
}
// MakeRequests creates outgoing requests to the Kargo bidding server.
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
requestJSON, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
}
requestData := &adapters.RequestData{
Method: "POST",
Uri: a.URI,
Body: requestJSON,
}
return []*adapters.RequestData{requestData}, nil
}
// MakeBids receives a bid response from the Kargo bidding server and creates bids for the publishers auction.
func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
return nil, nil
}
if responseData.StatusCode != http.StatusOK {
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode),
}
return nil, []error{err}
}
var response openrtb2.BidResponse
if err := json.Unmarshal(responseData.Body, &response); err != nil {
return nil, []error{err}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
bidResponse.Currency = response.Cur
for _, seatBid := range response.SeatBid {
for i, bid := range seatBid.Bid {
b := &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: getMediaTypeForBid(bid.Ext),
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, nil
}
func getMediaTypeForBid(ext json.RawMessage) openrtb_ext.BidType {
var impExt kargoExt
if err := json.Unmarshal(ext, &impExt); err == nil {
switch impExt.MediaType {
case string(openrtb_ext.BidTypeVideo):
return openrtb_ext.BidTypeVideo
case string(openrtb_ext.BidTypeNative):
return openrtb_ext.BidTypeNative
}
}
return openrtb_ext.BidTypeBanner
}