-
Notifications
You must be signed in to change notification settings - Fork 715
/
evolution.go
115 lines (98 loc) · 2.82 KB
/
evolution.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
package evolution
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 adapter struct {
URI string
}
type bidExt struct {
MediaType openrtb_ext.BidType `json:"mediaType"`
}
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
bidder := &adapter{
URI: config.Endpoint,
}
return bidder, nil
}
func (a *adapter) MakeRequests(
openRTBRequest *openrtb2.BidRequest,
reqInfo *adapters.ExtraRequestInfo,
) (
requestsToBidder []*adapters.RequestData,
errs []error,
) {
reqJSON, err := json.Marshal(openRTBRequest)
if err != nil {
return nil, []error{err}
}
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
return []*adapters.RequestData{{
Method: http.MethodPost,
Body: reqJSON,
Uri: a.URI,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(openRTBRequest.Imp),
}}, nil
}
func (a *adapter) MakeBids(
openRTBRequest *openrtb2.BidRequest,
requestToBidder *adapters.RequestData,
bidderRawResponse *adapters.ResponseData,
) (
bidderResponse *adapters.BidderResponse,
errs []error,
) {
if bidderRawResponse.StatusCode == http.StatusNoContent {
return nil, nil
}
if bidderRawResponse.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Bad Request. %s", string(bidderRawResponse.Body)),
}}
}
if bidderRawResponse.StatusCode == http.StatusServiceUnavailable {
return nil, nil
}
if bidderRawResponse.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Something went wrong, please contact your Account Manager. Status Code: [ %d ] ", bidderRawResponse.StatusCode),
}}
}
responseBody := bidderRawResponse.Body
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(responseBody, &bidResp); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Bad response, %s", err),
}}
}
if len(bidResp.SeatBid) == 0 {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Empty seatbid"),
}}
}
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid[0].Bid))
sb := bidResp.SeatBid[0]
for i := range sb.Bid {
var bidType openrtb_ext.BidType
var bidExt bidExt
if err := json.Unmarshal(sb.Bid[i].Ext, &bidExt); err != nil {
bidType = openrtb_ext.BidTypeBanner
} else {
bidType = bidExt.MediaType
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
})
}
return bidResponse, nil
}