This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
forked from prebid/prebid-server
/
index.go
168 lines (136 loc) · 3.72 KB
/
index.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
165
166
167
168
package adapters
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/dbmedialab/prebid-server/pbs"
"golang.org/x/net/context/ctxhttp"
"github.com/mxmCherry/openrtb"
)
type IndexAdapter struct {
http *HTTPAdapter
URI string
usersyncInfo *pbs.UsersyncInfo
}
/* Name - export adapter name */
func (a *IndexAdapter) Name() string {
return "indexExchange"
}
// used for cookies and such
func (a *IndexAdapter) FamilyName() string {
return "indexExchange"
}
func (a *IndexAdapter) GetUsersyncInfo() *pbs.UsersyncInfo {
return a.usersyncInfo
}
func (a *IndexAdapter) SkipNoCookies() bool {
return false
}
type indexParams struct {
SiteID int `json:"siteID"`
}
func (a *IndexAdapter) Call(ctx context.Context, req *pbs.PBSRequest, bidder *pbs.PBSBidder) (pbs.PBSBidSlice, error) {
if req.App != nil {
return nil, fmt.Errorf("Index doesn't support apps")
}
mediaTypes := []pbs.MediaType{pbs.MEDIA_TYPE_BANNER, pbs.MEDIA_TYPE_VIDEO}
indexReq, err := makeOpenRTBGeneric(req, bidder, a.FamilyName(), mediaTypes, true)
if err != nil {
return nil, err
}
for i, unit := range bidder.AdUnits {
var params indexParams
err := json.Unmarshal(unit.Params, ¶ms)
if err != nil {
return nil, fmt.Errorf("unmarshal params '%s' failed: %v", unit.Params, err)
}
if params.SiteID == 0 {
return nil, errors.New("Missing siteID param")
}
indexReq.Imp[i].TagID = unit.Code
// Index spec says "adunit path representing ad server inventory" but we don't have this
// ext is DFP div ID and KV pairs if avail
//indexReq.Imp[i].Ext = openrtb.RawJSON("{}")
siteCopy := *indexReq.Site
siteCopy.Publisher = &openrtb.Publisher{ID: fmt.Sprintf("%d", params.SiteID)}
indexReq.Site = &siteCopy
}
// spec also asks for publisher id if set
// ext object on request for prefetch
j, _ := json.Marshal(indexReq)
debug := &pbs.BidderDebug{
RequestURI: a.URI,
}
if req.IsDebug {
debug.RequestBody = string(j)
bidder.Debug = append(bidder.Debug, debug)
}
httpReq, err := http.NewRequest("POST", a.URI, bytes.NewBuffer(j))
httpReq.Header.Add("Content-Type", "application/json;charset=utf-8")
httpReq.Header.Add("Accept", "application/json")
ixResp, err := ctxhttp.Do(ctx, a.http.Client, httpReq)
if err != nil {
return nil, err
}
debug.StatusCode = ixResp.StatusCode
if ixResp.StatusCode == 204 {
return nil, nil
}
if ixResp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP status: %d", ixResp.StatusCode)
}
defer ixResp.Body.Close()
body, err := ioutil.ReadAll(ixResp.Body)
if err != nil {
return nil, err
}
if req.IsDebug {
debug.ResponseBody = string(body)
}
var bidResp openrtb.BidResponse
err = json.Unmarshal(body, &bidResp)
if err != nil {
return nil, fmt.Errorf("Error parsing response: %v", err)
}
bids := make(pbs.PBSBidSlice, 0)
numBids := 0
for _, sb := range bidResp.SeatBid {
for i, bid := range sb.Bid {
numBids++
bidID := bidder.LookupBidID(bid.ImpID)
if bidID == "" {
return nil, fmt.Errorf("Unknown ad unit code '%s'", bid.ImpID)
}
pbid := pbs.PBSBid{
BidID: bidID,
AdUnitCode: bidder.AdUnits[i].Code, // todo: check this
BidderCode: bidder.BidderCode,
Price: bid.Price,
Adm: bid.AdM,
Creative_id: bid.CrID,
Width: bid.W,
Height: bid.H,
DealId: bid.DealID,
}
bids = append(bids, &pbid)
}
}
return bids, nil
}
func NewIndexAdapter(config *HTTPAdapterConfig, uri string, userSyncURL string) *IndexAdapter {
a := NewHTTPAdapter(config)
info := &pbs.UsersyncInfo{
URL: userSyncURL,
Type: "redirect",
SupportCORS: false,
}
return &IndexAdapter{
http: a,
URI: uri,
usersyncInfo: info,
}
}