forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
209 lines (178 loc) · 6.33 KB
/
client.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package prebid_cache_client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/metrics"
"github.com/buger/jsonparser"
"github.com/golang/glog"
"golang.org/x/net/context/ctxhttp"
)
// Client stores values in Prebid Cache. For more info, see https://github.com/prebid/prebid-cache
type Client interface {
// PutJson stores JSON values for the given openrtb2.Bids in the cache. Null values will be
//
// The returned string slice will always have the same number of elements as the values argument. If a
// value could not be saved, the element will be an empty string. Implementations are responsible for
// logging any relevant errors to the app logs
PutJson(ctx context.Context, values []Cacheable) ([]string, []error)
// GetExtCacheData gets the scheme, host, and path of the externally accessible cache url.
GetExtCacheData() (scheme string, host string, path string)
}
type PayloadType string
const (
TypeJSON PayloadType = "json"
TypeXML PayloadType = "xml"
)
type Cacheable struct {
Type PayloadType `json:"type,omitempty"`
Data json.RawMessage `json:"value,omitempty"`
TTLSeconds int64 `json:"ttlseconds,omitempty"`
Key string `json:"key,omitempty"`
BidID string `json:"bidid,omitempty"` // this is "/vtrack" specific
Bidder string `json:"bidder,omitempty"` // this is "/vtrack" specific
Timestamp int64 `json:"timestamp,omitempty"` // this is "/vtrack" specific
}
func NewClient(httpClient *http.Client, conf *config.Cache, extCache *config.ExternalCache, metrics metrics.MetricsEngine) Client {
return &clientImpl{
httpClient: httpClient,
putUrl: conf.GetBaseURL() + "/cache",
externalCacheScheme: extCache.Scheme,
externalCacheHost: extCache.Host,
externalCachePath: extCache.Path,
metrics: metrics,
}
}
type clientImpl struct {
httpClient *http.Client
putUrl string
externalCacheScheme string
externalCacheHost string
externalCachePath string
metrics metrics.MetricsEngine
}
func (c *clientImpl) GetExtCacheData() (string, string, string) {
path := c.externalCachePath
if path == "/" {
// Only the slash for the path, remove it to empty
path = ""
} else if len(path) > 0 && !strings.HasPrefix(path, "/") {
// Path defined but does not start with "/", prepend it
path = "/" + path
}
return c.externalCacheScheme, c.externalCacheHost, path
}
func (c *clientImpl) PutJson(ctx context.Context, values []Cacheable) (uuids []string, errs []error) {
errs = make([]error, 0, 1)
if len(values) < 1 {
return nil, errs
}
uuidsToReturn := make([]string, len(values))
postBody, err := encodeValues(values)
if err != nil {
logError(&errs, "Error creating JSON for prebid cache: %v", err)
return uuidsToReturn, errs
}
httpReq, err := http.NewRequest("POST", c.putUrl, bytes.NewReader(postBody))
if err != nil {
logError(&errs, "Error creating POST request to prebid cache: %v", err)
return uuidsToReturn, errs
}
httpReq.Header.Add("Content-Type", "application/json;charset=utf-8")
httpReq.Header.Add("Accept", "application/json")
startTime := time.Now()
anResp, err := ctxhttp.Do(ctx, c.httpClient, httpReq)
elapsedTime := time.Since(startTime)
if err != nil {
c.metrics.RecordPrebidCacheRequestTime(false, elapsedTime)
logError(&errs, "Error sending the request to Prebid Cache: %v; Duration=%v, Items=%v, Payload Size=%v", err, elapsedTime, len(values), len(postBody))
return uuidsToReturn, errs
}
defer anResp.Body.Close()
c.metrics.RecordPrebidCacheRequestTime(true, elapsedTime)
responseBody, err := io.ReadAll(anResp.Body)
if anResp.StatusCode != 200 {
logError(&errs, "Prebid Cache call to %s returned %d: %s", c.putUrl, anResp.StatusCode, responseBody)
return uuidsToReturn, errs
}
currentIndex := 0
processResponse := func(uuidObj []byte, _ jsonparser.ValueType, _ int, err error) {
if uuid, valueType, _, err := jsonparser.Get(uuidObj, "uuid"); err != nil {
logError(&errs, "Prebid Cache returned a bad value at index %d. Error was: %v. Response body was: %s", currentIndex, err, string(responseBody))
} else if valueType != jsonparser.String {
logError(&errs, "Prebid Cache returned a %v at index %d in: %v", valueType, currentIndex, string(responseBody))
} else {
if uuidsToReturn[currentIndex], err = jsonparser.ParseString(uuid); err != nil {
logError(&errs, "Prebid Cache response index %d could not be parsed as string: %v", currentIndex, err)
uuidsToReturn[currentIndex] = ""
}
}
currentIndex++
}
if _, err := jsonparser.ArrayEach(responseBody, processResponse, "responses"); err != nil {
logError(&errs, "Error interpreting Prebid Cache response: %v\nResponse was: %s", err, string(responseBody))
return uuidsToReturn, errs
}
return uuidsToReturn, errs
}
func logError(errs *[]error, format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
glog.Error(msg)
*errs = append(*errs, errors.New(msg))
}
func encodeValues(values []Cacheable) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString(`{"puts":[`)
for i := 0; i < len(values); i++ {
if err := encodeValueToBuffer(values[i], i != 0, &buf); err != nil {
return nil, err
}
}
buf.WriteString("]}")
return buf.Bytes(), nil
}
func encodeValueToBuffer(value Cacheable, leadingComma bool, buffer *bytes.Buffer) error {
if leadingComma {
buffer.WriteByte(',')
}
buffer.WriteString(`{"type":"`)
buffer.WriteString(string(value.Type))
if value.TTLSeconds > 0 {
buffer.WriteString(`","ttlseconds":`)
buffer.WriteString(strconv.FormatInt(value.TTLSeconds, 10))
buffer.WriteString(`,"value":`)
} else {
buffer.WriteString(`","value":`)
}
buffer.Write(value.Data)
if len(value.Key) > 0 {
buffer.WriteString(`,"key":"`)
buffer.WriteString(string(value.Key))
buffer.WriteString(`"`)
}
//vtrack specific
if len(value.BidID) > 0 {
buffer.WriteString(`,"bidid":"`)
buffer.WriteString(string(value.BidID))
buffer.WriteString(`"`)
}
if len(value.Bidder) > 0 {
buffer.WriteString(`,"bidder":"`)
buffer.WriteString(string(value.Bidder))
buffer.WriteString(`"`)
}
if value.Timestamp > 0 {
buffer.WriteString(`,"timestamp":`)
buffer.WriteString(strconv.FormatInt(value.Timestamp, 10))
}
buffer.WriteByte('}')
return nil
}