-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource-http.go
204 lines (177 loc) · 4.13 KB
/
datasource-http.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
package main
import (
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/Kong/go-pdk"
"github.com/dgraph-io/ristretto"
"github.com/spf13/cast"
)
type Duration time.Duration
func (t *Duration) UnmarshalText(text []byte) (err error) {
d, err := time.ParseDuration(string(text))
if err != nil {
return err
}
*t = Duration(d)
return
}
func (t Duration) MarshalText() (text []byte, err error) {
return []byte(time.Duration(t).String()), nil
}
type DatasourceHTTP struct {
URL Template `json:"url" yaml:"url"`
Method Template `json:"method" yaml:"method"`
QueryParams map[string]Template `json:"query_params" yaml:"query_params"`
Headers map[string]Template `json:"headers" yaml:"headers"`
Body Template `json:"body" yaml:"body"`
Timeout *Duration `json:"timeout" yaml:"timeout"`
CacheKey Template `json:"cache-key" yaml:"cache-key"`
TTL Template `json:"ttl" yaml:"ttl"`
hc *http.Client
rcache *ristretto.Cache
}
type configDatasourceHTTP struct {
URL string `json:"url" yaml:"url"`
Method string `json:"method" yaml:"method"`
QueryParams map[string]string `json:"query_params" yaml:"query_params"`
Headers map[string]string `json:"headers" yaml:"headers"`
Body string `json:"body" yaml:"body"`
Timeout *string `json:"timeout" yaml:"timeout"`
CacheKey string `json:"cache-key" yaml:"cache-key"`
TTL string `json:"ttl" yaml:"ttl"`
}
func (dh *DatasourceHTTP) Init() (err error) {
t := 10 * time.Second
if dh.Timeout != nil {
t = time.Duration(*dh.Timeout)
}
dh.hc = &http.Client{Timeout: t}
dh.rcache, err = ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
})
return
}
func (dh *DatasourceHTTP) ToData(kong *pdk.PDK, data Data) (d map[string]interface{}, err error) {
d, cacheKey, err := dh.getCache(data)
if d != nil || err != nil {
return
}
ur, err := dh.URL.ToString(data)
if err != nil {
return
}
u, err := url.Parse(ur)
if err != nil {
return
}
m, err := dh.Method.ToString(data)
if err != nil {
return
}
if m == "" {
m = http.MethodGet
}
q := u.Query()
for k, v := range dh.QueryParams {
s, err := v.ToString(data)
if err != nil {
return nil, err
}
q.Add(k, s)
}
u.RawQuery = q.Encode()
b, err := dh.Body.ToString(data)
if err != nil {
return
}
req, err := http.NewRequest(m, u.String(), strings.NewReader(b))
if err != nil {
return
}
for h, t := range dh.Headers {
v, err := t.ToString(data)
if err != nil {
return nil, err
}
req.Header.Add(h, v)
}
res, err := dh.hc.Do(req)
if err != nil {
return
}
h := map[string]interface{}{}
for k, v := range res.Header {
h[strings.ReplaceAll(strings.ToLower(k), "-", "_")] = v
}
rs, err := io.ReadAll(res.Body)
if err != nil {
return
}
var rj interface{} = map[string]interface{}{}
if err := json.Unmarshal(rs, &rj); err != nil {
rj = string(rs)
}
d = map[string]interface{}{}
d["response"] = map[string]interface{}{
"status_code": res.StatusCode,
"headers": h,
"body": rj,
}
err = dh.cache(cacheKey, d, data)
return
}
func (dh *DatasourceHTTP) getCache(data Data) (d map[string]interface{}, key string, err error) {
if dh.TTL.Empty() {
return
}
key, err = dh.CacheKey.ToString(data)
if err != nil {
return
}
v, ok := dh.rcache.Get(key)
if !ok {
return
}
if d, ok = v.(map[string]interface{}); !ok {
return nil, "", nil
}
return
}
func (dh *DatasourceHTTP) cache(key string, d map[string]interface{}, data map[string]interface{}) (err error) {
if dh.TTL.Empty() {
return
}
m := map[string]interface{}{"__self__": d}
for k, v := range data {
m[k] = v
}
s, err := dh.TTL.ToString(m)
if err != nil {
return
}
ttl, err := time.ParseDuration(s)
if err != nil {
d, err := cast.StringToDate(s)
if err != nil {
return err
}
now := time.Now()
if d.Before(now) {
ttl = 0
} else {
ttl = d.Sub(now)
}
}
err = nil
if ttl <= 0 {
return
}
dh.rcache.SetWithTTL(key, d, 1, ttl)
return
}